NO

Author Topic: How I use fixed length fonts in Win32 programs  (Read 2075 times)

colepc

  • Guest
How I use fixed length fonts in Win32 programs
« on: April 07, 2015, 03:16:11 pm »
I'd thought I'd share how I handle fixed length fonts in my Win32 programs. This procedure has prevented the default variable length font from raising its unwanted head. I like fixed length fonts as they are easier to align. Fixed length fonts are used on most if not all C editors.

I wanted to suggest some available Microsoft information about GetStockFont() and SelectFont() (used below) but I couldn't find any. They are defined in windowsx.h using GetStockObject() and SelectObject(). Now these two functions are found in the Microsoft information. Why I used GetStockFont and SelectFont I don't remember. When I substituted GetStockObject() for GetStockFont() and SelectObject() for SelectFont() in one of my programs, they worked just as well. So you can decide which ones you want to use.

The following I have in WinMain().
It is not really needed there but I keep it for reference and information
{
    HWND mhwnd=NULL;
    HDC hdc;     // handle to device context for font
    HFONT hfontFixed;   //used for changing the font
    HFONT hfontPrev;   //used for changing the font

   //the main window is registered and created here

    //load the font
    //not needed here but doesn't hurt either
    //keep here for information about the font used
    //GetStockFont() and SelectFont() are defined in windowsx.h
    //GetStockObject() can be substituted for GetStockFont()
    //SelectObject() can be substituted for SelectFont()
    //GetStockObject() and SelectObject() is found in Microsoft information
    //make sure to use the following 3 lines before TextOut() is used
    //or anytime information about the font is needed
    hdc = GetDC(mhwnd);
    hfontFixed=GetStockFont(SYSTEM_FIXED_FONT);
    hfontPrev=SelectFont(hdc,hfontFixed);
    //SYSTEM_FIXED_FONT (height/width is OK, 0 has 1/2 slash)
    //OEM_FIXED_FONT (height/width is smaller, 0 has full slash)
    //ANSI_FIXED_FONT (height/width is slightly smaller, 0 has no slash)
    //the above font names are in wingdi.h which is included by windows.h
    //use the followed 2 lines after the last use of TextOut()
    SelectObject(hdc,hfontPrev);
    ReleaseDC(mhwnd, hdc);

The following I have in MainWndProc( HWND mhwnd, UINT uMsg,
    WPARAM wParam, LPARAM lParam)
{
    HDC hdc;     // handle to device context for font
    TEXTMETRIC tm;   // structure for text metrics
    HFONT hfontFixed;   //used for changing the font
    HFONT hfontPrev;   //used for changing the font
    switch (uMsg)
    {
        case WM_CREATE:
          // Get the metrics of the current font.
          hdc = GetDC(mhwnd);
          hfontFixed=GetStockFont(SYSTEM_FIXED_FONT);
          hfontPrev=SelectFont(hdc,hfontFixed);
          GetTextMetrics(hdc, &tm);
          ReleaseDC(mhwnd, hdc);
          SelectObject(hdc,hfontPrev);
          ReleaseDC(mhwnd, hdc);
          // Save the average character width and height.
          cxChar=tm.tmAveCharWidth;
          cyChar=tm.tmHeight;
          return 0; //from MainWndProc() at WM_CREATE

      //other cases are here but not shown
    } //end of switch (uMsg)
} //end of MainWndProc()

I have TextOut() in my WM_PAINT handler (a function call) and one other function call.
I have the following in those function calls.

FunctionCallWithTextOut(HWND mhwnd)
{
    PAINTSTRUCT ps;  //only used in my WM_PAINT handler
    HDC hdc; // handle to device context for font
    HFONT hfontFixed; //used for changing the font
    HFONT hfontPrev; //used for changing the font

    // this is where I have "BeginPaint(mhwnd, &ps);" for the WM_PAINT handler

    //set desired font
    hdc = GetDC(mhwnd); //don't forget to release this
    hfontFixed=GetStockFont(SYSTEM_FIXED_FONT);
    hfontPrev=SelectFont(hdc,hfontFixed);

    //do stuff including TextOut() statements

    //done with font
    SelectObject(hdc,hfontPrev);
    ReleaseDC(mhwnd, hdc);

    //this is where I have "EndPaint(mhwnd, &ps);" for WM_PAINT handler
} //end of FunctionCallWithTextOut()

Offline DMac

  • Member
  • *
  • Posts: 255
Re: How I use fixed length fonts in Win32 programs
« Reply #1 on: April 07, 2015, 05:13:56 pm »
Hello colepc,

It it preferable when posting code snippets to a forum to place them within code tags like so:
Code: [Select]
void MyCodeSnippet(void){
//Some code
}
The Code tags can be inserted into the message by selecting the button with the "#" in the message editor.

There are several reasons for this:
1. The first is that it makes it easier to differentiate the snippet from the other text of the post.
2. It preserves the formatting of the snippet.
3. And my favorite since I'm lazy - it provides the convenience of the code block select link so that the snippet can be selected with one click and copy pasted into an editor.
No one cares how much you know,
until they know how much you care.

colepc

  • Guest
Re: How I use fixed length fonts in Win32 programs
« Reply #2 on: April 11, 2015, 02:19:36 pm »
While researching "Code Tags" on this forum I found, "do NOT use code tags, use quote tags." Turns out its not called "Code Tags", or even "Quote Tags". It's called "Insert Code". I'll try to use the "Insert Code" button next time.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 761
Re: How I use fixed length fonts in Win32 programs
« Reply #3 on: April 12, 2015, 07:44:44 am »
While researching "Code Tags" on this forum I found, "do NOT use code tags, use quote tags." Turns out its not called "Code Tags", or even "Quote Tags". It's called "Insert Code". I'll try to use the "Insert Code" button next time.
The [Insert Code] button is a shortcut in order to put code tags ("[ code][ /code]", spaces added to avoid the code tags to be applied) around a piece of code...

Ralf