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
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
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.
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 |
+--------------------------------+