線上箭頭表示畫線的方向。WINDING模式和ALTERNATE模式都會填充三個封閉的L型區域,號碼從1到3。兩個更小的內部區域,號碼為4和5,在ALTERNATE模式下不被填充。但是在WINDING模式下,號碼5的區域會被填充,這是因為區域的內部到達圖形的外部必須穿過兩條相同方向的線。號碼為4的區域 ...
線上箭頭表示畫線的方向。WINDING模式和ALTERNATE模式都會填充三個封閉的L型區域,號碼從1到3。兩個更小的內部區域,號碼為4和5,在ALTERNATE模式下不被填充。但是在WINDING模式下,號碼5的區域會被填充,這是因為區域的內部到達圖形的外部必須穿過兩條相同方向的線。號碼為4的區域不會被填充,因為射線必須穿越兩條邊框線,但是這兩條邊框線的繪製方向相反。
1 /*------------------------------------------- 2 ALTWIND.C -- Alternate and Winding Fill Modes 3 (c) Charles Petzold, 1998 4 -------------------------------------------*/ 5 6 #include <Windows.h> 7 8 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 9 10 int WINAPI WinMain( __in HINSTANCE hInstance 11 , __in_opt HINSTANCE hPrevInstance 12 , __in LPSTR lpCmdLine 13 , __in int nShowCmd ) 14 { 15 static TCHAR szAppName[] = TEXT("AltWind"); 16 HWND hwnd; 17 MSG msg; 18 WNDCLASS wndclass; 19 20 wndclass.style = CS_HREDRAW | CS_VREDRAW; 21 wndclass.lpfnWndProc = WndProc; 22 wndclass.cbClsExtra = 0; 23 wndclass.cbWndExtra = 0; 24 wndclass.hInstance = hInstance; 25 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 26 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); 27 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 28 wndclass.lpszMenuName = NULL; 29 wndclass.lpszClassName = szAppName; 30 31 if (!RegisterClass(&wndclass)) 32 { 33 MessageBox(NULL, TEXT("Program requires Windows NT!") 34 , szAppName, MB_ICONERROR); 35 return 0; 36 } 37 38 hwnd= CreateWindow(szAppName, TEXT("Alternate and Winding Fill Modes") 39 , WS_OVERLAPPEDWINDOW 40 , CW_USEDEFAULT, CW_USEDEFAULT 41 , CW_USEDEFAULT, CW_USEDEFAULT 42 , NULL, NULL, hInstance, NULL); 43 44 ShowWindow(hwnd, nShowCmd); 45 UpdateWindow(hwnd); 46 47 while (GetMessage(&msg, NULL, 0, 0)) 48 { 49 TranslateMessage(&msg); 50 DispatchMessage(&msg); 51 } 52 53 return msg.wParam; 54 } 55 56 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 57 { 58 static POINT aptFigure[10] = {10, 70 59 , 50, 70 60 , 50, 10 61 , 90, 10 62 , 90, 50 63 , 30, 50 64 , 30, 90 65 , 70, 90 66 , 70, 30 67 , 10, 30}; 68 static int cxClient, cyClient; 69 HDC hdc; 70 int i; 71 PAINTSTRUCT ps; 72 POINT apt[10]; 73 74 switch (message) 75 { 76 case WM_SIZE: 77 cxClient = LOWORD(lParam); 78 cyClient = HIWORD(lParam); 79 return 0; 80 81 case WM_PAINT: 82 hdc = BeginPaint(hwnd, &ps); 83 SelectObject(hdc, GetStockObject(GRAY_BRUSH)); 84 85 for (i = 0; i !=10; ++i) 86 { 87 apt[i].x = cxClient * aptFigure[i].x / 200; 88 apt[i].y = cyClient * aptFigure[i].y / 100; 89 } 90 SetPolyFillMode(hdc, ALTERNATE); 91 Polygon(hdc, apt, 10); 92 93 for (i = 0; i != 10; ++i) 94 { 95 apt[i].x += cxClient / 2; 96 } 97 SetPolyFillMode(hdc, WINDING); 98 Polygon(hdc, apt, 10); 99 100 EndPaint(hwnd, &ps); 101 return 0; 102 103 case WM_DESTROY: 104 PostQuitMessage(0); 105 return 0; 106 } 107 108 return DefWindowProc(hwnd, message, wParam, lParam); 109 }ALTWIND.C
圖形的坐標(按一個100*100單位的區域設定)存儲在aptFigure數組中。這些坐標會根據客戶去的寬度和高度按比例縮放。程式顯示兩個圖形,一個使用ALTERNATE填充模式,另一個使用WINDING填充模式。結果如圖: