I am creating a GUI application using C and Win32 api. I would like to know how we can change the default font of the Main window to thaoma. I am comming from .NET background. In .NET if we change the font of parent control then automatically the child controls inherits that font.... Is there away similar to it or do we need to manually set the font of each control.....
Consider the following code...
#include <windows.h>
#define ID_EDIT 1
#define ID_BUTTON 2
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit;
static HWND hwndButton;
static int len;
static TCHAR text[30];
switch(msg)
{
case WM_CREATE:
hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
50, 50, 150, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
hwndButton = CreateWindow(
TEXT("button"), TEXT("Set Title"),
WS_VISIBLE | WS_CHILD,
50, 100, 80, 25,
hwnd, (HMENU) ID_BUTTON, NULL, NULL);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT( "Edit Control" );
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
CreateWindow( wc.lpszClassName, TEXT("Edit control"),
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
220, 220, 280, 200, 0, 0, hInstance, 0);
while( GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
How can i change the font of button, textbox in the above program..
Please help me here.......and let me know the general process followed while coding in win32 api....
THanks in advance..
You can set the font of a window by sending it the WM_SETFONT
message:
HWND myButton = CreateWindowEx(/* ... */);
HFONT myFont = /* ... load font from somewhere ... */
/* Change the button font. */
SendMessage(myButton, WM_SETFONT, WPARAM(myFont), TRUE);
This approach gives you a control-by-control control over which fonts you're using, but you only need to do it once per window.
Probably you try create a dialog based GUI application, which looks like a little like form based application in .NET. You can define a font for the form and all its child controls inherits that font. A close situation exist also in every dialog.
How to crate a dialog with respect of Win32 API you can read here: http://msdn.microsoft.com/en-us/library/ms644996%28VS.85%29.aspx. The main distinguish in programming of dialog comparing with creating of forms is that you should use resource editor (inside of Visual Studio or Windows SDK for example) to design of dialogs. The results will be saved in RC file (not yet compiled resource file). The results will looks like following:
IDD_ABOUTBOX DIALOGEX 0, 0, 205, 98
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
CAPTION "About"
FONT 8, "MS Shell Dlg 2", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,77,77,50,14
LTEXT "Trust to User (T2U) v1.0\n\n(c) Copyright 2003, 2004",
IDC_STATIC,72,32,120,32
ICON IDR_MAINFRAME,IDC_STATIC,25,27,20,20
GROUPBOX "",IDC_STATIC,7,3,191,69
LTEXT "OK soft GmbH",IDC_OK_SOFT_GMBH,72,16,120,8
END
you can have more as one resource in different languages saved in the same RC file:
IDD_ABOUTBOX DIALOGEX 0, 0, 205, 98
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
CAPTION "バージョンの情報"
FONT 8, "MS Shell Dlg 2", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,77,77,50,14
LTEXT "Trust to User (T2U) v1.0\n\n(c) Copyright 2003, 2004",
IDC_STATIC,72,32,120,32
ICON IDR_MAINFRAME,IDC_STATIC,25,27,20,20
GROUPBOX "",IDC_STATIC,7,3,191,69
LTEXT "OK soft GmbH",IDC_OK_SOFT_GMBH,72,16,120,8
END
You receive the best results in case of internationalization if you will use "MS Shell Dlg 2" or "MS Shell Dlg" instead of the font "Tahoma", but direct usage of "Tahoma" is also possible:
FONT 8, "Tahoma"
You should use "MS Shell Dlg 2" together with the flag DS_SHELLFONT
or a combination of DS_FIXEDSYS
and DS_SETFONT
(see http://blogs.msdn.com/oldnewthing/archive/2005/02/07/368423.aspx) which follows to usage of "Tahoma" on the most computers (verify "MS Shell Dlg 2" value under the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes
). Read http://msdn.microsoft.com/en-us/library/dd374112%28v=VS.85%29.aspx or http://support.microsoft.com/kb/282187/en about this.
By the way you can open a resource saved in an EXE, DLL or EXE.MUI/DLL.MUI with respect of Visual Studio. You should just open the file and choose "Open With" and choose "Resoure Editor".
On Window 7 you can search for C:\Windows\winsxs\x86_microsoft-windows-notepad
and open like file like C:\Windows\winsxs\x86_microsoft-windows-notepad.resources_31bf3856ad364e35_6.1.7600.16385_en-us_1dbc2e35304db501\notepad.exe.mui
. Then you can save the file as notepad.rc
, then open the file notepad.rc
in en text editor and you will find following fragments
15 DIALOGEX 30, 17, 300, 22
STYLE DS_SETFONT | DS_3DLOOK | DS_CONTROL | WS_CHILD | WS_CLIPSIBLINGS
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
LTEXT "&Encoding:",259,68,1,40,40,NOT WS_GROUP
COMBOBOX 257,130,0,164,100,CBS_DROPDOWNLIST | WS_VSCROLL | WS_GROUP | WS_TABSTOP
END
51200 DIALOGEX 0, 0, 324, 140
STYLE DS_SETFONT | DS_3DLOOK | WS_CHILD | WS_CAPTION
CAPTION "Software Licensing"
FONT 9, "Segoe UI", 0, 0, 0x0
BEGIN
LTEXT "To use this feature without interruption, this computer needs to be running genuine Windows.",-1,0,10,250,20
LTEXT "With genuine Windows you have access to all Windows updates and can be confident that your Windows software has the latest security and reliability enhancements from Microsoft.",-1,0,35,250,30
CONTROL 51209,-1,"Static",SS_BITMAP,260,10,100,55
CONTROL "&Resolve online now",51201,"Button",BS_COMMANDLINK | BS_LEFT | WS_TABSTOP,0,75,250,24
CONTROL "<a>Read the privacy statement online</a>",51202,"SysLink",WS_TABSTOP,0,128,120,10
END
UPDATED: Try to modify your WndProc
function to the following:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hwndEdit;
static HWND hwndButton;
static int len;
static TCHAR text[30];
HFONT hFont;
LOGFONT lf;
switch(msg)
{
case WM_CREATE:
hwndEdit = CreateWindow(TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
50, 50, 150, 20, hwnd, (HMENU) ID_EDIT,
NULL, NULL);
GetObject (GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
hFont = CreateFont (lf.lfHeight, lf.lfWidth,
lf.lfEscapement, lf.lfOrientation, lf.lfWeight,
lf.lfItalic, lf.lfUnderline, lf.lfStrikeOut, lf.lfCharSet,
lf.lfOutPrecision, lf.lfClipPrecision, lf.lfQuality,
lf.lfPitchAndFamily, lf.lfFaceName);
SendMessage (hwndEdit, WM_SETFONT, (WPARAM)hFont, TRUE);
hwndButton = CreateWindow(
TEXT("button"), TEXT("Set Title"),
WS_VISIBLE | WS_CHILD,
50, 100, 80, 25,
hwnd, (HMENU) ID_BUTTON, NULL, NULL);
SendMessage (hwndButton, WM_SETFONT, (WPARAM)hFont, TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With