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()