程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 鼠標的應用示例,鼠標應用示例

鼠標的應用示例,鼠標應用示例

編輯:C++入門知識

鼠標的應用示例,鼠標應用示例


  1 #include <Windows.h>
  2 #include <tchar.h>
  3 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow);
  4 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  5 HFONT CreateFont(HDC hDC, int nCharHeight, BOOL bItalic);
  6 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  7 {
  8     MSG msg;
  9     if (!InitWindowClass(hInstance, nCmdShow))
 10     {
 11         MessageBox(NULL, L"創建窗口失敗!", _T("創建窗口"), NULL);
 12         return 1;
 13     }
 14     while (GetMessage(&msg, NULL, 0, 0))
 15     {
 16         TranslateMessage(&msg);
 17         DispatchMessage(&msg);
 18     }
 19     return(int)msg.wParam;
 20 }
 21 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 22 {
 23     HDC hDC;
 24     HFONT hF;
 25     PAINTSTRUCT ps;
 26     TEXTMETRIC tm;
 27     TCHAR str[] = L" Hello World ";
 28     int i = 0;
 29     static int x[13], y[13];
 30     static int color[13];
 31     POINT pt;
 32     switch (message)
 33     {
 34     case WM_CREATE:
 35         SetTimer(hWnd, 1111, 200, NULL);
 36         GetCursorPos(&pt);
 37         ScreenToClient(hWnd, &pt);
 38         for (i = 0; i < 13; i++)
 39         {
 40             x[i] = pt.x + (i - 1) * 40;
 41             y[i] = pt.y;
 42             color[i] = 25 * (i - 1);
 43         }
 44         break;
 45     case WM_PAINT:
 46         hDC = BeginPaint(hWnd, &ps);
 47         hF = CreateFont(hDC, 40, 0);
 48         SelectObject(hDC, hF);
 49         for (i = 12; i > 1; i--)
 50         {
 51             x[i] = x[i - 1] + 40;
 52             y[i] = y[i - 1];
 53         }
 54         GetCursorPos(&pt);
 55         ScreenToClient(hWnd, &pt);
 56         x[1] = pt.x;
 57         y[1] = pt.y;
 58         for (i = 1; i < 13; i++)
 59         {
 60             SetTextColor(hDC, RGB(255 - color[i], color[i], 255));
 61             TextOut(hDC, x[i], y[i], &str[i], 1);
 62         }
 63         color[1] = color[12];
 64         for (i = 12; i > 1; i--)
 65             color[i] = color[i - 1];
 66         DeleteObject(hF);
 67         EndPaint(hWnd, &ps);
 68         break;
 69     case WM_TIMER:
 70         if (wParam == 1111)
 71             InvalidateRect(hWnd, NULL, 1);
 72         break;
 73     case WM_DESTROY:
 74         PostQuitMessage(0);
 75         break;
 76     default:
 77         return DefWindowProc(hWnd, message, wParam, lParam);
 78         break;
 79     }
 80     return 0;
 81 }
 82 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow)
 83 {
 84     WNDCLASSEX wcex;
 85     HWND hWnd;
 86     TCHAR szWindowClass[] = L"窗口示例";
 87     TCHAR szTitle[] = L"鼠標的應用示例";
 88     wcex.cbSize = sizeof(WNDCLASSEX);
 89     wcex.style = 0;
 90     wcex.lpfnWndProc = WndProc;
 91     wcex.cbClsExtra = 0;
 92     wcex.cbWndExtra = 0;
 93     wcex.hInstance = hInstance;
 94     wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
 95     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
 96     wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 97     wcex.lpszMenuName = NULL;
 98     wcex.lpszClassName = szWindowClass;
 99     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
100     if (!RegisterClassEx(&wcex))
101         return FALSE;
102     hWnd = CreateWindow(
103         szWindowClass,
104         szTitle,
105         WS_OVERLAPPEDWINDOW,
106         CW_USEDEFAULT, CW_USEDEFAULT,
107         CW_USEDEFAULT, CW_USEDEFAULT,
108         NULL,
109         NULL,
110         hInstance,
111         NULL
112         );
113     if (!hWnd)
114         return FALSE;
115     ShowWindow(hWnd, nCmdShow);
116     UpdateWindow(hWnd);
117     return TRUE;
118 }
119 HFONT CreateFont(HDC hDC, int nCharHeight, BOOL bItalic)
120 {
121     HFONT hFont;
122     hFont = CreateFont(
123         nCharHeight,
124         0,
125         0,
126         0,
127         400,
128         bItalic,
129         0,
130         0,
131         ANSI_CHARSET,
132         OUT_DEFAULT_PRECIS,
133         CLIP_DEFAULT_PRECIS,
134         DEFAULT_QUALITY,
135         DEFAULT_PITCH | FF_DONTCARE,
136         L"Arial"
137         );
138     if (hFont == NULL)
139         return NULL;
140     else
141         return hFont;
142 }

截圖看不出效果^-^

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved