Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WM_SETFONT not working

186 views
Skip to first unread message

David Wallace

unread,
Jun 6, 1997, 3:00:00 AM6/6/97
to

I can't get WM_SETFONT & WM_GETFONT to work on a dialog box I have
loaded with CreateDialogIndirect. If I specify a font in the resource
file, I can
use WM_GETFONT to get that font. However, I cannot change that font with
a WM_SETFONT. Calling WM_SETFONT and then a WM_GETFONT will
always retrieve the font specified in the rc file. If I do not have a font
specified in the rc file, I always get 0x0 returned with a call to
WM_GETFONT.

2 questions:
1) How can I set a default font on a dialogbox?
2) Can I set a default font on a dialog box without using a font
definition in the
resource file?

Thanks in advance for any help or suggestions.

David Wallace

I've included my sample code below:

// simpled.cxx ==========================================================

#include <windows.h>
#include "resource.h"
#include "simpled.h"
#include "resrc1.h"

typedef struct
{
WORD wDlgVer; // use 1
WORD wSignature; // use 0xFFFF
DWORD dwHelpID; // Dialog's context help ID
DWORD dwExStyle; // Extended style
DWORD dwStyle; // Style
WORD cDlgItems; // Number of controls in dialog
short x; // Initial position, horizontal
short y; // Initial position, vertical
short cx; // Width
short cy; // Height
} DLGTEMPLATEEX;

void fonttest(HWND hwndDlg)
{
LOGFONT lf;

memset (&lf, 0, sizeof(LOGFONT));
lf.lfHeight = 0xffffffeb;
lf.lfWeight = 0x190 ;
lf.lfItalic = 1 ;
lf.lfUnderline = 0;
lf.lfStrikeOut = 0;
lf.lfCharSet = 1;

strcpy(lf.lfFaceName, "Tahoma");

HFONT hFont = CreateFontIndirect (&lf);
SendMessage (hwndDlg, WM_SETFONT,(WPARAM)hFont, 0);
HFONT myFont = (HFONT)SendMessage( hwndDlg, WM_GETFONT, 0, 0 ) ;
if ( myFont )
{
LOGFONT lFont ;
if (GetObject(myFont, sizeof(LOGFONT), (LPSTR)&lFont)!= NULL)
{
// always get "MS Sans Serif" retrieved here
OutputDebugString( "hfont here!\n" ) ;
}
}
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{

WNDCLASS wc;

wc.lpszClassName = "FunkyFont";
wc.hInstance = hInstance;
wc.lpfnWndProc = (WNDPROC)&dialogProc;
wc.hCursor = NULL;
wc.hIcon = (HICON)0;
wc.lpszMenuName = 0;
wc.hbrBackground = (HBRUSH)NULL;
wc.style = 0;
wc.cbClsExtra = 0;
wc.cbWndExtra = DLGWINDOWEXTRA;
if (!RegisterClass(&wc))
return FALSE;


// get pointer to dialog template
HINSTANCE hinst = hInstance ;
HRSRC hrsrc = FindResource(hinst, MAKEINTRESOURCE(IDD_DIALOG),
RT_DIALOG);
HGLOBAL hglobal = LoadResource(hinst,hrsrc);
DLGTEMPLATEEX *dlgTemplate = (DLGTEMPLATEEX *)LockResource(hglobal);


DWORD resSize = SizeofResource(hinst, hrsrc);
int words = (int) resSize / sizeof(WORD);

DLGTEMPLATEEX *dlgTemplateCopy = (DLGTEMPLATEEX *) new WORD[words];

memcpy(dlgTemplateCopy, dlgTemplate, resSize);

UnlockResource(hglobal);
FreeResource(hglobal);

HWND hwnd =
CreateDialogIndirect(hInstance,(LPDLGTEMPLATE)dlgTemplateCopy,NULL,0);
ShowWindow(hwnd,SW_SHOW);
fonttest(hwnd) ;

MSG msg;
while ( GetMessage(&msg, NULL, 0, 0) )
if ( !IsDialogMessage(hwnd, &msg) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;

}


BOOL CALLBACK dialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM
lParam)
{
switch (uMsg)
{
case WM_CLOSE:
PostQuitMessage(0);
return TRUE;
case WM_COMMAND :
fonttest(hwndDlg) ;
return TRUE ;
case WM_INITDIALOG :
return DefDlgProc( hwndDlg, uMsg, wParam, lParam ) ;
break ;
case WM_CREATE :
return DefDlgProc( hwndDlg, uMsg, wParam, lParam ) ;
break ;
case WM_SETFONT :
return DefDlgProc( hwndDlg, uMsg, wParam, lParam ) ;
case WM_GETFONT :
return DefDlgProc( hwndDlg, uMsg, wParam, lParam ) ;

default:
return DefDlgProc( hwndDlg, uMsg, wParam, lParam ) ;
}
}
// end of simpled.cxx

// simpled.rc =============================================================
// Dialog definition only

IDD_DIALOG DIALOG DISCARDABLE 0, 0, 100, 100
STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Simpled"
CLASS "FunkyFont"
FONT 8, "MS Sans Serif"
BEGIN
PUSHBUTTON "Button1",IDC_BUTTON1,25,41,50,14
END

// end of simpled.rc

Cecil A. Galbraith

unread,
Jun 8, 1997, 3:00:00 AM6/8/97
to David Wallace

Before you can set a font, you must first CreateFont(..) a font... There
is an example article on how to do this at the URL below.

Cecil
--
Cecil Galbraith
CustomSoft mail to cgal...@concentric.net

Free programmer's utilities and MFC tips at
http://www.concentric.net/~cgalbrai

David Lowndes

unread,
Jun 10, 1997, 3:00:00 AM6/10/97
to

>Thanks for the reply.
>
>I've tried that to no avail. Using DS_SETFONT allows me to see the font
>specified in the resource but I still can't use WM_SETFONT to reset it.

It was only a shot in the dark.

Are you sure that the font you're creating is working? No NULL handles
at all? What's the return value from sending the WM_SETFONT message?

Dave
----
Address is altered to discourage junk mail.
Remove ".---" for the real address.

Andrew Matalus

unread,
Jun 13, 1997, 3:00:00 AM6/13/97
to

Andrew Matalus wrote:
>
> // We will want a local one of these (for scaling)
> LOGFONT lf = m_LogFont;
>

I realized I didn't specify where m_LogFont came from, it is a
LOGFONT struct and is acquired as follows :

// Get the LOGFONT structure for the original font
GetObject( m_DlgFont, sizeof( LOGFONT ), (LPSTR) &m_LogFont );
^^^^^^^^^

m_DlgFont is a HFONT as show in the previous code snippet.

Sorry for cluttering the thread.

-Drew

--

+--------------------------------+
| Andrew Matalus |
| Advanced Virtual Systems, Inc. |
| amat...@iren.com |
+--------------------------------+

0 new messages