LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; int iMapMode; static int cxClient, cyClient; switch (message) { case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); //创建自定义映射 iMapMode = GetMapMode(hdc); SetMapMode(hdc, MM_ISOTROPIC); //各向同性(会去干预映射过程,将多余裁剪) //SetMapMode(hdc, MM_ANISOTROPIC); //各向异性(不会去干预,会自适应) SetWindowExtEx(hdc, 100, 100, NULL); //相当于取出原来逻辑窗口的100*100像素放大为现在视口的1/2*1/2(1/4) 所以现在视口所显示的最大尺寸为200*200 SetViewportExtEx(hdc, cxClient / 2, cyClient / 2, NULL); //设置视口原点在中心 SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL); //MM_ISOTROPIC: windows为了保证按比例将整个视口落在视图中,会将多余的尺寸排除在外,若是有背景,则多余为黑色,会以最大比例占据窗口(前提是和原来设置和视口和窗口之比不变的前提下) //MM_ANISOTROPIC:则不会,而是成比例拉伸 Rectangle(hdc, -100, -100, 100, 100); Ellipse(hdc, -80, -80, 80, 80); 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; int iMapMode; static int cxClient, cyClient; switch (message) { case WM_SIZE: cxClient = LOWORD(lParam); cyClient = HIWORD(lParam); break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); //创建自定义映射 iMapMode = GetMapMode(hdc); //SetMapMode(hdc, MM_ISOTROPIC); //各向同性(会去干预映射过程,将多余裁剪) SetMapMode(hdc, MM_ANISOTROPIC); //各向异性(不会去干预,会自适应) SetWindowExtEx(hdc, 100, 100, NULL); //相当于取出原来逻辑窗口的100*100像素放大为现在视口的1/2*1/2(1/4) 所以现在视口所显示的最大尺寸为200*200 //修改这里两个值得正负可以修改视口的x,y指向 //默认MM_TEXT x向右为正 y向下为正 SetViewportExtEx(hdc, cxClient / 2, cyClient / 2, NULL); //修改后 x向左为正 y向上为正 SetViewportExtEx(hdc, -cxClient / 2, -cyClient / 2, NULL); //.... //设置视口原点在中心 SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL); //MM_ISOTROPIC: windows为了保证按比例将整个视口落在视图中,会将多余的尺寸排除在外,若是有背景,则多余为黑色,会以最大比例占据窗口(前提是和原来设置和视口和窗口之比不变的前提下) //MM_ANISOTROPIC:则不会,而是成比例拉伸 Rectangle(hdc, -100, -100, 100, 100); Ellipse(hdc, -80, -80, 80, 80); //发现采用默认MM_TEXT方式 TextOut(hdc, -40, -40, L"Love", 4); TextOut(hdc, 40, 40, L"HAHA", 4); MoveToEx(hdc, 0, -100, NULL); LineTo(hdc, 0, 100); MoveToEx(hdc, -100, 0, NULL); LineTo(hdc, 100, 0); EndPaint(hwnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); }