Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Font size on components in a dialog box.

I would like to change the font size on the text in edit boxes or/and static text boxes. The only change i have been able to make is through properties of the dialog box where i set the font for all of the items of the dialog box, although i cannot set bold for example, but i would like to set the fonts for the text boxes differently than the whole dialog box. I see no such similar capability in the properties of the text box. Is there a way to do this, either via program code or through the properties of the text box ? Thanks.

Comments

  • pingpongpingpong Posts: 937Member
    via code, you need to send the hFont to the control windows via WM_SETFONT. Check it out in MSDN.

    If you want all text boxes to be of a certain font, all labels of another, etc, then you might write some code like this:

    [code]
    INT WINAPI DialogProc(HWND hDlg, ...
    {
    switch(message)
    {
    case WM_INITDIALOG:
    {
    // enumerate all children
    HWND hControl = GetWindow(hDlg, GW_CHILD);
    while(hControl != null)
    {
    // get the class name
    TCHAR className[800];
    GetClassName(hControl, className, sizeof(className));

    // set the font based on class
    if(lstrcmpi(className, "edit") == 0)
    SendMessage(hControl, WM_SETFONT, hEditBoxFont, 0);
    else if(lstrcmpi(className, "static") == 0)
    SendMessage(hControl, WM_SETFONT, hStaticFont, 0);

    // get next control
    hControl = GetWindow(hControl, GWL_HWNDNEXT);
    }
    [/code]



    : I would like to change the font size on the text in edit boxes or/and static text boxes. The only change i have been able to make is through properties of the dialog box where i set the font for all of the items of the dialog box, although i cannot set bold for example, but i would like to set the fonts for the text boxes differently than the whole dialog box. I see no such similar capability in the properties of the text box. Is there a way to do this, either via program code or through the properties of the text box ? Thanks.
    :

  • jtcoelhojtcoelho Posts: 44Member
    Thanks for the information. But if i just want to create a font for a specific control such as a static text box, and no other, how do i get the handle for the font ? Do i first get a device handle GetDC then a handle to the font? I have looked at some of the sample code in Petzold and it seems rather intricate - and since i would like to apply that to one single control - i would like an easier way of setting the font for that control. Any examples ?
    : via code, you need to send the hFont to the control windows via WM_SETFONT. Check it out in MSDN.
    :
    : If you want all text boxes to be of a certain font, all labels of another, etc, then you might write some code like this:
    :
    : [code]
    : INT WINAPI DialogProc(HWND hDlg, ...
    : {
    : switch(message)
    : {
    : case WM_INITDIALOG:
    : {
    : // enumerate all children
    : HWND hControl = GetWindow(hDlg, GW_CHILD);
    : while(hControl != null)
    : {
    : // get the class name
    : TCHAR className[800];
    : GetClassName(hControl, className, sizeof(className));
    :
    : // set the font based on class
    : if(lstrcmpi(className, "edit") == 0)
    : SendMessage(hControl, WM_SETFONT, hEditBoxFont, 0);
    : else if(lstrcmpi(className, "static") == 0)
    : SendMessage(hControl, WM_SETFONT, hStaticFont, 0);
    :
    : // get next control
    : hControl = GetWindow(hControl, GWL_HWNDNEXT);
    : }
    : [/code]
    :
    :
    :
    : : I would like to change the font size on the text in edit boxes or/and static text boxes. The only change i have been able to make is through properties of the dialog box where i set the font for all of the items of the dialog box, although i cannot set bold for example, but i would like to set the fonts for the text boxes differently than the whole dialog box. I see no such similar capability in the properties of the text box. Is there a way to do this, either via program code or through the properties of the text box ? Thanks.
    : :
    :
    :

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [code]
    LOGFONT lf={0};
    HFONT hFont;
    HDC hScreenDC = GetDC (NULL);
    int iPixelsPerInch = GetDeviceCaps (hScreenDC, LOGPIXELSY);
    int iPointSize=9; // Font will be -> Tahoma,9

    strcpy (lf.lfFaceName, "Tahoma");
    lf.lfWeight = FW_NORMAL;
    lf.lfHeight = -MulDiv (iPointSize, iPixelsPerInch, 72);
    ReleaseDC (NULL, hScreenDC);
    hFont = CreateFontIndirect (&lf); // <- here is your font handle.
    [/code]
  • jtcoelhojtcoelho Posts: 44Member
    Thanks much.
    : [code]
    : LOGFONT lf={0};
    : HFONT hFont;
    : HDC hScreenDC = GetDC (NULL);
    : int iPixelsPerInch = GetDeviceCaps (hScreenDC, LOGPIXELSY);
    : int iPointSize=9; // Font will be -> Tahoma,9
    :
    : strcpy (lf.lfFaceName, "Tahoma");
    : lf.lfWeight = FW_NORMAL;
    : lf.lfHeight = -MulDiv (iPointSize, iPixelsPerInch, 72);
    : ReleaseDC (NULL, hScreenDC);
    : hFont = CreateFontIndirect (&lf); // <- here is your font handle.
    : [/code]
    :

  • jtcoelhojtcoelho Posts: 44Member
    Thanks again, i used the code and works beautifully. I do have a question, why do you use the MulDiv function to get the Height. Thanks again.
    : [code]
    : LOGFONT lf={0};
    : HFONT hFont;
    : HDC hScreenDC = GetDC (NULL);
    : int iPixelsPerInch = GetDeviceCaps (hScreenDC, LOGPIXELSY);
    : int iPointSize=9; // Font will be -> Tahoma,9
    :
    : strcpy (lf.lfFaceName, "Tahoma");
    : lf.lfWeight = FW_NORMAL;
    : lf.lfHeight = -MulDiv (iPointSize, iPixelsPerInch, 72);
    : ReleaseDC (NULL, hScreenDC);
    : hFont = CreateFontIndirect (&lf); // <- here is your font handle.
    : [/code]
    :

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : Thanks again, i used the code and works beautifully. I do have a question, why do you use the MulDiv function to get the Height. Thanks again.
    : : [code]
    : : LOGFONT lf={0};
    : : HFONT hFont;
    : : HDC hScreenDC = GetDC (NULL);
    : : int iPixelsPerInch = GetDeviceCaps (hScreenDC, LOGPIXELSY);
    : : int iPointSize=9; // Font will be -> Tahoma,9
    : :
    : : strcpy (lf.lfFaceName, "Tahoma");
    : : lf.lfWeight = FW_NORMAL;
    : : lf.lfHeight = -MulDiv (iPointSize, iPixelsPerInch, 72);
    : : ReleaseDC (NULL, hScreenDC);
    : : hFont = CreateFontIndirect (&lf); // <- here is your font handle.
    : : [/code]
    : :
    :
    :
    [blue]It is done, because the point (Tahoma,9pt) is 1/72 of an inch and with this function we can find how many pixels it will be on our HDC - whatever it is - it can be printer DC...

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/fontext_1wmq.asp[/blue]
Sign In or Register to comment.