1 #include <Windows.h> 2 #include <tchar.h> 3 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow); 4 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 5 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 6 { 7 MSG msg; 8 if (!InitWindowClass(hInstance, nCmdShow)) 9 { 10 MessageBox(NULL, L"創建窗口失敗!", _T("創建窗口"), NULL); 11 return 1; 12 } 13 while (GetMessage(&msg, NULL, 0, 0)) 14 { 15 TranslateMessage(&msg); 16 DispatchMessage(&msg); 17 } 18 19 return(int)msg.wParam; 20 } 21 22 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 23 { 24 HDC hDC; 25 PAINTSTRUCT PtStr; 26 HBRUSH hBrush; 27 HPEN hPen; 28 static int dispMode = -1; 29 LPCTSTR str; 30 switch (message) 31 { 32 case WM_LBUTTONDOWN: 33 InvalidateRect(hWnd, NULL, TRUE); 34 break; 35 case WM_PAINT: 36 hDC = BeginPaint(hWnd, &PtStr); 37 dispMode = (dispMode + 1) % 6; 38 switch (dispMode) 39 { 40 case 0: 41 str = _T("映射方式MM_TEXT:缺省的映射方式"); 42 SetMapMode(hDC, MM_TEXT); //設置映射方式為缺省方式 43 TextOut(hDC, 0, 0, str, _tcsclen(str)); //輸出映射方式及映射比例 44 break; 45 case 1: 46 str = _T("映射方式MM_ISOTROPIC:窗口坐標為20×20,映射為視口尺寸為10×10,圖形縮小1倍"); 47 SetMapMode(hDC, MM_ISOTROPIC); //設置映射方式 48 SetWindowExtEx(hDC, 20, 20, NULL); //窗口矩形為×20 49 SetViewportExtEx(hDC, 10, 10, NULL); //映射為視口的矩形為×10 50 TextOut(hDC, 0, 0, str, _tcsclen(str)); 51 break; 52 case 2: 53 str = _T("映射方式MM_ISOTROPIC:窗口坐標為10×10,映射為視口尺寸為20×20,圖形放大1倍"); 54 SetMapMode(hDC, MM_ISOTROPIC); 55 SetWindowExtEx(hDC, 10, 10, NULL); //窗口矩形為10×10 56 SetViewportExtEx(hDC, 20, 20, NULL); //映射為視口的矩形為20×20 57 TextOut(hDC, 0, 0, str, _tcsclen(str)); 58 break; 59 case 3: 60 str = _T("映射方式MM_ANISOTROPIC:窗口坐標為10×10,映射為視口尺寸為20×10,圖形橫向放大1倍,縱向不變"); 61 SetMapMode(hDC, MM_ANISOTROPIC); 62 SetWindowExtEx(hDC, 10, 10, NULL); //窗口矩形為10×10 63 SetViewportExtEx(hDC, 20, 10, NULL); //映射為視口的矩形為20×10 64 TextOut(hDC, 0, 0, str, _tcsclen(str)); 65 break; 66 case 4: 67 str = _T("映射方式MM_ANISOTROPIC:窗口坐標為10×10,映射為視口尺寸為20×5,圖形橫向放大1倍,縱向縮小1倍"); 68 SetMapMode(hDC, MM_ANISOTROPIC); 69 SetWindowExtEx(hDC, 10, 10, NULL); //窗口矩形為10×10 70 SetViewportExtEx(hDC, 20, 5, NULL); //映射為視口的矩形為20×5 71 TextOut(hDC, 0, 0, str, _tcsclen(str)); 72 break; 73 case 5: 74 str = _T("映射方式MM_ISOTROPIC:窗口坐標為10×10,映射為視口尺寸為20×5,圖形為了保持原縱橫比,系統會調整映射比例"); 75 SetMapMode(hDC, MM_ISOTROPIC); 76 SetWindowExtEx(hDC, 10, 10, NULL); //窗口矩形為10×10 77 SetViewportExtEx(hDC, 20, 5, NULL); //映射為視口的矩形為20×5 78 TextOut(hDC, 0, 0, str, _tcsclen(str)); 79 break; 80 } 81 hPen = (HPEN)GetStockObject(BLACK_PEN); //設置畫筆為系統預定義的黑色畫筆 82 hBrush = (HBRUSH)GetStockObject(DKGRAY_BRUSH); //深灰色畫刷 83 SelectObject(hDC, hBrush); //選擇畫刷 84 SelectObject(hDC, hPen); //選擇畫筆 85 RoundRect(hDC, 50, 120, 100, 200, 15, 15); //圓角矩形 86 hBrush = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //淡灰色畫刷 87 SelectObject(hDC, hBrush); //選擇畫刷 88 Ellipse(hDC, 150, 50, 200, 150); //橢圓 89 hBrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH); //采用系統預定義的虛畫刷 90 SelectObject(hDC, hBrush); //選擇畫刷 91 Pie(hDC, 250, 50, 300, 100, 250, 50, 300, 50); //餅形 92 EndPaint(hWnd, &PtStr); //結束繪畫 93 break; 94 case WM_DESTROY: 95 PostQuitMessage(0); //調用PostQuitMessage發出WM_QUIT消息 96 break; 97 default: 98 return DefWindowProc(hWnd, message, wParam, lParam);//默認時采用系統消息默認處理函數 99 break; 100 } 101 return 0; 102 } 103 104 BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow) 105 { 106 WNDCLASSEX wcex; 107 HWND hWnd; 108 TCHAR szWindowClass[] = L"窗口示例"; 109 TCHAR szTitle[] = L"映射模式及填充示例圖"; 110 wcex.cbSize = sizeof(WNDCLASSEX); 111 wcex.style = 0; 112 wcex.lpfnWndProc = WndProc; 113 wcex.cbClsExtra = 0; 114 wcex.cbWndExtra = 0; 115 wcex.hInstance = hInstance; 116 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); 117 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); 118 wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 119 wcex.lpszMenuName = NULL; 120 wcex.lpszClassName = szWindowClass; 121 wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); 122 if (!RegisterClassEx(&wcex)) 123 return FALSE; 124 hWnd = CreateWindow( 125 szWindowClass, 126 szTitle, 127 WS_OVERLAPPEDWINDOW, 128 CW_USEDEFAULT, CW_USEDEFAULT, 129 CW_USEDEFAULT, CW_USEDEFAULT, 130 NULL, 131 NULL, 132 hInstance, 133 NULL 134 ); 135 if (!hWnd) 136 return FALSE; 137 ShowWindow(hWnd, nCmdShow); 138 UpdateWindow(hWnd); 139 return TRUE; 140 }