首页 > 代码库 > win32 字体变换与窗口同大同小
win32 字体变换与窗口同大同小
[cpp] view plaincopy
- #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;
- }
该代码参考网上学习代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。