簡體   English   中英

更改Win32 Windows項目中的默認窗口字體

[英]Change the default window font in a Win32 Windows project

我正在使用C和Win32 api創建一個GUI應用程序。 我想知道如何將主窗口的默認字體更改為thaoma。 我是從.NET背景出來的。 在.NET中如果我們更改父控件的字體然后自動子控件繼承該字體....是否有類似它或我們需要手動設置每個控件的字體.....

考慮以下代碼......

#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;
}

如何在上面的程序中更改按鈕,文本框的字體..

請幫助我.......並讓我知道在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 Shell Dlg 2”與標志DS_SHELLFONTDS_FIXEDSYSDS_SETFONT的組合(請參閱http://blogs.msdn.com/oldnewthing/archive/2005/02/07/368423.aspx )一起使用。在大多數計算機上使用“Tahoma”(在注冊表項HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes下驗證“MS Shell Dlg 2”值)。 請閱讀http://msdn.microsoft.com/en-us/library/dd374112%28v=VS.85%29.aspxhttp://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         "<a>Read the privacy statement online</a>",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);
}

您可以通過發送WM_SETFONT消息來設置窗口的字體:

HWND myButton = CreateWindowEx(/* ... */);
HFONT myFont  = /* ... load font from somewhere ... */

/* Change the button font. */
SendMessage(myButton, WM_SETFONT, WPARAM(myFont), TRUE);

這種方法為您提供了一個控制控件,可以控制您正在使用的字體,但每個窗口只需要執行一次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM