5、1 設定像素 設定像素一般用四個位元組,最高位省略不用。 // 5-1-PWCP_設定像素_顏色.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "5-1-PWCP_設定像素_顏色.h" #define MAX_LOADSTRING 100 ...
5、1 設定像素
設定像素一般用四個位元組,最高位省略不用。
// 5-1-PWCP_設定像素_顏色.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "5-1-PWCP_設定像素_顏色.h" #define MAX_LOADSTRING 100 // 全局變數: 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_MY51PWCP, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY51PWCP)); 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_MY51PWCP)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;// MAKEINTRESOURCEW(IDC_MY51PWCP); 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) { size_t red, green, blue; TCHAR szBuffer[1024]; 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_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // TODO: 在此處添加使用 hdc 的任何繪圖代碼... COLORREF color; color = RGB(255, 0, 0); //賦值顏色 for (size_t i = 0; i < 100; i++) //設置像素點顏色 SetPixel(hdc, 100+i, 100, color); RECT rect; GetClientRect(hWnd, &rect); for (size_t i = rect.left; i <= rect.right; i++) for (size_t j = rect.top; j <= rect.bottom; j++) { color = RGB(255, rand() % 256, rand() % 256); SetPixel(hdc, i, j, color); } //獲取像素點顏色 color = GetPixel(hdc, 100, 100); red = GetRValue(color); green = GetGValue(color); blue = GetBValue(color); wsprintf(szBuffer, TEXT("x=100,y= 100的像素點:R = %dpx, G= %dpx, B = %dpx"), red, green, blue); TextOut(hdc, 1, 1, szBuffer, lstrlen(szBuffer)); 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; }
5、2 畫直線
// 5-2-PWCP_直線.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "5-2-PWCP_直線.h" #define MAX_LOADSTRING 100 // 全局變數: 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_MY52PWCP, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY52PWCP)); 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_MY52PWCP)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY52PWCP); 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) { 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_PAINT: { PAINTSTRUCT ps; RECT rect; POINT apt[] = { 300,100,400,100,400,200,300,200,300,100 }; POINT apt2[] = { 500,100,600,100,600,200,500,200,500,100 }; POINT apt3[] = { 700,100,800,100,800,200,700,200,700,100 }; HDC hdc = BeginPaint(hWnd, &ps); // TODO: 在此處添加使用 hdc 的任何繪圖代碼... //畫矩形框 MoveToEx(hdc, 100, 100, NULL); //移動畫筆的位置 LineTo(hdc, 200, 100); //畫完線後自動改變畫筆的位置 LineTo(hdc, 200, 200); LineTo(hdc, 100, 200); LineTo(hdc, 100, 100); //迴圈畫 MoveToEx(hdc, apt[0].x, apt[0].y, NULL); for (size_t i = 1; i < 5; i++) LineTo(hdc, apt[i].x, apt[i].y); //polyline 函數畫 Polyline(hdc, apt2, 5); //polylineTo 函數畫 MoveToEx(hdc, apt3[0].x, apt3[0].y, NULL); PolylineTo(hdc,apt3+1,4); //畫表格 /*GetClientRect(hWnd, &rect); for (size_t x = 0; x <= rect.right; x+=100) { MoveToEx(hdc, x, 0, NULL); LineTo(hdc, x, rect.bottom); } for (size_t i = 0; i < rect.bottom; i+=100) { MoveToEx(hdc, 0, i, NULL); LineTo(hdc, rect.right, i); }*/ 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; }
5、3 SINEWAVE
// 5-3-PWCP_直線_SINEWAVE.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "5-3-PWCP_直線_SINEWAVE.h" #include <math.h> #define MAX_LOADSTRING 100 #define NUM 1000 #define TWOPI (2 * 3.1415926) // 全局變數: 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_MY53PWCPSINEWAVE, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY53PWCPSINEWAVE)); 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_MY53PWCPSINEWAVE)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;// MAKEINTRESOURCEW(IDC_MY53PWCPSINEWAVE); 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, TEXT("SINEWAVE"), 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 int cxClient, cyClient; POINT apt[NUM]; switch (message) { case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); 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_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); HPEN hPen = CreatePen(PS_DASH, 2, RGB(255, 0, 0)); HPEN hOldPen = (HPEN)SelectObject(hdc, hPen); MoveToEx(hdc, 0, cyClient / 2, NULL); LineTo(hdc, cxClient, cyClient / 2); for (size_t i = 0; i < NUM; i++) { apt[i].x =i * cxClient / NUM; apt[i].y = (int)(cyClient / 2 * (1 - sin(TWOPI * i / NUM))); //SetPixel(hdc, apt[i].x, apt[i].y, RGB(255, 0, 0)); } Polyline(hdc, apt, NUM); // TODO: 在此處添加使用 hdc 的任何繪圖代碼... 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
5、 4 邊框繪製函數
// 5-4-PWCP_邊框繪製函數.cpp : 定義應用程式的入口點。 // #include "framework.h" #include "5-4-PWCP_邊框繪製函數.h" #define MAX_LOADSTRING 100 // 全局變數: 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_MY54PWCP, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); // 執行應用程式初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY54PWCP)); 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_MY54PWCP)); wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL;//MAKEINTRESOURCEW(IDC_MY54PWCP); 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 int cxClient, cyClient; switch (message) { case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); return 0; case WM_COMMAND: { int wmId = LOWORD(wParam); // 分析菜單選擇: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break