Change default window font in win32 windows project

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 start with a .NET background. In .NET, if we change the font of the parent control, then the children automatically inherit this font .... Do you have a similar option 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 the button, the text box in the above program.

Please help me here ....... and let me know the general process, followed by encoding in win32 api ....

Thanks in advance..

+4
source share
2 answers

You can set the window font by sending 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 control over the control of the fonts you use, but you only need to do this once for each window.

+2
source

You will probably try to create a interactive graphical application that looks like a formal application in .NET. You can define a font for a form, and all its children inherit this font. A similar situation exists in every dialog box.

How to create a Win32 API dialog box, which you can read here: http://msdn.microsoft.com/en-us/library/ms644996%28VS.85%29.aspx . The main difference in dialog programming compared to creating forms is that you must use a resource editor (for example, inside Visual Studio or the Windows SDK) to develop dialogs. The results will be saved in an RC file (not yet compiled resource file). The results will look like this:

 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 than one resource in different languages โ€‹โ€‹stored 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 will get the best results in case of internationalization if you use "MS Shell Dlg 2" or "MS Shell Dlg" instead of the font "Tahoma", but direct use of "Tahoma" is also possible:

 FONT 8, "Tahoma" 

You should use "MS Shell Dlg 2" with the DS_SHELLFONT flag or a combination of DS_FIXEDSYS and DS_SETFONT (see http://blogs.msdn.com/oldnewthing/archive/2005/02/07/368423.aspx ), which follows the use "Tahoma" on most computers (check the value of "MS Shell Dlg 2" in 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 stored in EXE, DLL or EXE.MUI / DLL.MUI in relation to Visual Studio. You just have to open the file and select "Open with" and select "Resoure Editor". In window 7, you can find C:\Windows\winsxs\x86_microsoft-windows-notepad and open a file such as 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 , and then open the notepad.rc file in a text editor, and you will find the following snippets

 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 changing the 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); } 
+5
source

Source: https://habr.com/ru/post/1310563/


All Articles