GDI 线段绘制示例程序

Posted yenyuloong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GDI 线段绘制示例程序相关的知识,希望对你有一定的参考价值。

  1 #include <windows.h>
  2 #include <strsafe.h>
  3 #include <stack>
  4 
  5 typedef struct tagLINE {
  6     POINT ptStart;
  7     POINT ptEnd;
  8 }LINE;
  9 
 10 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 11 {
 12     HDC hdc;
 13     PAINTSTRUCT ps;
 14     TCHAR szBuffer[100];
 15     size_t nLength;
 16     static int cxClient, cyClient;
 17     static BOOL fDrawing;
 18     static LINE arrLine[1000];
 19     static int length;
 20     static LINE line;
 21     int i;
 22 
 23     switch (message)
 24     {
 25     case WM_CREATE:
 26         length = 0;
 27         fDrawing = FALSE;
 28         return 0;
 29 
 30     case WM_LBUTTONUP:
 31         
 32         if (fDrawing) {
 33             line.ptEnd.x = LOWORD(lParam);
 34             line.ptEnd.y = HIWORD(lParam);
 35             arrLine[length] = line;
 36             length++;
 37         }
 38         else {
 39             line.ptStart.x = LOWORD(lParam);
 40             line.ptStart.y = HIWORD(lParam);
 41         }
 42 
 43         fDrawing = !fDrawing;
 44 
 45         InvalidateRect(hwnd, NULL, TRUE);
 46         
 47         return 0;
 48 
 49     case WM_SIZE:
 50         hdc = GetDC(hwnd);
 51 
 52         cxClient = LOWORD(lParam);
 53         cyClient = HIWORD(lParam);
 54 
 55         ReleaseDC(hwnd, hdc);
 56         return 0;
 57 
 58     case WM_PAINT:
 59         hdc = BeginPaint(hwnd, &ps);
 60 
 61         for (i = 0; i < length; i++) {
 62             MoveToEx(hdc, arrLine[i].ptStart.x, arrLine[i].ptStart.y, NULL);
 63             LineTo(hdc, arrLine[i].ptEnd.x, arrLine[i].ptEnd.y);
 64         }
 65 
 66         StringCchPrintf(szBuffer, sizeof(szBuffer) / sizeof(TCHAR), TEXT("Size: %d x %d"), cxClient, cyClient);
 67         StringCchLength(szBuffer, sizeof(szBuffer) / sizeof(TCHAR), &nLength);
 68 
 69         SetTextAlign(hdc, TA_TOP | TA_RIGHT);
 70         TextOut(hdc, cxClient, 0, szBuffer, nLength);
 71 
 72         EndPaint(hwnd, &ps);
 73         return 0;
 74 
 75     case WM_DESTROY:
 76         PostQuitMessage(0);
 77         return 0;
 78     }
 79 
 80     return DefWindowProc(hwnd, message, wParam, lParam);
 81 }
 82 
 83 
 84 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdLine)
 85 {
 86     LPCTSTR lpszClassName = TEXT("Demo");
 87     LPCTSTR lpszWindowName = TEXT("Demo Window");
 88 
 89     WNDCLASS wndclass;
 90     wndclass.cbClsExtra = 0;
 91     wndclass.cbWndExtra = 0;
 92     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 93     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 94     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 95     wndclass.hInstance = hInstance;
 96     wndclass.lpfnWndProc = WndProc;
 97     wndclass.lpszClassName = lpszClassName;
 98     wndclass.lpszMenuName = NULL;
 99     wndclass.style = CS_HREDRAW | CS_VREDRAW;
100 
101     if (!RegisterClass(&wndclass)) {
102         MessageBox(NULL, TEXT("This program requires Windows NT!"), TEXT("Error"), MB_ICONERROR);
103         return 0;
104     }
105 
106     HWND hwnd = CreateWindow(
107         lpszClassName,
108         lpszWindowName,
109         WS_OVERLAPPEDWINDOW,
110         CW_USEDEFAULT,
111         CW_USEDEFAULT,
112         CW_USEDEFAULT,
113         CW_USEDEFAULT,
114         NULL,
115         NULL,
116         hInstance,
117         NULL
118     );
119 
120     ShowWindow(hwnd, nCmdLine);
121     UpdateWindow(hwnd);
122 
123     MSG msg;
124     while (GetMessage(&msg, NULL, 0, 0))
125     {
126         TranslateMessage(&msg);
127         DispatchMessage(&msg);
128     }
129 
130     return msg.wParam;
131 }

 

以上是关于GDI 线段绘制示例程序的主要内容,如果未能解决你的问题,请参考以下文章

C# wpf 使用GDI+实现截屏

GDI对象泄漏导致程序UI界面绘制异常的问题排查

GDI+ Graphics Obj - 使用 GDI 直接绘制到 DC 时消除锯齿丢失

GDI 中的多边形函数

GDI对象泄漏导致程序UI界面绘制异常的问题排查

GDI+ 多线程绘图