This article shows how to
change the font
for all child
windows using
only one line with a
callback function.
The idea is to create a
callback function that is
passed to Win32 API function
::EnumChildWindows().
Somewhere in the
application, global CFont
object is created. Pointer
to this object is passed as
a third argument in
EnumChildWindows().
This argument is passed to a
callback function together
with a handle to a child
window. Using these 2
arguments, the function
changes the font
for the child
window.
Using this technique, it is
very easy to:
-
Change the font for main
frame window, its child
views and a status bar
from
CMainFrame::OnCreate().
-
Change the font for all
dialog controls from
OnInitDialog().
The Callback function looks
like this:
Collapse
Copy Code
BOOL __stdcall SetChildFont(HWND hwnd, LPARAM lparam)
{
CFont *pFont = (CFont*)lparam;
CWnd *pWnd = CWnd::FromHandle(hwnd);
pWnd->SetFont(pFont);
return TRUE;
}
This callback function is
used from OnCreate() or
OnInitDialog() in the
following way:
Collapse
Copy Code
...
EnumChildWindows(m_hWnd, ::SetChildFont, &g_Font);
...
Load cursor from resource:
HCURSOR LoadAniCursor(UINT nID)
{
HINSTANCE hInst=AfxGetInstanceHandle();
HRSRC hRes=FindResource(hInst,MAKEINTRESOURCE(nID),"ANICURSORS");
DWORD dwSize=SizeofResource(hInst, hRes);
HGLOBAL hGlob=LoadResource(hInst, hRes);
LPBYTE pBytes=(LPBYTE)LockResource(hGlob);
return (HCURSOR)CreateIconFromResource(pBytes,dwSize,FALSE,0x00030000);
}