鼠标绘图
1 // TestGDI.cpp : 定义应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "TestGDI.h" 6 7 #define MAX_LOADSTRING 100 8 9 // 全局变量: 10 HINSTANCE hInst; // 当前实例 11 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 12 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 13 14 // 此代码模块中包含的函数的前向声明: 15 ATOM MyRegisterClass(HINSTANCE hInstance); 16 BOOL InitInstance(HINSTANCE, int); 17 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 18 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 19 20 int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, 21 _In_opt_ HINSTANCE hPrevInstance, 22 _In_ LPTSTR lpCmdLine, 23 _In_ int nCmdShow) 24 { 25 UNREFERENCED_PARAMETER(hPrevInstance); 26 UNREFERENCED_PARAMETER(lpCmdLine); 27 28 // TODO: 在此放置代码。 29 MSG msg; 30 HACCEL hAccelTable; 31 32 // 初始化全局字符串 33 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 34 LoadString(hInstance, IDC_TESTGDI, szWindowClass, MAX_LOADSTRING); 35 MyRegisterClass(hInstance); 36 37 // 执行应用程序初始化: 38 if (!InitInstance (hInstance, nCmdShow)) 39 { 40 return FALSE; 41 } 42 43 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTGDI)); 44 45 // 主消息循环: 46 while (GetMessage(&msg, NULL, 0, 0)) 47 { 48 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 49 { 50 TranslateMessage(&msg); 51 DispatchMessage(&msg); 52 } 53 } 54 55 return (int) msg.wParam; 56 } 57 58 59 60 // 61 // 函数: MyRegisterClass() 62 // 63 // 目的: 注册窗口类。 64 // 65 ATOM MyRegisterClass(HINSTANCE hInstance) 66 { 67 WNDCLASSEX wcex; 68 69 wcex.cbSize = sizeof(WNDCLASSEX); 70 71 wcex.style = CS_HREDRAW | CS_VREDRAW; 72 wcex.lpfnWndProc = WndProc; 73 wcex.cbClsExtra = 0; 74 wcex.cbWndExtra = 0; 75 wcex.hInstance = hInstance; 76 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTGDI)); 77 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 78 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 79 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_TESTGDI); 80 wcex.lpszClassName = szWindowClass; 81 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 82 83 return RegisterClassEx(&wcex); 84 } 85 86 // 87 // 函数: InitInstance(HINSTANCE, int) 88 // 89 // 目的: 保存实例句柄并创建主窗口 90 // 91 // 注释: 92 // 93 // 在此函数中,我们在全局变量中保存实例句柄并 94 // 创建和显示主程序窗口。 95 // 96 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 97 { 98 HWND hWnd; 99 100 hInst = hInstance; // 将实例句柄存储在全局变量中 101 102 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 103 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 104 105 if (!hWnd) 106 { 107 return FALSE; 108 } 109 110 ShowWindow(hWnd, nCmdShow); 111 UpdateWindow(hWnd); 112 113 return TRUE; 114 } 115 116 DWORD flag = 0; 117 INT_PTR DlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 118 { 119 int wmId, wmEvent; 120 switch (message) 121 { 122 123 case WM_COMMAND: 124 wmId = LOWORD(wParam); 125 wmEvent = HIWORD(wParam); 126 127 switch (wmId) 128 { 129 case IDC_BUTTON1: 130 flag = 1; 131 break; 132 case IDC_BUTTON2: 133 flag = 2; 134 break; 135 case IDC_BUTTON3: 136 flag = 3; 137 break; 138 default: 139 break; 140 } 141 break; 142 } 143 144 return FALSE; 145 } 146 147 // dc.Rectangle(260, 100, 180, 240);//绘制矩形 148 // dc.Ellipse(260, 100, 180, 240);//绘制椭圆 149 150 void DradLine(HWND hWnd, COORD startPoint, COORD endPoint){ HDC hdc; 151 152 hdc = GetDC(hWnd); 153 MoveToEx(hdc, startPoint.X, startPoint.Y, NULL); 154 LineTo(hdc, endPoint.X, endPoint.Y); 155 ReleaseDC(hWnd, hdc); 156 } 157 void DradEllipse(HWND hWnd, COORD startPoint, COORD endPoint){ 158 HDC hdc; 159 160 hdc = GetDC(hWnd); 161 Ellipse(hdc, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y); 162 ReleaseDC(hWnd, hdc); 163 } 164 void DradRect(HWND hWnd, COORD startPoint, COORD endPoint){ 165 HDC hdc; 166 167 hdc = GetDC(hWnd); 168 RoundRect(hdc, startPoint.X, startPoint.Y, endPoint.X, endPoint.Y, 5, 5); 169 ReleaseDC(hWnd, hdc); 170 } 171 172 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 173 { 174 int wmId, wmEvent; 175 PAINTSTRUCT ps; 176 HDC hdc; 177 static HWND hDlgWnd; 178 static COORD startPoint = { 0 }; 179 static COORD endPoint = { 0 }; 180 switch (message) 181 { 182 case WM_CREATE: 183 hDlgWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, (DLGPROC)DlgProc); 184 ShowWindow(hDlgWnd, SW_SHOW); 185 break; 186 187 case WM_LBUTTONDOWN: 188 startPoint.X = LOWORD(lParam); 189 startPoint.Y = HIWORD(lParam); 190 191 break; 192 case WM_LBUTTONUP: 193 194 endPoint.X = LOWORD(lParam); 195 endPoint.Y = HIWORD(lParam); 196 if (flag == 1) 197 { 198 DradLine(hWnd,startPoint, endPoint); 199 } 200 if (flag == 2) 201 { 202 DradEllipse(hWnd, startPoint, endPoint); 203 } 204 if (flag == 3) 205 { 206 DradRect(hWnd, startPoint, endPoint); 207 } 208 209 break; 210 case WM_MOUSEMOVE: 211 { 212 // endPoint.X = LOWORD(lParam); 213 // endPoint.Y = HIWORD(lParam); 214 } break; 215 case WM_COMMAND: 216 wmId = LOWORD(wParam); 217 wmEvent = HIWORD(wParam); 218 // 分析菜单选择: 219 switch (wmId) 220 { 221 222 case IDM_ABOUT: 223 DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); 224 break; 225 case IDM_EXIT: 226 DestroyWindow(hWnd); 227 break; 228 default: 229 return DefWindowProc(hWnd, message, wParam, lParam); 230 } 231 break; 232 case WM_PAINT: 233 hdc = BeginPaint(hWnd, &ps); 234 // TODO: 在此添加任意绘图代码... 235 EndPaint(hWnd, &ps); 236 break; 237 case WM_DESTROY: 238 PostQuitMessage(0); 239 break; 240 default: 241 return DefWindowProc(hWnd, message, wParam, lParam); 242 } 243 return 0; 244 } 245 246 // “关于”框的消息处理程序。 247 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 248 { 249 UNREFERENCED_PARAMETER(lParam); 250 switch (message) 251 { 252 case WM_INITDIALOG: 253 return (INT_PTR)TRUE; 254 255 case WM_COMMAND: 256 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 257 { 258 EndDialog(hDlg, LOWORD(wParam)); 259 return (INT_PTR)TRUE; 260 } 261 break; 262 } 263 return (INT_PTR)FALSE; 264 }
让数据变得更安全!