windows编程之文本输出(字体渐变)

来源:互联网 发布:yum缓存目录 编辑:程序博客网 时间:2024/06/07 15:30


代码如下:


#include<windows.h>#include<math.h>#include<stdio.h>const double PI = 3.1415926;LRESULT CALLBACK WndProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter);int WINAPI WinMain(  HINSTANCE hInstance,      // handle to current instance  HINSTANCE hPrevInstance,  // handle to previous instance  LPSTR lpCmdLine,          // command line  int nCmdShow              // show state){WNDCLASS wndclass;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndclass.hInstance = hInstance;wndclass.lpfnWndProc = WndProc;wndclass.lpszClassName = "我的窗口";wndclass.lpszMenuName = NULL;wndclass.style = CS_HREDRAW | CS_VREDRAW;RegisterClass(&wndclass); //注册窗口类HWND hwnd;hwnd = CreateWindow("我的窗口", "窗口", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);ShowWindow(hwnd, SW_SHOWNORMAL);UpdateWindow(hwnd);MSG Msg;while(GetMessage(&Msg, NULL, 0, 0)){TranslateMessage(&Msg);         DispatchMessage(&Msg); }return 0;}LRESULT CALLBACK WndProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter){HDC hdc;PAINTSTRUCT ps;HFONT hfont;TEXTMETRIC tm;SIZE size;char str[] = "文字渐变练习";int i, nchlen;switch(uMsg){case WM_PAINT:hdc = BeginPaint(hwnd, &ps);hfont = CreateFont(20, 0, 0, 0, FW_HEAVY, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "粗体字");SelectObject(hdc, hfont);GetTextMetrics(hdc, &tm);GetTextExtentPoint32(hdc, str, strlen(str), &size);SetTextColor(hdc, RGB(0, 0, 0));GetTextMetrics(hdc, &tm);TextOut(hdc, 200, 200, str, strlen(str));MoveToEx(hdc, 200, 200, NULL);SetTextColor(hdc, RGB(0, 255, 0));SetBkColor(hdc, RGB(255, 0, 0));nchlen = size.cx / strlen(str);for(i=0; i<strlen(str); i=i+2){TextOut(hdc, 200 + nchlen * i, 200, &str[i], 2);Sleep(800);}EndPaint(hwnd, &ps);DeleteObject(hfont);break;case WM_CLOSE:DestroyWindow(hwnd);break;case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, uMsg, wParam, lParam);}return 0;}


原创粉丝点击