Question : Why is ComboBox font bold after a Create()

When I create a simple CComboBox in a dialog that is to overlay a placeholder, then the font that I when I enter characters in the edit box is boldface.  If the box is created from a resource, then the font is normal.

What I want to do is create simple combobox with normal dialog box font.

Here is the code snippet from OnInitDialog():

    // this will overlay the placeholder text with a simple
    // combobox of the same size
    // ?? the problem is that the text is boldface in the edit box
    CWnd* pPlaceHolder = GetDlgItem(IDC_PLACEHOLDER);
    CRect rect;
    pPlaceHolder->GetWindowRect(&rect);
    pPlaceHolder->DestroyWindow();
    ScreenToClient(&rect);
    m_cbNewBox.Create(CBS_SIMPLE | CBS_SORT | CBS_AUTOHSCROLL
       | WS_CHILD | WS_VISIBLE
       | WS_TABSTOP | WS_VSCROLL
     , rect, this, IDC_COMBO3);

    // now we look at the style and font
    CWnd* pWnd;
    CFont* pFont;
    HWND hwnd;
    long style;
    pWnd = GetDlgItem(IDC_COMBO3);
    hwnd = pWnd->GetSafeHwnd();
    style = GetWindowLong(hwnd, GWL_STYLE);
    // ?? why is this font return null?
    pFont = pWnd->GetFont();
    if(pFont) {
        LOGFONT lf;
        pFont->GetLogFont(&lf);
        pFont = NULL;
    }

When I step thru this with the debugger, the style is 0x50010341 which corresponds to a simple combobox created from a resource.  The combobox that is actually created is the correct size and is a simple box with no list so it looks just like an edit control - fine so far!  However when I type, the characters are boldface.  In the code above, I thought I could check the font but the pFont returned is null.

When I put a combobox into the dialog via the resource editor, and doing the same thing as above, then the pFont points somewhere and I get the LOGFONT struct filled.  Inside the lf struct as seen from the debugger are the following nonzero items:

lfHeight 0xfffffff3
lfWeight 0x00000190
lfFaceName "MS Sans Serif"

I'm not  sure what the weight means but typing in the box produces normal weight characters for a dialog box.

Answer : Why is ComboBox font bold after a Create()

When Windows creates a dialog box, it also creates all the controls. The controls are created to have the same font as the dialog box resource. That way, everything looks homogenous.

When you create a control with CreateWindow(), Windows doesn't look at the parent and pull its font info. It just creates the window for you.

If you want, you can set the font yourself. Just send WM_GETFONT to the dialog, and use WM_SETFONT to set the resulting font to your new control. If you're using MFC, you can use the GetFont() and SetFont() members.

When you create a new window and call GetFont() on it, you get NULL because no font has ever been set to that window. That means that Windows is using the default font system stock object.

The lfWeight value is 0x0190, which is the same as FW_NORMAL. The FW_constants are described in the documentation for the lfWeight member of the LOGFONT structure in the SDK documentation.

..B ekiM
Random Solutions  
 
programming4us programming4us