You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Win32中何时向主窗口的静态控件发送WM_SETFONT消息?

主窗口静态文本框设置字体的时机与疑问

我正在尝试为主窗口的静态文本框设置字体,已经明确核心步骤:

  • 使用CreateFont创建字体对象
  • 向控件发送WM_SETFONT消息,传入字体句柄
  • 不再需要字体对象时调用DeleteObject销毁

但所有资料都未明确说明发送WM_SETFONT消息的时机——是否必须在窗口过程WinProc中处理特定消息时发送?我未找到相关的清晰解释。

有一篇MSDN文档提到可在处理WM_INITDIALOG消息时设置字体,但我的场景并非对话框,而是带有静态子文本框的主窗口。

我尝试在进入消息循环前发送WM_SETFONT消息,意外发现这可行!但根据SendMessage的文档,它要等到窗口过程处理完消息才会返回,我对此感到疑惑:代码为何还能进入消息循环?

以下是我的代码:

#include <windows.h>
#include <windowsx.h>

HWND hMainWind;
HWND hStartButton;
HWND hStopButton;
HWND hStaticBox;
static HFONT hFont;
static LRESULT CALLBACK MainWinProc(HWND, UINT, WPARAM, LPARAM);

#define START_BUTTON 1001
#define STOP_BUTTON  1002

int WINAPI WinMain( HINSTANCE hMainInstance, HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine, int nShowCmd )
{

  MSG Msg = {0};
  WNDCLASSEX MainWinClass;

  MainWinClass.cbSize         = sizeof(WNDCLASSEX);
  MainWinClass.style          = CS_VREDRAW|CS_HREDRAW;
  MainWinClass.lpfnWndProc    = MainWinProc;
  MainWinClass.cbClsExtra     = 0;
  MainWinClass.cbWndExtra     = 0;
  MainWinClass.hInstance      = hMainInstance;

  MainWinClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  MainWinClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  MainWinClass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);

  MainWinClass.hbrBackground  = (HBRUSH)COLOR_BACKGROUND;
  MainWinClass.lpszMenuName   = NULL; /*No menu*/
  MainWinClass.lpszClassName  = "TwoButtons"; /* class name to register*/

  if( !RegisterClassEx( &MainWinClass ) )
  {
      MessageBox( NULL, "Window Failed to Register!", "ERROR",
                  MB_ICONEXCLAMATION | MB_OK);
    return 0;
  }

  hMainWind = CreateWindowEx
  (
    WS_EX_LEFT,
    MainWinClass.lpszClassName,
    "TwoButtons",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,  /*Default x pos*/
    CW_USEDEFAULT,  /*Default y pos*/
    640,  /*Width*/
    480,  /*Height*/
    HWND_DESKTOP,
    NULL,
    hMainInstance,
    NULL
  );

  hStartButton = CreateWindow
  (
    "BUTTON",
    "START",
    WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
    270,  /*x pos*/
    200,  /*y pos*/
    100,
    50,
    hMainWind,
    (HMENU)START_BUTTON,
    hMainInstance,
    NULL
  );

  hStopButton = CreateWindow
  (
    "BUTTON",
    "STOP",
    WS_TABSTOP | WS_VISIBLE | WS_DISABLED | WS_CHILD | BS_PUSHBUTTON,
    270,
    300,
    100,
    50,
    hMainWind,
    (HMENU)STOP_BUTTON,
    hMainInstance,
    NULL
  );

  /*create a static text box*/
  hStaticBox = CreateWindow
  (
    "STATIC",                       /* lpClassName */
    "Box",                          /* lpWindowName */
    WS_BORDER|WS_CHILD|WS_VISIBLE,  /* dwStyle */
    270,                            /* x */
    100,                            /* y */
    100,                            /* nWidth */
    50,                             /* nHeight */
    hMainWind,                      /* hWndParent */
    NULL,                           /* hMenu */
    hMainInstance,                  /* hInstance */
    (LPVOID)1                       /* lpParam */
  );

  hFont = CreateFont
  (
    24,                   /* cHeight */
    0,                    /* cWidth use default*/
    0,                    /* cEscapement */
    0,                    /* cOrientation */
    FW_NORMAL,            /* cWeight */
    FALSE,                /* bItalic */
    FALSE,                /* bUnderline */
    FALSE,                /* bStrikeOut */
    DEFAULT_CHARSET,      /* iCharSet */
    OUT_DEFAULT_PRECIS,   /* iOutPrecision */
    CLIP_DEFAULT_PRECIS,  /* iClipPrecision */
    DEFAULT_QUALITY,      /* iQuality */
    DEFAULT_PITCH,        /* iPitchAndFamily */
    "Arial"               /* pszFacename */
  );

  /* Make the main window visible on the screen */
  ShowWindow(hMainWind, nShowCmd);
  UpdateWindow(hMainWind);

  /*Set up the font for the static text box*/
  SendMessage ( hStaticBox, WM_SETFONT, (WPARAM) hFont, TRUE );

  /* Run the message loop. It will run until GetMessage() returns 0 */
  while( GetMessage( &Msg, NULL, 0, 0 ) )
  {
    /* Translate virtual-key messages into character messages */
    TranslateMessage( &Msg );
    /* Send message to MainWinProc */
    DispatchMessage( &Msg );
  }

  /* The program return-value is 0 - The value that PostQuitMessage() gave */
  return Msg.wParam;
}

/* This function is called by DispatchMessage()  */
static LRESULT CALLBACK MainWinProc(  HWND hWind, UINT Message, WPARAM wParam,
                                      LPARAM lParam )
{
  switch(Message) /* handle the message */
  {
    case WM_DESTROY:
      DeleteObject(hFont);
      PostQuitMessage(0); /* sends a WM_QUIT to the message queue */
      break;

    case WM_COMMAND:
      if( HIWORD( wParam ) == BN_CLICKED )  /*A button was clicked*/
      {
        switch ( LOWORD(wParam) ) /*Which button?*/
        {
          case START_BUTTON:
            Button_Enable( hStartButton, FALSE );
            Button_Enable( hStopButton,  TRUE );
            SetWindowText( hStaticBox, "START" );
            break;

          case STOP_BUTTON:
            Button_Enable( hStartButton, TRUE );
            Button_Enable( hStopButton,  FALSE );
            SetWindowText( hStaticBox, "STOP" );
            break;
        }
      }
    default:  /* Pass on messages not handled here */
      return DefWindowProc (hWind, Message, wParam, lParam);
  }
  return 0;
}

内容的提问来源于stack exchange,提问作者AWM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 11:24:55