Announcement

Collapse
No announcement yet.

How does one retrieve textmetrics from a dialog control?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How does one retrieve textmetrics from a dialog control?

    How can I retrieve a textmetric-structure from a control (for instance label or listbox)
    within a dialog? I know, I have to treat the control as a device context, but I really don't know how.
    Petzold and Brent/Newcomer do'nt provide a solution either.

    Regards and thanks,
    [email protected]

    ------------------

    Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
    http://zijlema.basicguru.eu
    *** Opinions expressed here are not necessarily untrue ***

  • #2
    "The TEXTMETRIC structure contains basic information about a
    physical font." In other words, by using GetTextMetrics one
    can retrieve the data for the font used by a control or dialog,
    like: (hWnd is the control's or dialog's hWnd)
    Code:
        LOCAL hDc AS LONG, tm AS TEXTMETRIC
        hDC = GetDC(hWnd)
        GetTextMetrics hDC, tm
        ReleaseDC hWnd, hDC
        MSGBOX STR$(tm.tmHeight) 'show character height
    ------------------

    Comment


    • #3
      Egbert --
      Probably, this code explain you something.
      Code:
         #Compile Exe
         #Register None
         #Dim All
         #Include "Win32Api.Inc"
      
         %ID_Label =  101
         %ID_TextBox =  101
         %ID_Button = 301
         CallBack Function DlgProc
            Static hWndT As Long, hFontT As Long
            Select Case CbMsg
               Case %WM_INITDIALOG
                  hWndT = GetDlgItem(CbHndl, %ID_LABEL)
                  hFontT = CreateFont(-14, 0, 0, 0, %FW_BOLD, %True, 0, 0, _
                     %RUSSIAN_CHARSET, 0, 0, 0, 0, "Times New Roman")
                  SendMessage hWndT, %WM_SETFONT, hFontT, 0
               Case %WM_DESTROY
                  DeleteObject hFontT
               Case %WM_COMMAND
                 If CbCtl = %ID_BUTTON Then
                    Local hDC As Long, tm As TextMetric, lf As LOGFONT, hFont As Long, hFontOld As Long
                    hFont = SendMessage(hWndT, %WM_GETFONT, 0, 0)
                    hDC = GetDC(hWndT)
                    hFontOld = SelectObject(hDC, hFont)
                    GetTextMetrics hdc, tm
                    SelectObject hdc, hFontOld
                    ReleaseDC hWndT, hDC
                    GetObject hFont, SizeOf(lf), ByVal VarPtr(lf)
                    MsgBox lf.lfFaceName + _
                           "  lf.lfHeight = "       + Format$(lf.lfHeight) + _
                           "  lf.lfWidth = "        + Format$(lf.lfWidth) + _
                           "  tm.tmHeight = "       + Format$(tm.tmHeight) + _
                           "  tm.tmAveCharWidth = " + Format$(tm.tmAveCharWidth)
                 End If
            End Select
         End Function
      
         Function PbMain
            Local hDlg As Long
            Dialog New 0, "",,, 300, 150, %WS_SYSMENU Or %WS_CAPTION To hDlg
            Control Add Label, hDlg, %ID_Label, "Label", 10, 5, 280, 10, %SS_RIGHT
            Control Add TextBox, hDlg, %ID_TextBox, "", 10, 20, 280, 90, _
                %ES_WANTRETURN Or %ES_MULTILINE, %WS_EX_CLIENTEDGE
            Control Add Button, hDlg, %ID_Button, "Show", 10, 120, 280, 15
            Dialog Show Modal hDlg Call DlgProc
         End Function
      ------------------

      Comment

      Working...
      X