7、 1 滑鼠 // 7-1-滑鼠.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "7-1-滑鼠.h" #define MAX_LOADSTRING 100 #define MAXPOINTS 1000 // 全局變數: HINSTANCE ...
7、 1 滑鼠
// 7-1-滑鼠.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "7-1-滑鼠.h" #define MAX_LOADSTRING 100 #define MAXPOINTS 1000 // 全局變數: HINSTANCE hInst; // 當前實例 WCHAR szTitle[MAX_LOADSTRING]; // 標題欄文本 WCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類名 // 此代碼模塊中包含的函數的前向聲明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此處放置代碼。 // 初始化全局字元串 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_MY71, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY71)); MSG msg; // 主消息迴圈: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函數: MyRegisterClass() // // 目標: 註冊視窗類。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY71)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY71); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex); } // // 函數: InitInstance(HINSTANCE, int) // // 目標: 保存實例句柄並創建主視窗 // // 註釋: // // 在此函數中,我們在全局變數中保存實例句柄並 // 創建和顯示主程式視窗。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 將實例句柄存儲在全局變數中 HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函數: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目標: 處理主視窗的消息。 // // WM_COMMAND - 處理應用程式菜單 // WM_PAINT - 繪製主視窗 // WM_DESTROY - 發送退出消息並返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; static POINT pt[MAXPOINTS]; static int iCount; switch (message) { case WM_COMMAND: { int wmId = LOWORD(wParam); // 分析菜單選擇: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_LBUTTONDOWN: iCount = 0; InvalidateRect(hWnd, NULL, true); return 0; case WM_MOUSEMOVE: if (wParam & MK_LBUTTON) { pt[iCount].x = LOWORD(lParam); pt[iCount++].y = HIWORD(lParam); hdc = GetDC(hWnd); SetPixel(hdc, LOWORD(lParam), HIWORD(lParam), 0); ReleaseDC(hWnd, hdc); } return 0; case WM_LBUTTONUP: InvalidateRect(hWnd, NULL, FALSE); return 0; case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); // TODO: 在此處添加使用 hdc 的任何繪圖代碼... SetCursor(LoadCursor(NULL,IDC_WAIT)); //設置滑鼠樣式 ShowCursor(TRUE); for (size_t i = 0; i < iCount - 1; i++) for (size_t j = i + 1; j < iCount; j++) { MoveToEx(hdc, pt[i].x, pt[i].y, NULL); LineTo(hdc, pt[j].x, pt[j].y); } SetCursor(FALSE); SetCursor(LoadCursor(NULL, IDC_ARROW)); EndPaint(hWnd, &ps); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // “關於”框的消息處理程式。 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }View Code
7、 2 滑鼠 示例:Checker
// 7-2-滑鼠Checker1.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "7-2-滑鼠Checker1.h" #define MAX_LOADSTRING 100 #define DIVISIONS 5 // 全局變數: HINSTANCE hInst; // 當前實例 WCHAR szTitle[MAX_LOADSTRING]; // 標題欄文本 WCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類名 // 此代碼模塊中包含的函數的前向聲明: ATOM MyRegisterClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此處放置代碼。 // 初始化全局字元串 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_MY72CHECKER1, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY72CHECKER1)); MSG msg; // 主消息迴圈: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函數: MyRegisterClass() // // 目標: 註冊視窗類。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY72CHECKER1)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY72CHECKER1); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex); } // // 函數: InitInstance(HINSTANCE, int) // // 目標: 保存實例句柄並創建主視窗 // // 註釋: // // 在此函數中,我們在全局變數中保存實例句柄並 // 創建和顯示主程式視窗。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 將實例句柄存儲在全局變數中 HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函數: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目標: 處理主視窗的消息。 // // WM_COMMAND - 處理應用程式菜單 // WM_PAINT - 繪製主視窗 // WM_DESTROY - 發送退出消息並返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static BOOL fState[DIVISIONS][DIVISIONS]; static int cxBlock, cyBlock; TCHAR szBuffer[10]; int x, y; //滑鼠左鍵點擊位置 RECT rect; PAINTSTRUCT ps; HDC hdc; POINT point; switch (message) { case WM_SIZE: cxBlock = LOWORD(lParam)/DIVISIONS; //5 方格的長度 cyBlock = HIWORD(lParam)/DIVISIONS; //5 方格的寬度 return 0; case WM_COMMAND: { int wmId = LOWORD(wParam); // 分析菜單選擇: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_KEYDOWN: GetCursorPos(&point); ScreenToClient(hWnd, &point); x = max(0, min(DIVISIONS, point.x / cxBlock)); y = max(0, min(DIVISIONS, point.y / cyBlock)); switch (wParam) { case VK_UP: y--; break; case VK_DOWN: y++; break; case VK_LEFT: x--; break; case VK_RIGHT: x++; break; case VK_HOME: x = y = 0; break; case VK_END: x = y = DIVISIONS - 1; break; case VK_RETURN: case VK_SPACE: SendMessage(hWnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELONG(x * cxBlock, cyBlock * y)); break; default: break; } x = (x + DIVISIONS) % DIVISIONS; y = (y + DIVISIONS) % DIVISIONS; point.x = x * cxBlock + cxBlock / 2; point.y = y * cyBlock + cyBlock / 2; ClientToScreen(hWnd, &point); SetCursorPos(point.x, point.y); return 0; case WM_LBUTTONDOWN: x = LOWORD(lParam)/cxBlock; y = HIWORD(lParam) / cyBlock; if (x < DIVISIONS && y < DIVISIONS) { fState[x][y] ^= 1; rect.left = x * cxBlock; rect.top = y * cyBlock; rect.right = (x + 1) * cxBlock; rect.bottom = (x + 1) * cyBlock; InvalidateRect(hWnd, &rect, FALSE); } else MessageBeep(0); return 0; case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); // TODO: 在此處添加使用 hdc 的任何繪圖代碼... for (size_t x = 0; x < DIVISIONS; x++) for (size_t y = 0; y < DIVISIONS; y++) { Rectangle(hdc, x * cxBlock, y * cyBlock, (x + 1)*cxBlock, (y + 1)*cyBlock); if (fState[x][y]) { _sntprintf(szBuffer, 10, TEXT("x,y,%d,%d"), x, y); MoveToEx(hdc, x * cxBlock, y * cyBlock, NULL); LineTo(hdc, (x + 1)*cxBlock, (y + 1)*cyBlock); MoveToEx(hdc, x *cxBlock, (y + 1)*cyBlock, NULL); LineTo(hdc, (x + 1)*cxBlock, y*cyBlock); SetTextColor(hdc, RGB(255, 0, 0)); TextOut(hdc, x * cxBlock+(cxBlock/2.5) , y * cyBlock +(cyBlock/4), szBuffer, lstrlen(szBuffer)); SetTextColor(hdc, RGB(0, 0, 0)); } } EndPaint(hWnd, &ps); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // “關於”框的消息處理程式。 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); switch (message) { case WM_INITDIALOG: return (INT_PTR)TRUE; case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } return (INT_PTR)FALSE; }View Code
7、 3 使用子視窗和鍵盤 創建 Checker
// 7-3-使用子視窗實現Checker1.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "7-3-使用子視窗實現Checker1.h" #define MAX_LOADSTRING 100 #define DIVISIONS 5 // 全局變數: HINSTANCE hInst; // 當前實例 WCHAR szTitle[MAX_LOADSTRING]; // 標題欄文本 WCHAR szWindowClass[MAX_LOADSTRING]; // 主視窗類名 TCHAR szChildClass[] = TEXT("Checker_Child"); //子視窗名稱 int idFocus = 0; // 此代碼模塊中包含的函數的前向聲明: ATOM MyRegisterClass(HINSTANCE hInstance); ATOM MyRegisterChildClass(HINSTANCE hInstance); BOOL InitInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //主視窗過程 LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);//子視窗過程 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM); int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // TODO: 在此處放置代碼。 // 初始化全局字元串 LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadStringW(hInstance, IDC_MY73CHECKER1, szWindowClass, MAX_LOADSTRING); //註冊主視窗 MyRegisterClass(hInstance); //註冊子視窗 MyRegisterChildClass(hInstance); // 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY73CHECKER1)); MSG msg; // 主消息迴圈: while (GetMessage(&msg, nullptr, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam; } // // 函數: MyRegisterClass() // // 目標: 註冊視窗類。 // ATOM MyRegisterClass(HINSTANCE hInstance) { WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY73CHECKER1)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY73CHECKER1); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex); } // // 函數: MyRegisterChildClass() // // 目標: 註冊子視窗類。 // ATOM MyRegisterChildClass(HINSTANCE hInstance) { WNDCLASSEXW wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = ChildWndProc; //子視窗過程 wcex.cbClsExtra = 0; wcex.cbWndExtra = sizeof(long); //保存子視窗數據 欄位 1 or 0 wcex.hInstance = hInstance; wcex.hIcon = NULL; //LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY73CHECKER1)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY73CHECKER1); wcex.lpszClassName = szChildClass; wcex.hIconSm = NULL; //LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassExW(&wcex); } // // 函數: InitInstance(HINSTANCE, int) // // 目標: 保存實例句柄並創建主視窗 // // 註釋: // // 在此函數中,我們在全局變數中保存實例句柄並 // 創建和顯示主程式視窗。 // BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) { hInst = hInstance; // 將實例句柄存儲在全局變數中 //主視窗、父視窗 HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); if (!hWnd) { return FALSE; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; } // // 函數: WndProc(HWND, UINT, WPARAM, LPARAM) // // 目標: 處理主視窗的消息。 // // WM_COMMAND - 處理應用程式菜單 // WM_PAINT - 繪製主視窗 // WM_DESTROY - 發送退出消息並返回 // // LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hwndChild[DIVISIONS][DIVISIONS]; PAINTSTRUCT ps; HDC hdc; int x, y,cxBlock, cyBlock; switch (message) { case WM_CREATE: //創建主視窗 //創建25個子視窗 for (x = 0; x < DIVISIONS; x++) for (y = 0; y < DIVISIONS; y++) hwndChild[x][y] = CreateWindowW(szChildClass, NULL, WS_CHILDWINDOW | WS_VISIBLE, 0, 0, 0, 0, hWnd, (HMENU)(y << 8 | x), hInst,NULL); ////主視窗、父視窗 //HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, // CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); return 0; case WM_SIZE: cxBlock = LOWORD(lParam) / DIVISIONS; cyBlock = HIWORD(lParam) / DIVISIONS; for (x = 0; x < DIVISIONS; x++) for (y = 0; y < DIVISIONS; y++) MoveWindow(hwndChild[x][y], x *cxBlock, y * cyBlock, cxBlock, cyBlock, TRUE); return 0; case WM_COMMAND: { int wmId = LOWORD(wParam); // 分析菜單選擇: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } } break; case WM_KEYDOWN: x = idFocus & 0xFF; y = idFocus >> 8; switch (wParam) { case VK_UP: y--; break; case VK_DOWN: y++; break; case VK_LEFT: x--; break; case VK_RIGHT: x++; break; case VK_HOME: x = y = 0; break; case VK_END: x = y = DIVISIONS - 1; break; default: break; } x = (x + DIVISIONS) % DIVISIONS; y = (y + DIVISIONS) % DIVISIONS; idFocus = y << 8 | x; SetFocus(GetDlgItem(hWnd, idFocus)); return 0; case WM_SETFOCUS: SetFocus(GetDlgItem(hWnd, idFocus)); return 0; case WM_PAINT: { hdc = BeginPaint(hWnd, &ps); // TODO: 在此處添加使用 hdc 的任何繪圖代碼... EndPaint(hWnd, &ps); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // // 函數: WndProc(HWND, UINT, WPARAM, LPARAM) // //