c - HFONT I am getting from my dialog box is not the one I sent -
i playing raw windows api here. trying pass font dialog during wm_initdialog message , keep using font whenever new child window created inside dialog. not want keep passing hfont created, want retrieve dialog window instead, whenever need it.
so when enter wm_initdialog handler, create font , send dialog using sendmessage(wm_setfont). then, whenever wanted create new child window, thought needed retrieve dialog font, using sendmessage(wm_getfont). handle receive dialog not 1 created, no surprise when use new handle set new control font wrong font.
i destroying font handle when application terminates. bet invalid handle not problem otherwise, in example below, control explicit pass font handle not displaying correct one.
any ideas?
#include <windows.h> #include <commctrl.h> static int_ptr callback dialogproc( _in_ hwnd hwnddlg, _in_ uint umsg, _in_ wparam wparam, _in_ lparam lparam) { static hfont hfont = null; switch(umsg) { case wm_close: ::enddialog(hwnddlg, 0); ::deleteobject(hfont); return true; case wm_initdialog: { // create our font hfont = ::createfontw( 36, 0, 0, 0, fw_normal, 0, 0, 0, default_charset, out_default_precis, clip_default_precis, cleartype_quality, variable_pitch, l"tahoma"); // set font dialog font ::sendmessagew(hwnddlg, wm_setfont, (wparam)hfont, true); // retrieve dialog font hfont hfontdialog = (hfont)::sendmessagew(hwnddlg, wm_getfont, 0, 0); // create 2 child windows hwnd hwndstatic1 = ::createwindowexw(0, wc_static, l"hfont", ws_visible | ws_child, 0, 0, 100, 30, hwnddlg, null, 0, 0); hwnd hwndstatic2 = ::createwindowexw(0, wc_static, l"hfontdialog", ws_visible | ws_child, 0, 50, 100, 20, hwnddlg, null, 0, 0); // set fonts, using 1 created first 1 , // 1 dialog second 1 ::sendmessagew(hwndstatic1, wm_setfont, (wparam)hfont, true); ::sendmessagew(hwndstatic2, wm_setfont, (wparam)hfontdialog, true); return false; } } return false; } #include <pshpack2.h> struct dialogtemplate { dlgtemplate base; word menu; word class; wchar title[1]; }; #include <poppack.h> int winapi wwinmain( __in hinstance hinstance, __in_opt hinstance hprevinstance, __in lpwstr lpcmdline, __in int nshowcmd) { dialogtemplate dt = {0}; dt.base.style = ds_center | ds_modalframe | ws_popup | ws_caption | ws_sysmenu; dt.base.cx = 200; dt.base.cy = 200; return ::dialogboxindirectparamw( ::getmodulehandle(null), (dlgtemplate*)&dt, // holy hack, batman! hope ok... null, dialogproc, (lparam)0); }
dialogs don't respond wm_setfont
. dialog has font defined in template, , if ds_setfont
style set propogate child controls on creation. font wm_getfont
1 created based on template.
if want change control fonts after dialog has been created need send them wm_setfont
messages individually.
my theory why case: dialog layout based on font size ("dialog units" fraction of font height , average font width). therefore, dialog needs know font upon creation, in order resize , layout controls. changing font afterwards, dialog's point of view, potentially require resize/re-layout , not functionality has been implemented in dialog manager.
Comments
Post a Comment