我想知道
1. 如何在GDI中使用系统已安装字体
2. 如何在GDI中使用我自己的字体,假定字体是与程序(EXE)同目录的一个TTF文件 且未安装在运行此程序的电脑上
顺便附我的代码,希望这能帮你更好地解决我的问题
和字体有关的部分主要在 Init 和 Paint 函数里
#include
#include
#include
#include
#include
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE L"WND_TITLE"
#define REPAIN_TIME 0.02
HDC g_hdc = NULL;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL Init(HWND hwnd);
VOID Paint(HWND hwnd);
BOOL CleanUp(HWND hwnd);
VOID Deal_WM_KEYDOWN(HWND hwnd, WPARAM wParam);
ULONGLONG ull_PaintTime, ull_LastPaintTime;
HPEN hPen_tmp;
HFONT hFont; //字体
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_ HINSTANCE hPrevInstance, _In_opt_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
WNDCLASSEX wndClass = { 0 };
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;
wndClass.hIcon = (HICON)::LoadImage(NULL, L"icon.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = L"ClassName";
if (!RegisterClassEx(&wndClass))
{
MessageBox(0, L"RegisterClassEx", L"RegisterClassEx", 0);
return -1;
}
HWND hwnd = CreateWindow(L"ClassName", WINDOW_TITLE,
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH,
WINDOW_HEIGHT, NULL, NULL, hInstance, NULL);
MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true);
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
if (!Init(hwnd))
{
MessageBox(hwnd, L"Init", L"Init", 0);
return -1;
}
MSG msg = { 0 };
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
ull_PaintTime = GetTickCount64();
if (ull_PaintTime - ull_LastPaintTime >= REPAIN_TIME)
{
ull_LastPaintTime = ull_PaintTime;
Paint(hwnd);
}
}
}
UnregisterClass(L"ClassName", wndClass.hInstance);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
switch (wParam)
{
/*
case VK_ESCAPE:
{
DestroyWindow(hwnd);
break;
}
*/
default:
{
}
}
break;
case WM_DESTROY:
CleanUp(hwnd);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
BOOL Init(HWND hwnd)
{
g_hdc = GetDC(hwnd);
SetBkMode(g_hdc, TRANSPARENT); //设置输出文字背景透明
SetTextColor(g_hdc, RGB(255, 255, 255));
//hFont = CreateFont(-18, -9, 0, 0, FW_REGULAR, FALSE, FALSE, FALSE, GB2312_CHARSET, 0, 0, 0, 0, 0);
LPCWSTR Font_Resource = L"ProggyCleanSZ-1.ttf";
if (AddFontResource(Font_Resource) == 0)
{
MessageBox(0, L"AddFontResource", L"AddFontResource", MB_OK);
}
PostMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
hPen_tmp = CreatePen(PS_SOLID, 2, RGB(255, 255, 255));
Paint(hwnd);
return TRUE;
}
VOID Paint(HWND hwnd)
{
LPCTSTR s = L"a quick brown fox jumps over the lazy dog,A QUICK BROWN FOX JUMPS OVER THE LAZY DOG.";
RECT tmp_rect;
tmp_rect.left = 10;
tmp_rect.right = 1000;
tmp_rect.top = 10;
tmp_rect.bottom = 40;
int s_len = _tcslen(s);
//SelectObject(g_hdc, hFont);
DrawText(g_hdc, s, s_len, &tmp_rect, DT_LEFT);
}
BOOL CleanUp(HWND hwnd)
{
DeleteObject(hPen_tmp);
DeleteObject(hFont);
RemoveFontResource(L"ProggyCleanSZ-1.ttf");
ReleaseDC(hwnd, g_hdc);
return TRUE;
}