LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hdc; PAINTSTRUCT ps; RECT rect; static int cxClient, cyClient; static int iMapMode; switch (message) { case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect);//视口和窗口 //逻辑坐标系统 ->窗口(程序员编码的) //设备坐标系统 -> 视口(眼睛看到的) //映射模式决定逻辑坐标的单位 //设备坐标的单位是像素 //在窗口下编程,在视口下显示 MoveToEx(hdc, cxClient / 2, 0, NULL); LineTo(hdc, cxClient / 2, cyClient); MoveToEx(hdc, 0, cyClient / 2, NULL); LineTo(hdc, cxClient, cyClient / 2); //获取旧的映射模式 iMapMode = GetMapMode(hdc); //设置新的 x向右 y向上 单位 0.1mm SetMapMode(hdc, MM_LOMETRIC); //设置视口 SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL); //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0) TextOut(hdc, 100, 100, L"ViewPort", 8); EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam);}
正弦函数练习
#define NUM 1000 #define PI 3.1415926 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hdc; PAINTSTRUCT ps; RECT rect; static int cxClient, cyClient; static int iMapMode; POINT apt[1000]; switch (message) { case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect);//视口和窗口 //逻辑坐标系统 ->窗口(程序员编码的) //设备坐标系统 -> 视口(眼睛看到的) //映射模式决定逻辑坐标的单位 //设备坐标的单位是像素 //在窗口下编程,在视口下显示 MoveToEx(hdc, 0, cyClient/2, NULL); LineTo(hdc, cxClient, cyClient/2); MoveToEx(hdc, 0, 0, NULL); LineTo(hdc, 0, cyClient); //获取旧的映射模式 iMapMode = GetMapMode(hdc); //设置视口 SetViewportOrgEx(hdc, 0, cyClient / 2, NULL); //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0) for (int i = 0; i < NUM;i++) { apt[i].x = cxClient / NUM*i; //单位不一致,需要测试 apt[i].y = -(int)(100 * sin(2 * PI*i / NUM)); } Polyline(hdc, apt, 1000); EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam);}
坐标转换
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ HDC hdc; PAINTSTRUCT ps; RECT rect; static int cxClient, cyClient; static int iMapMode; POINT apt[1000]; switch (message) { case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect);//视口和窗口 //逻辑坐标系统 ->窗口(程序员编码的) //设备坐标系统 -> 视口(眼睛看到的) //映射模式决定逻辑坐标的单位 //设备坐标的单位是像素 //在窗口下编程,在视口下显示 //设备坐标转换为逻辑坐标 DPtoLP //逻辑坐标转换为设备坐标 LPtoDP MoveToEx(hdc, 0, cyClient/2, NULL); LineTo(hdc, cxClient, cyClient/2); MoveToEx(hdc, 0, 0, NULL); LineTo(hdc, 0, cyClient); //获取旧的映射模式 iMapMode = GetMapMode(hdc); SetMapMode(hdc,MM_LOMETRIC); DPtoLP(hdc, &rect, 2); //句柄,POINT指针,点个数 rect是两个点,矩形左上,右下 //设置视口 SetViewportOrgEx(hdc, 0, cyClient / 2, NULL); //新的视口原点x,新的视口原点y,以及一个point指针用于存放原来视口位置(0,0) for (int i = 0; i < NUM;i++) { apt[i].x = rect.right*i / NUM; //单位不一致,需要测试 apt[i].y = (int)(rect.bottom/2 * sin(2 * PI*i / NUM)); } Polyline(hdc, apt, 1000); EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam);}