在logfont结构中有lfWidth的属性,但如果是一个设定好字体和字号的单行编辑框,如何获取对应的font的lfwidth/?最好是基于win32api的实现,,多谢1!

解决方案 »

  1.   

    CFont *pfont = new CFont;
    pfont = GetDlgItem(IDC_EIDT1)->GetFont();
    LOGFONT logfont;
    pfont->GetLogFont(&logfont);
    logfont里的lfWidth就是
      

  2.   

    HFONT hFont = (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0);
    LOGFONT logFont;
    GetObject(hFont , sizeof(LOGFONT), &logFont);
      

  3.   

    HFONT hFont = (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0);
    LOGFONT logFont;
    GetObject(hFont , sizeof(LOGFONT), &logFont);
      

  4.   

    如果EDIT是采用的当前系统字体,HFONT hFont = (HFONT)SendMessage(hWnd, WM_GETFONT, 0, 0);会返回NULL。
      

  5.   

    GetTextExtent(str)能直接获取str的长度,
    但只有在CDC类中使用(它内部是要获取当前dc的字体信息)
      

  6.   

    龙工同志提的问题可以参考这个
    HOWTO: Change the Font Used by Dialog Controls in Windows Q74737
    --------------------------------------------------------------------------------
    The information in this article applies to:Microsoft Win32 Software Development Kit (SDK) 
    Microsoft Windows Software Development Kit (SDK) 3.1--------------------------------------------------------------------------------
    SUMMARY
    In Windows 3.x, there are two ways to specify the font used by dialog controls: The FONT statement can be used in the dialog template to specify the font used by ALL the controls in the dialog box. 
    The WM_SETFONT message can be sent to one or more dialog controls during the processing of the WM_INITDIALOG message. 
    If a font is specified in the dialog template, the controls will use a bold version of that font. The following code demonstrates how to change the font used by dialog box controls to a nonbold font using WM_SETFONT. The font should be deleted with DeleteObject() when the dialog box is closed. 
    Sample Code   HWND hDlg;
       HFONT hDlgFont;
       LOGFONT lFont;   case WM_INITDIALOG:
           /* Get dialog font and create non-bold version */ 
           hDlgFont = NULL;
           if ((hDlgFont = (HFONT)SendMessage(hDlg, WM_GETFONT, 0, 0L))
                    != NULL)
              {
              if (GetObject(hDlgFont, sizeof(LOGFONT), (LPSTR)&lFont)
                        != NULL)
                 {
                 lFont.lfWeight = FW_NORMAL;
                 if ((hDlgFont = CreateFontIndirect(&lFont)) != NULL)
                    {
                    SendDlgItemMessage(hDlg, CTR1, WM_SETFONT, hDlgFont, 0L);
                    // Send WM_SETFONT message to desired controls
                    }
                 }
              }
           else  // user did not specify a font in the dialog template
              {  // must simulate system font
              lFont.lfHeight = 13;
              lFont.lfWidth = 0;
              lFont.lfEscapement = 0;
              lFont.lfOrientation = 0;
              lFont.lfWeight = 200; // non-bold font weight
              lFont.lfItalic = 0;
              lFont.lfUnderline = 0;
              lFont.lfStrikeOut = 0;
              lFont.lfCharSet = ANSI_CHARSET;
              lFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
              lFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
              lFont.lfQuality = DEFAULT_QUALITY;
              lFont.lfPitchAndFamily = VARIABLE_PITCH | FF_SWISS;
              lFont.lfFaceName[0] = NULL;
              hDlgFont = CreateFontIndirect(&lFont);          SendDlgItemMessage(hDlg, CTR1, WM_SETFONT, hDlgFont,
                 (DWORD)TRUE);
              // Send WM_SETFONT message to desired controls
              }      return TRUE;
          break; Additional query words: Keywords : kbDlg kbNTOS kbWinOS2000 kbSDKWin32 kbGrpUser kbWinOS 
    Issue type : kbhowto 
    Technology : kbvcSearch 
    Last Reviewed: July 7, 2000
    © 2001 Microsoft Corporation. All rights reserved. Terms of Use.
     --------------------------------------------------------------------------------
    Send feedback to MSDN.Look here for MSDN Online resources.
      

  7.   

    多谢各位,我用的是bcb,代码如下:
     HDC hdc;
      hdc = GetDC(Edit1->Handle) ;
      LOGFONT font;
      LONG i;
      SIZE lstr_Size;
      char s[50],ss[20];  font.lfHeight = 15;
      font.lfWidth = 10;
      font.lfWeight = 400;
      font.lfCharSet = GB2312_CHARSET;//'0' ;
      strcpy(font.lfFaceName,"ËÎÌå");
      HFONT af,of;
      af =  CreateFontIndirect ( &font );
      of = SelectObject(hdc,af);
      LOGFONT logFont;
      GetObject(of , sizeof(LOGFONT), &logFont);
      i = logFont.lfWidth;
      itoa(i,ss,10)   ;
      ShowMessage( ss)  ;edit11的字体无论用24或者是18返回值都是7,麻烦帮看看什么地方错了,多谢!
      

  8.   

    麻烦各位帮看看,就是如果是8号的宋体字,如何知道lfwidth是多少?谢谢!
      

  9.   

    我用的函数,通过DC来取得字符的宽度,调用此函数前要先得到字体。void CXXView::GetDrawMetrics(CDC* pDC, CFont* pFont, int* pCharWidth, int* pCharHeight)
    {
    // retrieve title font
    CFont* pOldFont = pDC->SelectObject(pFont);

    TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);

    // get average character width and title bar height
    *pCharWidth = tm.tmAveCharWidth;
    *pCharHeight = tm.tmHeight + tm.tmExternalLeading;

    pDC->SelectObject(pOldFont);
    }