Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Mobile
  4. precalculating button control size issue

precalculating button control size issue

Scheduled Pinned Locked Moved Mobile
jsonhelptutorialquestionannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rodent
    wrote on last edited by
    #1

    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);

    J 1 Reply Last reply
    0
    • R rodent

      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);

      J Offline
      J Offline
      Joao Paulo Figueira
      wrote on last edited by
      #2

      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

      R 2 Replies Last reply
      0
      • J Joao Paulo Figueira

        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

        R Offline
        R Offline
        rodent
        wrote on last edited by
        #3

        thanks...my assumption was based on the sdk doc's concerning the ui services which show tahoma bold 8pt used as the button text...i'll play with my code to see if using the system font gives better results as you suggest.

        1 Reply Last reply
        0
        • J Joao Paulo Figueira

          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

          R Offline
          R Offline
          rodent
          wrote on last edited by
          #4

          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);

          J 1 Reply Last reply
          0
          • R rodent

            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);

            J Offline
            J Offline
            Joao Paulo Figueira
            wrote on last edited by
            #5

            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

            R 1 Reply Last reply
            0
            • J Joao Paulo Figueira

              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

              R Offline
              R Offline
              rodent
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups