15,978
社区成员
发帖
与我相关
我的任务
分享
BOOL NDlg::SetDefaultFont(HWND hWnd, BOOL bChild)
{
HFONT hFont;
HWND hChildWnd;
LOGFONT LogFont;
SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LogFont), &LogFont, 0);
hFont = ::CreateFontIndirect(&LogFont);
SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
if(bChild == FALSE)
return TRUE;
for(hChildWnd = ::GetWindow(hWnd, GW_CHILD);
hChildWnd != NULL;
hChildWnd = ::GetWindow(hChildWnd, GW_HWNDNEXT))
{
SendMessage(hChildWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
}
return TRUE;
}
NONCLIENTMETRICS ncm = new NONCLIENTMETRICS();
bool isGood = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), ref ncm, 0);
if (isGood == true)
{
LOGFONT logFont2;
//logFont2=ncm.lfntCaptionFont);//CaptionFont 标题字体
//logFont2 =ncm.lfntSMCaptionFont;//CaptionFont_Small
//logFont2 = ncm.lfntMenuFont;//MenuFont 菜单字体
//logFont2 = ncm.lfntStatusFont;//StatusFont 状态栏字体
logFont2 = ncm.lfntMessageFont;//MessageFont 消息字体
CFont * f;
f = new CFont;
const LOGFONT* pFont = new LOGFONT(logFont2);
f->CreateFontIndirectW(pFont);
//::EnumChildWindows(m_hWnd, ::SetChildFont, (LPARAM)f);
}
#include <windows.h>
#define IDC_BUTTON 1
TCHAR szWindowClass[] = TEXT("FontTest");
TCHAR szTitle[] = TEXT("字体测试");
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static CHOOSEFONT cf;
static LOGFONT lf;
static HFONT hFont;
static HWND hButton;
switch (message)
{
case WM_CREATE:
hButton = CreateWindow("button", "我不是按钮", WS_VISIBLE|WS_CHILD,
150, 15, 150, 25, hWnd, (HMENU)IDC_BUTTON, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hWnd;
cf.hDC = NULL;
cf.lpLogFont = &lf;
cf.iPointSize = 0;
cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_EFFECTS | CF_SCREENFONTS;
cf.rgbColors = 0;
cf.lCustData = 0;
cf.lpfnHook = NULL;
cf.lpTemplateName = NULL;
cf.hInstance = NULL;
cf.lpszStyle = NULL;
cf.nFontType = 0;
cf.nSizeMin = 0;
cf.nSizeMax = 0;
if(ChooseFont(&cf))
{
hFont = CreateFontIndirect(&lf);
SendMessage(hButton, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(true, 0));
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd,&ps);
EndPaint(hWnd,&ps);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}