标签:
栗子1:
1 #include <windows.h> 2 3 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//回调函数 4 5 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 6 { 7 WNDCLASS wndclass; 8 MSG msg; 9 HWND hWnd; 10 TCHAR Name_1[] = TEXT("MyClass"), Name_2[] = TEXT("MyWindows"); 11 12 //设置类风格 13 wndclass.cbClsExtra = 0; 14 wndclass.cbWndExtra = 0; 15 wndclass.hbrBackground =(HBRUSH)GetStockObject(WHITE_BRUSH); 16 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 17 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 18 wndclass.hInstance = hInstance; 19 wndclass.lpfnWndProc = WndProc; 20 wndclass.lpszClassName = Name_1; 21 wndclass.lpszMenuName = NULL; 22 wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;//增加 鼠标可双击 标识 23 24 RegisterClass(&wndclass);//注册类 25 26 //创建窗体 27 hWnd = CreateWindow(Name_1, Name_2, 28 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 29 30 //更新 显示窗口 31 UpdateWindow(hWnd); 32 ShowWindow(hWnd, iCmdShow); 33 34 //消息循环 35 while (GetMessage(&msg, NULL, NULL, NULL)) 36 { 37 TranslateMessage(&msg);//翻译键盘消息 38 DispatchMessage(&msg);//转发消息给WndProc 39 } 40 return msg.wParam; 41 } 42 43 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 44 { 45 PAINTSTRUCT ps; 46 HDC hdc; 47 RECT rect; 48 49 switch (msg) 50 { 51 case WM_LBUTTONDOWN://单机消息 屏蔽 不然 它阻碍了 双击消息 52 //MessageBox(hWnd, TEXT("这是单击"), TEXT("test"), MB_OK); 53 return 0; 54 case WM_LBUTTONDBLCLK://双击消息 55 MessageBox(hWnd, TEXT("6666"), TEXT("test"), MB_OK); 56 return 0; 57 case WM_PAINT: 58 hdc = BeginPaint(hWnd, &ps); 59 GetClientRect(hWnd, &rect); 60 DrawText(hdc, TEXT("兄弟 请双击~~~~"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); 61 62 EndPaint(hWnd, &ps); 63 return 0; 64 case WM_DESTROY: 65 PostQuitMessage(0); 66 return 0; 67 } 68 69 70 71 return DefWindowProc(hWnd, msg, wParam, lParam); 72 }
栗子2:
1 #include <windows.h> 2 #include <windowsx.h>//MSDN最新 获取 多屏幕 x,y坐标函数 用的 库 3 4 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 5 6 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 7 { 8 WNDCLASS wndclass; 9 MSG msg; 10 HWND hWnd; 11 TCHAR Name_1[] = TEXT("MyClass"), Name_2[] = TEXT("MyWindows"); 12 13 wndclass.cbClsExtra = 0; 14 wndclass.cbWndExtra = 0; 15 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 16 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 17 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 18 wndclass.hInstance = hInstance; 19 wndclass.lpfnWndProc = WndProc; 20 wndclass.lpszClassName = Name_1; 21 wndclass.lpszMenuName = NULL; 22 wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;//增加双击 23 24 RegisterClass(&wndclass); 25 26 hWnd = CreateWindow(Name_1, Name_2, 27 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 28 29 UpdateWindow(hWnd); 30 ShowWindow(hWnd, iCmdShow); 31 32 while (GetMessage(&msg, NULL, NULL, NULL)) 33 { 34 TranslateMessage(&msg); 35 DispatchMessage(&msg); 36 } 37 return msg.wParam; 38 } 39 40 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 41 { 42 PAINTSTRUCT ps; 43 HDC hdc; 44 RECT rect; 45 //LPCTSTR 转到定义 就是 *TCHAR 46 LPCTSTR pszFormat = TEXT("关闭按钮被按下 \n当前的鼠标坐标为(屏幕坐标):x->%d y->%d\n转换后鼠标坐标为(客户坐标):x->%d y->%d"); 47 POINT ptScreen, ptClient;//屏幕坐标和 客户坐标 48 TCHAR pszThe[800];//存放的BUFFER 49 50 switch (msg) 51 { 52 case WM_LBUTTONDOWN: 53 //MessageBox(hWnd, TEXT("这是单击"), TEXT("test"), MB_OK); 54 return 0; 55 case WM_LBUTTONDBLCLK: 56 MessageBox(hWnd, TEXT("6666"), TEXT("test"), MB_OK); 57 return 0; 58 case WM_NCLBUTTONDOWN://非客户区 单机 59 if(wParam & HTCLOSE)//判断是否在 关闭按钮上 60 { 61 ptScreen.x = GET_X_LPARAM(lParam);//获取鼠标坐标x 62 ptScreen.y = GET_Y_LPARAM(lParam);//获取鼠标坐标y 63 ptClient.x = ptScreen.x;//临时 屏幕坐标 64 ptClient.y = ptScreen.y;//同上 65 ScreenToClient(hWnd, &ptClient);//转换坐标 保存到 ptClient 66 wsprintf(pszThe, pszFormat, ptScreen.x, ptScreen.y, ptClient.x, ptClient.y);//格式化字符串 67 MessageBox(hWnd, pszThe, TEXT("test"), MB_OK); 68 } 69 //MessageBox(hWnd, TEXT("6666"), TEXT("test"), MB_OK); 70 return DefWindowProc(hWnd,msg,wParam,lParam);//其他地方的消息依旧默认执行。 71 case WM_PAINT: 72 hdc = BeginPaint(hWnd, &ps); 73 GetClientRect(hWnd, &rect); 74 DrawText(hdc, TEXT("兄弟 请双击~~~~"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); 75 76 EndPaint(hWnd, &ps); 77 return 0; 78 case WM_DESTROY: 79 PostQuitMessage(0); 80 return 0; 81 } 82 83 84 85 return DefWindowProc(hWnd, msg, wParam, lParam); 86 }
栗子3:
1 #include <windows.h> 2 #define DIVISIONS 5//宏定义 5 3 4 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 5 6 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 7 { 8 WNDCLASS wndclass; 9 MSG msg; 10 HWND hWnd; 11 TCHAR Name_1[] = TEXT("MyClass"), Name_2[] = TEXT("MyWindows"); 12 13 wndclass.cbClsExtra = 0; 14 wndclass.cbWndExtra = 0; 15 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 16 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 17 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 18 wndclass.hInstance = hInstance; 19 wndclass.lpfnWndProc = WndProc; 20 wndclass.lpszClassName = Name_1; 21 wndclass.lpszMenuName = NULL; 22 wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; 23 24 RegisterClass(&wndclass); 25 26 hWnd = CreateWindow(Name_1, Name_2, 27 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); 28 29 UpdateWindow(hWnd); 30 ShowWindow(hWnd, iCmdShow); 31 32 while (GetMessage(&msg, NULL, NULL, NULL)) 33 { 34 TranslateMessage(&msg); 35 DispatchMessage(&msg); 36 } 37 return msg.wParam; 38 } 39 40 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 41 { 42 static BOOL fState[DIVISIONS][DIVISIONS]; 43 static int cxBlock, cyBlock; 44 int x, y; 45 PAINTSTRUCT ps; 46 HDC hdc; 47 RECT rect; 48 switch (msg) 49 { 50 case WM_SIZE: 51 //当 窗口尺寸改变的时候 52 /* 53 cxBlock=窗口宽度/ 5 54 cyBlock=窗口高度/ 5 55 比如 宽度为 100 高度为 100 56 cxBlock=20 cyBlock=20 57 58 */ 59 cxBlock = LOWORD(lParam) / DIVISIONS; 60 cyBlock = HIWORD(lParam) / DIVISIONS; 61 return 0; 62 case WM_LBUTTONDOWN://鼠标左键单击 63 /* 64 x=x坐标/ 一个格子宽度 65 y=y坐标/ 一个格子高度 66 x=当前指向行的格子 y=当前指向列的格子 67 */ 68 x = LOWORD(lParam) / cxBlock; 69 y = HIWORD(lParam) / cyBlock; 70 71 //如果 都小于 最大格子 72 if (x < DIVISIONS && y < DIVISIONS) 73 { 74 //数组fState[当前行格子][当前列格子] 75 //意义为 本来保存的 BOOL 每次都取反 得到相反的值 76 fState[x][y] ^= 1; 77 78 /* 79 矩形的 左边=当前行格子*行格子宽度 80 矩形的 顶边=当前列格子*列格子高度 81 矩形的 右边=(当前行格子+1)*行格子宽度 82 矩形的 底边=(当前列格子+1)*列格子高度 83 */ 84 rect.left = x * cxBlock; 85 rect.top = y * cyBlock; 86 rect.right = (x + 1)*cxBlock; 87 rect.bottom = (y + 1)*cyBlock; 88 InvalidateRect(hWnd, &rect, FALSE);//重画矩形 89 } 90 else 91 { 92 MessageBeep(0); 93 } 94 return 0; 95 case WM_PAINT: 96 hdc = BeginPaint(hWnd, &ps); 97 98 //循环 x=0;x<最大 ;x++ 99 100 for (x = 0; x < DIVISIONS; x++) 101 { 102 //y=0;y<最大;y++ 103 for (y = 0; y < DIVISIONS; y++) 104 { 105 //构造矩形 106 /* 107 外部循环0次 里面循环5次 一共25次 25个方格 108 内部循环 5次(外部循环第1次 x=0): 109 构造1: 左:0*行格子宽度 顶:0*列格子高度 右:(0+1)*行格子宽度 底:(0+1)*列格子高度 110 构造2: 左:0*行格子宽度 顶:1*列格子高度 右:(0+1)*行格子宽度 底:(1+1)*列格子高度 111 构造3: 左:0*行格子宽度 顶:2*列格子高度 右:(0+1)*行格子宽度 底:(2+1)*列格子高度 112 构造4: 左:0*行格子宽度 顶:3*列格子高度 右:(0+1)*行格子宽度 底:(3+1)*列格子高度 113 构造5: 左:0*行格子宽度 顶:4*列格子高度 右:(0+1)*行格子宽度 底:(4+1)*列格子高度 114 口 115 口 116 口 117 口 118 口 119 =============================================================================== 120 内部循环 5次(外部循环第2次 x=1): 121 构造1: 左:1*行格子宽度 顶:0*列格子高度 右:(1+1)*行格子宽度 底:(0+1)*列格子高度 122 构造2: 左:1*行格子宽度 顶:1*列格子高度 右:(1+1)*行格子宽度 底:(1+1)*列格子高度 123 构造3: 左:1*行格子宽度 顶:2*列格子高度 右:(1+1)*行格子宽度 底:(2+1)*列格子高度 124 构造4: 左:1*行格子宽度 顶:3*列格子高度 右:(1+1)*行格子宽度 底:(3+1)*列格子高度 125 构造5: 左:1*行格子宽度 顶:4*列格子高度 右:(1+1)*行格子宽度 底:(4+1)*列格子高度 126 口口 127 口口 128 口口 129 口口 130 口口 131 */ 132 Rectangle(hdc, x*cxBlock, y*cyBlock, (x + 1)*cxBlock, (y + 1)*cyBlock);//这个处于某种原因 每次鼠标单击都要重绘 感觉效率不是很好 133 if (fState[x][y])//如果是标记了的 那么就画出来 134 { 135 //从 0*行格子宽度,0*列格子高度 画到 (0+1)*行格子高度,(0+1)*列格子高度 136 //从 左上角到 右下角 137 MoveToEx(hdc, x*cxBlock, y*cyBlock, NULL); 138 LineTo(hdc, (x + 1)*cxBlock, (y + 1)*cyBlock); 139 140 //从 0*行格子宽度,(0+1)*列格子高度 画到 (0+1)*行格子高度,0*列格子高度 141 //从 右上角到 左下角 142 MoveToEx(hdc, x*cxBlock, (y + 1)*cyBlock, NULL); 143 LineTo(hdc, (x + 1)*cxBlock, y*cyBlock); 144 } 145 } 146 } 147 148 EndPaint(hWnd, &ps); 149 return 0; 150 case WM_DESTROY: 151 PostQuitMessage(0); 152 return 0; 153 } 154 155 156 157 return DefWindowProc(hWnd, msg, wParam, lParam); 158 }
标签:
原文地址:http://www.cnblogs.com/summergull/p/5979013.html