gpt4 book ai didi

c++ - 获取系统字体

转载 作者:行者123 更新时间:2023-11-28 05:43:25 28 4
gpt4 key购买 nike

想知道有没有办法获取红框内的如下windows字体的字体。

enter image description here

有没有办法通过 Win32 API 函数将此字体作为 HFONT 接收?

提前致谢!

最佳答案

这是通过 uxtheme.dll,AeroWizard 类,HeaderArea 部分完成的。状态 0 或状态 1 (NoMargins) 都可以工作。这是一个示例,使用 DrawThemeText() 和手动。 (请注意,作为演示程序,它不进行错误检查。)

// 20 june 2016
#define UNICODE
#define _UNICODE
#define STRICT
#define STRICT_TYPED_ITEMIDS
#define WINVER 0x0600
#define _WIN32_WINNT 0x0600
#define _WIN32_WINDOWS 0x0600
#define _WIN32_IE 0x0700
#define NTDDI_VERSION 0x06000000
#include <windows.h>
#include <uxtheme.h>
#include <vsstyle.h>
#include <vssym32.h>

HWND mainwin;
HWND edit;

void redraw(void)
{
HDC dc;
PAINTSTRUCT ps;
WCHAR text[1024];
HTHEME theme;
LOGFONTW lf;
HFONT font, oldfont;
COLORREF color;
SIZE extent;
RECT r;

dc = BeginPaint(mainwin, &ps);

theme = OpenThemeData(mainwin, VSCLASS_AEROWIZARD);
GetThemeFont(theme, dc, AW_HEADERAREA, 0, TMT_FONT, &lf);
font = CreateFontIndirectW(&lf);
GetThemeColor(theme, AW_HEADERAREA, 0, TMT_TEXTCOLOR, &color);

GetWindowTextW(edit, text, 1024);
GetThemeTextExtent(theme, dc, AW_HEADERAREA, 0,
text, -1, 0, NULL, &r);
extent.cx = r.right - r.left;
extent.cy = r.bottom - r.top;

SetWindowPos(edit, NULL,
10, 10, 500, 20,
SWP_NOOWNERZORDER | SWP_NOZORDER);

r.left = 10;
r.top = 40;
r.right = 10 + extent.cx;
r.bottom = r.top + extent.cy;
DrawThemeText(theme, dc, AW_HEADERAREA, 0,
text, -1, 0, 0, &r);

oldfont = (HFONT) SelectObject(dc, font);
color = SetTextColor(dc, color);
r.top = r.bottom + 10;
r.bottom = r.top + extent.cy;
DrawTextW(dc, text, -1, &r, 0);
SetTextColor(dc, color);
SelectObject(dc, oldfont);
DeleteObject(font);

CloseThemeData(theme);
EndPaint(mainwin, &ps);
}

LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_PAINT:
redraw();
return 0;
case WM_COMMAND:
InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}

int main(void)
{
WNDCLASSW wc;
MSG msg;

ZeroMemory(&wc, sizeof (WNDCLASSW));
wc.lpszClassName = L"mainwin";
wc.lpfnWndProc = wndproc;
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
RegisterClassW(&wc);

mainwin = CreateWindowExW(0,
L"mainwin", L"Main Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
640, 200,
NULL, NULL, NULL, NULL);

edit = CreateWindowExW(WS_EX_CLIENTEDGE,
L"EDIT", L"Top is DrawThemeText(), bottom is manual",
WS_CHILD | WS_VISIBLE,
10, 10, 100, 100,
mainwin, (HMENU) 100, NULL, NULL);

ShowWindow(mainwin, SW_SHOWDEFAULT);
UpdateWindow(mainwin);

while (GetMessageW(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return 0;
}

关于c++ - 获取系统字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682286/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com