zoukankan      html  css  js  c++  java
  • win32 字体变换与窗口同大同小

    [cpp] view plaincopy
     
    1. #include <windows.h>  
    2. #include "res/resource.h"  
    3.   
    4.   
    5. LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);  
    6. BOOL InitApp(HINSTANCE hInstance, LPCSTR szClassName);  
    7. BOOL InitInstance(HINSTANCE hInstance, LPCSTR szClassName, int nCmdShow);  
    8.   
    9.   
    10. int WINAPI WinMain(   
    11.      IN HINSTANCE hInstance,  
    12.      IN HINSTANCE hPrevInstance,   
    13.      IN LPSTR lpCmdLine,   
    14.      IN int nShowCmd  
    15.      )  
    16. {  
    17.     MSG msg;  
    18.     char szClassName[] = "FontFace";  //窗口名  
    19.     //注册窗口类  
    20.     if(!InitApp(hInstance, szClassName))  
    21.         return 0;  
    22.     //初始化窗口  
    23.     if( !InitInstance(hInstance, szClassName, nShowCmd) )  
    24.         return 0;  
    25.   
    26.   
    27.     while(GetMessage(&msg, NULL, NULL, NULL))  
    28.     {  
    29.         TranslateMessage(&msg);  //消息解释  
    30.         DispatchMessage(&msg);   //消息传送  
    31.     }  
    32.     return (int)msg.wParam;  
    33. }  
    34.   
    35.   
    36. LRESULT CALLBACK WinProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )  
    37. {  
    38.     PAINTSTRUCT ps;  
    39.     HDC hdc;  
    40.     HFONT hFont, hOldFont;  
    41.     RECT rect;  
    42.     char *str = "徐国先";  
    43.     int width;  
    44.     int height;  
    45.     switch( uMsg )  
    46.     {  
    47.     case WM_PAINT:  
    48.         hdc = BeginPaint(hwnd, &ps);  
    49.         GetClientRect(hwnd, &rect);  
    50.         width = (rect.right-20)/(int)strlen(str);  
    51.         height = rect.bottom-20;  
    52.         hFont = CreateFont(  
    53.             height,      //字体的逻辑高度  
    54.             width,       //逻辑平均字符宽度  
    55.             0,           //与水平线的角度  
    56.             0,           //基线方位角度  
    57.             FW_REGULAR,  //字形:常规  
    58.             FALSE,       //字形:斜体  
    59.             FALSE,       //字形:下划线  
    60.             FALSE,       //字形:粗体  
    61.             GB2312_CHARSET,          //字符集  
    62.             OUT_DEFAULT_PRECIS,      //输出精度  
    63.             CLIP_DEFAULT_PRECIS,     //剪截精度  
    64.             PROOF_QUALITY,           //输出品质  
    65.             FIXED_PITCH | FF_MODERN, //倾斜度  
    66.             "隶书"                   //字体  
    67.             );   
    68.         hOldFont = (HFONT)SelectObject(hdc, hFont);       //选择字体  
    69.         SetTextColor(hdc, 0xC0C0C0);  
    70.         TextOut(hdc, 10, 10, str, (int)strlen(str));  
    71.         SelectObject(hdc, hOldFont);                      //选择旧字体  
    72.         DeleteObject(hFont);                              //删除新字体  
    73.         EndPaint(hwnd, &ps);  
    74.         break;  
    75.   
    76.   
    77.     case WM_DESTROY:  
    78.         PostQuitMessage(0);  
    79.         break;  
    80.     default:  
    81.        return DefWindowProc(hwnd, uMsg, wParam, lParam);  
    82.     }  
    83.     return 0;  
    84. }  
    85.   
    86.   
    87. BOOL InitApp(HINSTANCE hInstance, LPCSTR szClassName)  
    88. {  
    89.     WNDCLASS wndClass;  
    90.     ZeroMemory(&wndClass, sizeof(wndClass));  
    91.   
    92.   
    93.     wndClass.style          = CS_VREDRAW|CS_HREDRAW;  
    94.     wndClass.cbClsExtra     = 0;  
    95.     wndClass.cbWndExtra     = 0;  
    96.     wndClass.hInstance      = hInstance;  
    97.     wndClass.hCursor        = LoadCursor(hInstance, IDC_ARROW);  
    98.     wndClass.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TEST));  
    99.     wndClass.hbrBackground  = (HBRUSH)GetStockObject(COLOR_BTNFACE);  
    100.     wndClass.lpfnWndProc    = (WNDPROC)WinProc;  
    101.     wndClass.lpszClassName  = (LPCSTR)szClassName;  
    102.     wndClass.lpszMenuName   = NULL;  
    103.     return RegisterClass(&wndClass);  
    104. }  
    105.   
    106.   
    107. BOOL InitInstance(HINSTANCE hInstance, LPCSTR szClassName, int nCmdShow)  
    108. {  
    109.     HWND hwnd;  
    110.     hwnd = CreateWindow(  
    111.         szClassName,  
    112.         "Sdk 字体变换",  
    113.         WS_OVERLAPPEDWINDOW,  
    114.         CW_USEDEFAULT, CW_USEDEFAULT,  
    115.         400, 300,  
    116.         NULL,  
    117.         NULL,  
    118.         hInstance,  
    119.         NULL  
    120.         );  
    121.     //  
    122.     if (!hwnd) return FALSE;  
    123.     ShowWindow(hwnd, nCmdShow);  
    124.     UpdateWindow(hwnd);  
    125.     return TRUE;  
    126. }  

    该代码参考网上学习代码

  • 相关阅读:
    比特币全节点(bitcoind) eth 全节点
    Knowledge Tracing -- 基于贝叶斯的学生知识点追踪(BKT)
    trate
    spark
    linux 切换c++版本
    查找两个数组的相同字符(两个超大文件的相同字符)
    作业调度框架FluentScheduler
    .net与js数据交换中文乱码问题解决
    数据库死锁问题
    Service Fabric service 根据环境变量读取配置文件
  • 原文地址:https://www.cnblogs.com/lidabo/p/3701679.html
Copyright © 2011-2022 走看看