precalculating button control size issue
-
using ppc2k2 with just wince api, i'm trying to determine the size required by a button control based on it's text. my understanding is that by default 8pt tahoma bold font is used for the button text. in order to determine the minimum button size, my plan is to use DrawText() to return the RECT structure of the text based upon the 8pt tahoma bold font in the DC. however, when i use the following code, i have to add padding to the RECT in order for the button to not be too small. what am i not understanding about correctly measuring the size of text in the DC used by the button control? thanks...
HWND hChildWnd; // handle to push button RECT cliRect, txtRect; // RECT size of main window client area & btn text HDC hdc; // handle to client DC HFONT hBtnFnt, hOldFnt; // handle to fonts LOGFONT lf; // pointer to LOGFONT structure // get size of main window's client area GetClientRect(hWnd, &cliRect); // zero structures memset(&txtRect, 0, sizeof(RECT)); memset(&lf, 0, sizeof(LOGFONT)); // get client area DC hdc = GetDC(hWnd); // set up LOGFONT for an 8 pt bold Tahoma font, the font used // for the button text lf.lfHeight = -8 * GetDeviceCaps(hdc, LOGPIXELSY)/72; lf.lfWeight = FW_BOLD; lf.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS; _tcscpy(lf.lfFaceName, _T("Tahoma")); // create new font based on LOGFONT member data hBtnFnt = CreateFontIndirect(&lf); // select new font into DC, save old font, and calculate button text // and return it in RECT structure. hOldFnt = (HFONT)SelectObject(hdc, hBtnFnt); DrawText(hdc, szBtnTxt, -1, &txtRect, DT_CALCRECT); // select old font back into DC, delete new font and release DC SelectObject(hdc, hOldFnt); DeleteObject(hBtnFnt); ReleaseDC(hWnd, hdc); // add padding to text size // SHOULDN'T HAVE TO ADD THIS MUCH PADDING IF SIZING IS CORRECT // FIND OUT WHY I DONT UNDERSTAND HOW TO GET CORRECT TEXT SIZE txtRect.bottom += 6; txtRect.right += 30; // create a centered pushbutton hChildWnd = CreateWindowEx(NULL, _T("BUTTON"), szBtnTxt, WS_VISIBLE | WS_CHILD | BS_CENTER | BS_VCENTER | BS_PUSHBUTTON, (cliRect.right/2 - txtRect.right/2) , cliRect.bottom - 25, txtRect.right, txtRect.bottom, hWnd, (HMENU)IDC_PROPBTN, g_hInst, NULL);
-
using ppc2k2 with just wince api, i'm trying to determine the size required by a button control based on it's text. my understanding is that by default 8pt tahoma bold font is used for the button text. in order to determine the minimum button size, my plan is to use DrawText() to return the RECT structure of the text based upon the 8pt tahoma bold font in the DC. however, when i use the following code, i have to add padding to the RECT in order for the button to not be too small. what am i not understanding about correctly measuring the size of text in the DC used by the button control? thanks...
HWND hChildWnd; // handle to push button RECT cliRect, txtRect; // RECT size of main window client area & btn text HDC hdc; // handle to client DC HFONT hBtnFnt, hOldFnt; // handle to fonts LOGFONT lf; // pointer to LOGFONT structure // get size of main window's client area GetClientRect(hWnd, &cliRect); // zero structures memset(&txtRect, 0, sizeof(RECT)); memset(&lf, 0, sizeof(LOGFONT)); // get client area DC hdc = GetDC(hWnd); // set up LOGFONT for an 8 pt bold Tahoma font, the font used // for the button text lf.lfHeight = -8 * GetDeviceCaps(hdc, LOGPIXELSY)/72; lf.lfWeight = FW_BOLD; lf.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS; _tcscpy(lf.lfFaceName, _T("Tahoma")); // create new font based on LOGFONT member data hBtnFnt = CreateFontIndirect(&lf); // select new font into DC, save old font, and calculate button text // and return it in RECT structure. hOldFnt = (HFONT)SelectObject(hdc, hBtnFnt); DrawText(hdc, szBtnTxt, -1, &txtRect, DT_CALCRECT); // select old font back into DC, delete new font and release DC SelectObject(hdc, hOldFnt); DeleteObject(hBtnFnt); ReleaseDC(hWnd, hdc); // add padding to text size // SHOULDN'T HAVE TO ADD THIS MUCH PADDING IF SIZING IS CORRECT // FIND OUT WHY I DONT UNDERSTAND HOW TO GET CORRECT TEXT SIZE txtRect.bottom += 6; txtRect.right += 30; // create a centered pushbutton hChildWnd = CreateWindowEx(NULL, _T("BUTTON"), szBtnTxt, WS_VISIBLE | WS_CHILD | BS_CENTER | BS_VCENTER | BS_PUSHBUTTON, (cliRect.right/2 - txtRect.right/2) , cliRect.bottom - 25, txtRect.right, txtRect.bottom, hWnd, (HMENU)IDC_PROPBTN, g_hInst, NULL);
I can't read your code, but I would say that you have a wrong assumption: rodent¹ wrote: my understanding is that by default 8pt tahoma bold font is used for the button text. It is not, unless you tell it to. By default you get the system font that is larger than Tahoma 8. Regards, João Paulo
-
I can't read your code, but I would say that you have a wrong assumption: rodent¹ wrote: my understanding is that by default 8pt tahoma bold font is used for the button text. It is not, unless you tell it to. By default you get the system font that is larger than Tahoma 8. Regards, João Paulo
-
I can't read your code, but I would say that you have a wrong assumption: rodent¹ wrote: my understanding is that by default 8pt tahoma bold font is used for the button text. It is not, unless you tell it to. By default you get the system font that is larger than Tahoma 8. Regards, João Paulo
thanks for the info joão paulo...after the liberal use of the following code snippet, i discovered a few interesting points re: wince fonts.. 1) the SYSTEM_FONT on my ppc2k2 sdk emulator and on my dell axim x5 is tahoma. the system font's LOGFONT lfHeight member was -12 while tmHeight, tmInternalLeading, tmDescent, and tmExternalLeading (from the system font's TEXTMETRIC struct) were 14, 2, 2, and 0 respectively. 2) to properly calculate the RECT size of the text used in my button control using the system font, i had to set lfHeight to 9 (??) in my first post's code snippet
hTmpFnt = (HFONT)GetStockObject(SYSTEM_FONT); GetObject(hTmpFnt, sizeof(LOGFONT), &lf); GetTextMetrics(hdc, &tm); wsprintf(szOut, _T("LOGFONT:\nFace: %s, h: %li\n\nTEXTMETRIC:\nh: %li, il: %li, d: %li, el: %li"), lf.lfFaceName, lf.lfHeight, tm.tmHeight, tm.tmInternalLeading, tm.tmDescent, tm.tmExternalLeading); MessageBox(hWnd, szOut, _T("Sys Font Info"), MB_OK);
-
thanks for the info joão paulo...after the liberal use of the following code snippet, i discovered a few interesting points re: wince fonts.. 1) the SYSTEM_FONT on my ppc2k2 sdk emulator and on my dell axim x5 is tahoma. the system font's LOGFONT lfHeight member was -12 while tmHeight, tmInternalLeading, tmDescent, and tmExternalLeading (from the system font's TEXTMETRIC struct) were 14, 2, 2, and 0 respectively. 2) to properly calculate the RECT size of the text used in my button control using the system font, i had to set lfHeight to 9 (??) in my first post's code snippet
hTmpFnt = (HFONT)GetStockObject(SYSTEM_FONT); GetObject(hTmpFnt, sizeof(LOGFONT), &lf); GetTextMetrics(hdc, &tm); wsprintf(szOut, _T("LOGFONT:\nFace: %s, h: %li\n\nTEXTMETRIC:\nh: %li, il: %li, d: %li, el: %li"), lf.lfFaceName, lf.lfHeight, tm.tmHeight, tm.tmInternalLeading, tm.tmDescent, tm.tmExternalLeading); MessageBox(hWnd, szOut, _T("Sys Font Info"), MB_OK);
rodent¹ wrote: the SYSTEM_FONT on my ppc2k2 sdk emulator and on my dell axim x5 is tahoma You are luckier than me. My iPAQ 3850 gives me something other than Tahoma 8. But I believe that you can change this using a registry setting. Maybe Dell did that for you... Regards, João Paulo
-
rodent¹ wrote: the SYSTEM_FONT on my ppc2k2 sdk emulator and on my dell axim x5 is tahoma You are luckier than me. My iPAQ 3850 gives me something other than Tahoma 8. But I believe that you can change this using a registry setting. Maybe Dell did that for you... Regards, João Paulo
interesting. as you suggested in an earlier post, it makes sense to use SYSTEM_FONT rather than assuming Tahoma...more cross platform. fyi, i discovered the reason why i had to use -9 for lfHeight (rather than -12) was that i was inconsistently muliplying by GetDeviceCaps(hdc, LOGPIXELSY)/72...everything works as expected when i use it consistently. thanks for the feedback