15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


GetTextExtent in zoom mode

Kalyan Chakravarthy -- kalyan@narmada.wipsys.stph.net
Tuesday, March 18, 1997

Environment: Win 95, VC++ 4.1

I have some text objects displayed on the client area. I have a zoom
functionality where the user can zoom-in/out and I want a box to be drawn
around the text object. In zoom mode the GetTextExtent function is
returning the width of string greater than what is being displayed on the
client area, due to which the enclosing box is larger than the actual size
of the text object. GetTextExtent() documentation says that the size that
is returned may not be equal to what is being displayed. Is there any
work-around for this problem so that I can display the rectangle same as
the size of the text object?

Any help in this regard is appreciated.

Thanks in advance,
Kalyan.V
(kalyan@wipsys.stph.net)



ASN Raju -- asn@infotech.stph.net
Saturday, March 22, 1997

[Mini-digest: 2 responses]

>Environment: Win 95, VC++ 4.1
>
>I have some text objects displayed on the client area. I have a zoom
>functionality where the user can zoom-in/out and I want a box to be draw=
n
>around the text object. In zoom mode the GetTextExtent function is
>returning the width of string greater than what is being displayed on th=
e
>client area, due to which the enclosing box is larger than the actual si=
ze
>of the text object. GetTextExtent() documentation says that the size tha=
t
>is returned may not be equal to what is being displayed. Is there any
>work-around for this problem so that I can display the rectangle same as
>the size of the text object?
>
>Any help in this regard is appreciated.
>
>Thanks in advance,
>Kalyan.V
>(kalyan@wipsys.stph.net)
>
>

Hi Kalyan,

When you are doing zoom in/zoom out, you must be knowing the scale factor
and the offset.

Use GetTextMetrics f(n) to get the text width and height and now calculat=
e
the length of the text string and=20
multiply it with the text width. These calculated text width and height
should be multiplied by the scale factor.

ex:

        CString                         strChar("Hello Kalyan");
        TEXTMETRIC          tm;
        CDC                             *pDC  =3D  GetDC();
        CRect                           Rect;

         pDC-> GetTextMetrics(&tm);
       =20
        Rect.left =3D             // you know this this is the current of=
fset
value to display this string
        Rect.top =3D             // This is also known to you
        Rect.right =3D Rect.left + tm.tmWidth * strChar.GetLength() *
Scale_factor;
        Rect.bottom =3D Rect.top + tm.tmHeight * Scale_factor;

        // Now Rect contains the bounding rectangle of your text string.
        // You can use tm.AveCharWidth and tm.InternalLeading etc paramet=
ers
for better and accurate=20
        // calculation of the rectangle

        //IMP:  The font with which you are displaying the string should =
be
selected into pDC before calling
        // GetTextMetrics becuase this f(n) gives the values based on
currently selected font.

Hope this solves your problem
Bye
ASN Raju

-----From: "Claire Rollet" 

Maybe, by the time you call the "GetTextExtent" you didn't selected the n=
ew
"zoommed" bigger font in your dc.=20
------------------------------------------------------
Claire Rollet
crollet@sympatico.ca

----------
> De : Kalyan Chakravarthy 
> A : mfc-l@netcom.com
> Objet : GetTextExtent in zoom mode
> Date=A0: 17 mars, 1997 21:02
>=20
> Environment: Win 95, VC++ 4.1
>=20
> I have some text objects displayed on the client area. I have a zoom
> functionality where the user can zoom-in/out and I want a box to be dra=
wn
> around the text object. In zoom mode the GetTextExtent function is
> returning the width of string greater than what is being displayed on t=
he
> client area, due to which the enclosing box is larger than the actual
size
> of the text object. GetTextExtent() documentation says that the size th=
at
> is returned may not be equal to what is being displayed. Is there any
> work-around for this problem so that I can display the rectangle same a=
s
> the size of the text object?
>=20
> Any help in this regard is appreciated.
>=20
> Thanks in advance,
> Kalyan.V
> (kalyan@wipsys.stph.net)



Greg D. Tighe -- gdt@eng.aisinc.com
Tuesday, March 25, 1997

If you get really desperate you can always create a single-plane 
bitmap whose dimensions are based upon the values returned by 
GetTextExtent().  Create a DC, select this bitmap into the DC and 
draw your zoomed text into the DC.

You can now access the pixels of the bitmap using GetBitmapBits() and 
scan the bitmap row by row for any 'black' ('zero') pixels.  Keep 
track of the first row which contains at least one black pixel, the 
last row which contains a black pixels, the lowest pixel offset (into 
any row) at which a black pixel occurs and the highest pixel offset 
for a black pixel.

These values will indicate the actual bounding box for your text, as 
rendered at that particular zoom level.


> Environment: Win 95, VC++ 4.1
> 
> I have some text objects displayed on the client area. I have a zoom
> functionality where the user can zoom-in/out and I want a box to be drawn
> around the text object. In zoom mode the GetTextExtent function is
> returning the width of string greater than what is being displayed on the
> client area, due to which the enclosing box is larger than the actual size
> of the text object. GetTextExtent() documentation says that the size that
> is returned may not be equal to what is being displayed. Is there any
> work-around for this problem so that I can display the rectangle same as
> the size of the text object?
> 

	-Greg Tighe
	Applied Intelligent Systems, Inc.
	Ann Arbor, MI
	gdt@aisinc.com




Become an MFC-L member | Вернуться в корень Архива |