May 08, 2024, 06:06:05 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Use of GetTextSize() in a subclassed dialog

Started by Bruce Peaslee, January 28, 2006, 01:18:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bruce Peaslee

I use the function GetTextSize() successfully in a subclassed window to help me center text. When I want to get text information in a subclassed dialog, all I get are zeroes.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

That function has to look up the information from a window's device context (DC). And seeing as you can't draw to dialogs, I don't think they have a DC.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_8smq.asp
However, you can create your own DC and set its font information using CreateFont or CreateFontIndirect. Remember to free it once you're done.

Parker

This seems to do something at least
#define LF_FACESIZE 32
struct LOGFONT
{
unsigned int lfHeight;
unsigned int lfWidth;
unsigned int lfEsapement;
unsigned int lfOrientation;
unsigned int lfWeight;
byte lfItalic;
byte lfUnderline;
byte lfStrikeOut;
byte lfCharSet;
byte lfOutPrecision;
byte lfClipPrecision;
byte lfQuality;
byte lfPitchAndFamily;
dstring lfFaceName[LF_FACESIZE];
}

DECLARE IMPORT,GetTextExtentPoint32 alias GetTextExtentPoint32A(
hdc as unsigned int,
lpszStr as STRING,
cbString as INT,
lpSize as POINT
),INT;

declare import, CreateFontIndirect alias CreateFontIndirectA(
LOGFONT *lf
),unsigned int;

declare import, DeleteObject(
unsigned int hObject
),int;

declare import, DeleteDC(
unsigned int hDC
),int;

declare import, CreateCompatibleDC(
unsigned int hDC
),unsigned int;

declare import, CreateCompatibleBitmap(
unsigned int hDC,
int nWidth,
int nHeight
),unsigned int;

declare import, ZeroMemory alias RtlZeroMemory(
void *Destination,
unsigned int Length
);

declare import, MulDiv(
int nNumber,
int nNumerator,
int nDenominator
),int;

declare import, SelectObject(
unsigned int hdc,
unsigned int hgdiobj
),unsigned int;

declare import, GetDeviceCaps(
unsigned int hdc,
int nIndex
),int;

global sub main()
{
LOGFONT lf;
POINT pt;
unsigned int hFont, hBitmap, hDC;

hDC = CreateCompatibleDC(0);
hBitmap = CreateCompatibleBitmap(hDC, 100, 100);

ZeroMemory(&lf, len(LOGFONT));
// Fill the members of lf that you want
lf.lfFaceName = "Courier New";
lf.lfHeight = -MulDiv(10, GetDeviceCaps(hDC, 90 /*LOGPIXELSY*/), 72);
lf.lfWeight = 100;
lf.lfQuality = 1; /*DRAFT_QUALITY*/
lf.lfPitchAndFamily = 0; /*DRAFT_PITCH*/
hFont = CreateFontIndirect(&lf);

// Set 'lf' as the DC's font
SelectObject(hDC, hFont);

GetTextExtentPoint32(hDC, "Hello", len("Hello"), pt);
writeln("x: " + str$(pt.x,0) + " - y: " + str$(pt.y,0));

DeleteObject(hBitmap);
DeleteDC(hDC);
DeleteObject(hFont);

do{}until (GetKey() <> "");
}


Try it with your dialog and let me know if it works ;)

Bruce Peaslee

It works, but not for want I want.ÂÃ,  ;)

This is a better explanation of what I am trying to do. I am putting a static control on a dialog into which I will write some text. I want to resize the static control depending on how much text (how high). If I know the font size, I can compute the size based on the number of lines. I know how to set the control's font but not how to get it. Another way is to use GetTextSize, but I can't make it work for a dialog control. Your example works well, but requires knowledge of the font.

I'm trying to make my class as generic as possible so I want to avoid setting the control's font.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Bruce Peaslee

Quote from: peaslee on January 28, 2006, 04:22:17 PM
I'm trying to make my class as generic as possible so I want to avoid setting the control's font.

Oops - I hadn't thought this through. I'm either setting font or the programmer would be (if the class is given that capability), so I will know its size. I'm still curious about the answer, but if I had been thinking clearly, the question would never have come up.ÂÃ,  ÂÃ, :-[
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Parker

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowmessages/wm_getfont.asp
You can send a WM_GETFONT message to that control, or you can get its DC. Maybe a pControl->GetTextSize() call? The control is derived from window so you can do that.