- #include <windows.h>
- #include "res/resource.h"
- LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
- BOOL InitApp(HINSTANCE hInstance, LPCSTR szClassName);
- BOOL InitInstance(HINSTANCE hInstance, LPCSTR szClassName, int nCmdShow);
- int WINAPI WinMain(
- IN HINSTANCE hInstance,
- IN HINSTANCE hPrevInstance,
- IN LPSTR lpCmdLine,
- IN int nShowCmd
- )
- {
- MSG msg;
- char szClassName[] = "FontFace"; //窗口名
- //注册窗口类
- if(!InitApp(hInstance, szClassName))
- return 0;
- //初始化窗口
- if( !InitInstance(hInstance, szClassName, nShowCmd) )
- return 0;
- while(GetMessage(&msg, NULL, NULL, NULL))
- {
- TranslateMessage(&msg); //消息解释
- DispatchMessage(&msg); //消息传送
- }
- return (int)msg.wParam;
- }
- LRESULT CALLBACK WinProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- PAINTSTRUCT ps;
- HDC hdc;
- HFONT hFont, hOldFont;
- RECT rect;
- char *str = "徐国先";
- int width;
- int height;
- switch( uMsg )
- {
- case WM_PAINT:
- hdc = BeginPaint(hwnd, &ps);
- GetClientRect(hwnd, &rect);
- width = (rect.right-20)/(int)strlen(str);
- height = rect.bottom-20;
- hFont = CreateFont(
- height, //字体的逻辑高度
- width, //逻辑平均字符宽度
- 0, //与水平线的角度
- 0, //基线方位角度
- FW_REGULAR, //字形:常规
- FALSE, //字形:斜体
- FALSE, //字形:下划线
- FALSE, //字形:粗体
- GB2312_CHARSET, //字符集
- OUT_DEFAULT_PRECIS, //输出精度
- CLIP_DEFAULT_PRECIS, //剪截精度
- PROOF_QUALITY, //输出品质
- FIXED_PITCH | FF_MODERN, //倾斜度
- "隶书" //字体
- );
- hOldFont = (HFONT)SelectObject(hdc, hFont); //选择字体
- SetTextColor(hdc, 0xC0C0C0);
- TextOut(hdc, 10, 10, str, (int)strlen(str));
- SelectObject(hdc, hOldFont); //选择旧字体
- DeleteObject(hFont); //删除新字体
- EndPaint(hwnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- return 0;
- }
- BOOL InitApp(HINSTANCE hInstance, LPCSTR szClassName)
- {
- WNDCLASS wndClass;
- ZeroMemory(&wndClass, sizeof(wndClass));
- wndClass.style = CS_VREDRAW|CS_HREDRAW;
- wndClass.cbClsExtra = 0;
- wndClass.cbWndExtra = 0;
- wndClass.hInstance = hInstance;
- wndClass.hCursor = LoadCursor(hInstance, IDC_ARROW);
- wndClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));
- wndClass.hbrBackground = (HBRUSH)GetStockObject(COLOR_BTNFACE);
- wndClass.lpfnWndProc = (WNDPROC)WinProc;
- wndClass.lpszClassName = (LPCSTR)szClassName;
- wndClass.lpszMenuName = NULL;
- return RegisterClass(&wndClass);
- }
- BOOL InitInstance(HINSTANCE hInstance, LPCSTR szClassName, int nCmdShow)
- {
- HWND hwnd;
- hwnd = CreateWindow(
- szClassName,
- "Sdk 字体变换",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- 400, 300,
- NULL,
- NULL,
- hInstance,
- NULL
- );
- //
- if (!hwnd) return FALSE;
- ShowWindow(hwnd, nCmdShow);
- UpdateWindow(hwnd);
- return TRUE;
- }
该代码参考网上学习代码