1 // 01WM_PAINT与GetDC.cpp : 定义应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include "01WM_PAINT与GetDC.h" 6 7 #define MAX_LOADSTRING 100 8 9 #define MYBUTTON1 10001 10 11 #define MYBUTTON2 10002 12 13 #define MYBUTTON3 10003 14 // 全局变量: 15 HINSTANCE hInst; // 当前实例 16 TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本 17 TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名 18 19 // 此代码模块中包含的函数的前向声明: 20 ATOM MyRegisterClass(HINSTANCE hInstance); 21 BOOL InitInstance(HINSTANCE, int); 22 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 23 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); 24 25 int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, 26 _In_opt_ HINSTANCE hPrevInstance, 27 _In_ LPTSTR lpCmdLine, 28 _In_ int nCmdShow) 29 { 30 UNREFERENCED_PARAMETER(hPrevInstance); 31 UNREFERENCED_PARAMETER(lpCmdLine); 32 33 // TODO: 在此放置代码。 34 MSG msg; 35 HACCEL hAccelTable; 36 37 // 初始化全局字符串 38 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 39 LoadString(hInstance, IDC_MY01WM_PAINTGETDC, szWindowClass, MAX_LOADSTRING); 40 MyRegisterClass(hInstance); 41 42 // 执行应用程序初始化: 43 if (!InitInstance (hInstance, nCmdShow)) 44 { 45 return FALSE; 46 } 47 48 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY01WM_PAINTGETDC)); 49 50 // 主消息循环: 51 while (GetMessage(&msg, NULL, 0, 0)) 52 { 53 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 54 { 55 TranslateMessage(&msg); 56 DispatchMessage(&msg); 57 } 58 } 59 60 return (int) msg.wParam; 61 } 62 63 64 65 // 66 // 函数: MyRegisterClass() 67 // 68 // 目的: 注册窗口类。 69 // 70 ATOM MyRegisterClass(HINSTANCE hInstance) 71 { 72 WNDCLASSEX wcex; 73 74 wcex.cbSize = sizeof(WNDCLASSEX); 75 76 wcex.style = CS_HREDRAW | CS_VREDRAW; 77 wcex.lpfnWndProc = WndProc; 78 wcex.cbClsExtra = 0; 79 wcex.cbWndExtra = 0; 80 wcex.hInstance = hInstance; 81 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY01WM_PAINTGETDC)); 82 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 83 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); 84 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MY01WM_PAINTGETDC); 85 wcex.lpszClassName = szWindowClass; 86 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); 87 88 return RegisterClassEx(&wcex); 89 } 90 91 // 92 // 函数: InitInstance(HINSTANCE, int) 93 // 94 // 目的: 保存实例句柄并创建主窗口 95 // 96 // 注释: 97 // 98 // 在此函数中,我们在全局变量中保存实例句柄并 99 // 创建和显示主程序窗口。 100 // 101 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 102 { 103 HWND hWnd; 104 105 hInst = hInstance; // 将实例句柄存储在全局变量中 106 107 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 108 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); 109 110 if (!hWnd) 111 { 112 return FALSE; 113 } 114 115 ShowWindow(hWnd, nCmdShow); 116 UpdateWindow(hWnd); 117 118 return TRUE; 119 } 120 121 // 122 // 函数: WndProc(HWND, UINT, WPARAM, LPARAM) 123 // 124 // 目的: 处理主窗口的消息。 125 // 126 // WM_COMMAND - 处理应用程序菜单 127 // WM_PAINT - 绘制主窗口 128 // WM_DESTROY - 发送退出消息并返回 129 // 130 // 131 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 132 { 133 int wmId, wmEvent; 134 PAINTSTRUCT ps; 135 HDC hdc; 136 static int nNum = 10; 137 switch (message) 138 { 139 case WM_CREATE: 140 //创建按钮用于GetDC() 141 CreateWindow( 142 TEXT("button"), 143 TEXT("hehe"), 144 WS_VISIBLE | WS_CHILD, 145 50, 50, 60, 30, hWnd, 146 (HMENU)MYBUTTON1, 147 hInst, 148 NULL); 149 //创建按钮用于GetWindowDC() 150 CreateWindow( 151 TEXT("button"), 152 TEXT("hehe"), 153 WS_VISIBLE | WS_CHILD, 154 120, 50, 60, 30, hWnd, 155 (HMENU)MYBUTTON2, 156 hInst, 157 NULL); 158 CreateWindow( 159 TEXT("button"), 160 TEXT("hehe"), 161 WS_VISIBLE | WS_CHILD, 162 190, 50, 60, 30, hWnd, 163 (HMENU)MYBUTTON3, 164 hInst, 165 NULL); 166 167 case WM_COMMAND: 168 wmId = LOWORD(wParam); 169 wmEvent = HIWORD(wParam); 170 // 分析菜单选择: 171 switch (wmId) 172 { 173 case MYBUTTON1: 174 { 175 HDC hDc = GetDC(NULL); 176 TextOut(hDc, 0, 0, TEXT("15PB"), 4); 177 ReleaseDC(hWnd, hDc); 178 } 179 break; 180 case MYBUTTON2: 181 { 182 HDC hDc = GetWindowDC(hWnd); 183 TextOut(hDc, 0, 20, TEXT("15PB"), 4); 184 } 185 break; 186 case MYBUTTON3: 187 { 188 HDC hDc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL); 189 TextOut(hDc, 100, 100, TEXT("15PB"), 4); 190 } 191 default: 192 return DefWindowProc(hWnd, message, wParam, lParam); 193 } 194 break; 195 case WM_PAINT: 196 { 197 hdc = BeginPaint(hWnd, &ps); 198 // TODO: 在此添加任意绘图代码... 199 //用以验证有效区域和无效区域 200 nNum++; 201 TCHAR buf[20] = {}; 202 _stprintf_s(buf, 20, TEXT("%d"), nNum); 203 TextOut(hdc, 100, 100, buf, _tcsclen(buf)); 204 EndPaint(hWnd, &ps); 205 break; 206 } 207 case WM_NCPAINT: 208 { 209 // HDC hdc; 210 // hdc = GetDCEx(hWnd, (HRGN)wParam, DCX_WINDOW | DCX_INTERSECTRGN); 211 // TextOut(hdc, 0, 0, TEXT("15PB"), 4); 212 // ReleaseDC(hWnd, hdc); 213 return DefWindowProc(hWnd, message, wParam, lParam); 214 break; 215 } 216 case WM_DESTROY: 217 PostQuitMessage(0); 218 break; 219 default: 220 return DefWindowProc(hWnd, message, wParam, lParam); 221 } 222 return 0; 223 } 224 225 // “关于”框的消息处理程序。 226 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) 227 { 228 UNREFERENCED_PARAMETER(lParam); 229 switch (message) 230 { 231 case WM_INITDIALOG: 232 return (INT_PTR)TRUE; 233 234 case WM_COMMAND: 235 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 236 { 237 EndDialog(hDlg, LOWORD(wParam)); 238 return (INT_PTR)TRUE; 239 } 240 break; 241 } 242 return (INT_PTR)FALSE; 243 }
时间: 2024-09-30 15:06:42