我正在使用C和Win32 api创建一个GUI应用程序.我想知道如何将主窗口的默认字体更改为thaoma.我是从.NET背景出来的.在.NET中,如果我们更改父控件的字体,那么子控件会自动继承该字体....是否有相似之处或我们需要手动设置每个控件的字体.....
考虑以下代码......
#include#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; }
如何在上面的程序中更改按钮,文本框的字体..
请帮助我.......并让我知道在win32 api中编码的一般过程....
提前致谢..
可能你尝试创建一个基于对话框的GUI应用程序,它看起来有点像.NET中的基于表单的应用程序.您可以为表单定义字体,并且其所有子控件都继承该字体.每个对话中也存在密切的情况.
如何创建一个关于Win32 API的对话框,你可以在这里阅读:http://msdn.microsoft.com/en-us/library/ms644996%28VS.85%29.aspx.与形式创建相比,对话编程的主要区别在于您应该使用资源编辑器(例如Visual Studio或Windows SDK内部)来设计对话框.结果将保存在RC文件中(尚未编译的资源文件).结果如下:
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
您可以将更多作为一种资源保存在同一RC文件中的不同语言中:
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
如果你使用"MS Shell Dlg 2"或"MS Shell Dlg"代替字体"Tahoma",你会收到国际化的最佳结果,但也可以直接使用"Tahoma":
FONT 8, "Tahoma"
您应与标志一起使用"MS壳牌DLG 2" DS_SHELLFONT
的组合,或DS_FIXEDSYS
与DS_SETFONT
(见http://blogs.msdn.com/oldnewthing/archive/2005/02/07/368423.aspx)下面来的"使用Tahoma"在大多数计算机上(在注册表项下验证"MS Shell Dlg 2"值HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontSubstitutes
).请阅读http://msdn.microsoft.com/en-us/library/dd374112%28v=VS.85%29.aspx或http://support.microsoft.com/kb/282187/en.
顺便说一下,您可以打开与Visual Studio相关的EXE,DLL或EXE.MUI/DLL.MUI中保存的资源.您应该只需打开文件并选择"打开方式",然后选择"资源编辑器".在Window 7上,您可以搜索C:\Windows\winsxs\x86_microsoft-windows-notepad
并打开像文件一样的文件C:\Windows\winsxs\x86_microsoft-windows-notepad.resources_31bf3856ad364e35_6.1.7600.16385_en-us_1dbc2e35304db501\notepad.exe.mui
.然后您可以将文件另存为notepad.rc
,然后notepad.rc
在文本编辑器中打开文件,您将找到以下片段
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 "Read the privacy statement online",51202,"SysLink",WS_TABSTOP,0,128,120,10 END
更新:尝试将您的WndProc
功能修改为以下内容:
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); }