update for HEAD-2003091401
[reactos.git] / apps / tests / lineclip / lineclip.c
1 #include <windows.h>
2 #include <stdio.h>
3
4 LRESULT WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
5
6 int WINAPI 
7 WinMain(HINSTANCE hInstance,
8         HINSTANCE hPrevInstance,
9         LPSTR lpszCmdLine,
10         int nCmdShow)
11 {
12   WNDCLASS wc;
13   MSG msg;
14   HWND hWnd;
15
16   wc.lpszClassName = "ClipClass";
17   wc.lpfnWndProc = MainWndProc;
18   wc.style = CS_VREDRAW | CS_HREDRAW;
19   wc.hInstance = hInstance;
20   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
21   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
22   wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
23   wc.lpszMenuName = NULL;
24   wc.cbClsExtra = 0;
25   wc.cbWndExtra = 0;
26   if (RegisterClass(&wc) == 0)
27     {
28       fprintf(stderr, "RegisterClass failed (last error 0x%X)\n",
29               GetLastError());
30       return(1);
31     }
32
33   hWnd = CreateWindow("ClipClass",
34                       "Line clipping test",
35                       WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,
36                       0,
37                       0,
38                       CW_USEDEFAULT,
39                       CW_USEDEFAULT,
40                       NULL,
41                       NULL,
42                       hInstance,
43                       NULL);
44   if (hWnd == NULL)
45     {
46       fprintf(stderr, "CreateWindow failed (last error 0x%X)\n",
47               GetLastError());
48       return(1);
49     }
50
51   ShowWindow(hWnd, nCmdShow);
52
53   while(GetMessage(&msg, NULL, 0, 0))
54   {
55     TranslateMessage(&msg);
56     DispatchMessage(&msg);
57   }
58
59   return msg.wParam;
60 }
61
62 static void
63 DrawLines(HDC hDC)
64 {
65   static struct
66     {
67     int fromx;
68     int fromy;
69     int tox;
70     int toy;
71     }
72   points[ ] =
73     {
74       {  50,  99, 125,  99 },
75       { 160,  99, 190,  99 },
76       { 300,  99, 225,  99 },
77       {  50, 100, 125, 100 },
78       { 160, 100, 190, 100 },
79       { 300, 100, 225, 100 },
80       {  50, 125, 300, 125 },
81       {  50, 149, 125, 149 },
82       { 160, 149, 190, 149 },
83       { 300, 149, 225, 149 },
84       {  50, 150, 125, 150 },
85       { 160, 150, 190, 150 },
86       { 300, 150, 225, 150 },
87       { 160, 249, 190, 249 },
88       { 160, 250, 190, 250 },
89       { 149,  50, 149, 125 },
90       { 149, 160, 149, 190 },
91       { 149, 300, 149, 225 },
92       { 150,  50, 150, 125 },
93       { 150, 160, 150, 190 },
94       { 150, 300, 150, 225 },
95       { 199,  50, 199, 125 },
96       { 199, 160, 199, 190 },
97       { 199, 300, 199, 225 },
98       { 200,  50, 200, 125 },
99       { 200, 160, 200, 190 },
100       { 200, 300, 200, 225 },
101       { 175,  50, 175, 300 },
102       {  50,  55, 300, 290 },
103       { 300, 295,  50,  60 },
104       {  50, 290, 300,  55 },
105       { 300,  60,  50, 295 },
106       {  55,  50, 290, 300 },
107       { 295, 300,  60,  50 },
108       {  55, 300, 290,  50 },
109       { 295,  50,  60, 300 }
110     };
111   int i;
112
113   for (i = 0; i < sizeof(points) / sizeof(points[0]); i++)
114     {
115       MoveToEx(hDC, points[i].fromx, points[i].fromy, NULL);
116       LineTo(hDC, points[i].tox, points[i].toy);
117     }
118 }
119
120 LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
121 {
122         PAINTSTRUCT ps;
123         HDC hDC;
124         RECT clr, wir;
125         HRGN ClipRgn, ExcludeRgn;
126         char spr[100], sir[100];
127         RECT Rect;
128
129         switch(msg)
130         {
131         case WM_PAINT:
132           GetClientRect(hWnd, &clr);
133           ClipRgn = CreateRectRgnIndirect(&clr);
134           hDC = BeginPaint(hWnd, &ps);
135           Rect.left = 100;
136           Rect.top = 100;
137           Rect.right = 250;
138           Rect.bottom = 150;
139           FillRect(hDC, &Rect, CreateSolidBrush(RGB(0xFF, 0x00, 0x00)));
140           ExcludeRgn = CreateRectRgnIndirect(&Rect);
141           CombineRgn(ClipRgn, ClipRgn, ExcludeRgn, RGN_DIFF);
142           DeleteObject(ExcludeRgn);
143           Rect.left = 150;
144           Rect.top = 150;
145           Rect.right = 200;
146           Rect.bottom = 250;
147           FillRect(hDC, &Rect, CreateSolidBrush(RGB(0xFF, 0x00, 0x00)));
148           SelectObject(hDC, CreatePen(PS_SOLID, 0, RGB(0xFF, 0xFF, 0x00)));
149           DrawLines(hDC);
150           SelectObject(hDC, CreatePen(PS_SOLID, 0, RGB(0x00, 0x00, 0xFF)));
151           ExcludeRgn = CreateRectRgnIndirect(&Rect);
152           CombineRgn(ClipRgn, ClipRgn, ExcludeRgn, RGN_DIFF);
153           DeleteObject(ExcludeRgn);
154           SelectClipRgn(hDC, ClipRgn);
155           DrawLines(hDC);
156           EndPaint(hWnd, &ps);
157           break;
158
159         case WM_DESTROY:
160           PostQuitMessage(0);
161           break;
162
163         default:
164           return DefWindowProc(hWnd, msg, wParam, lParam);
165         }
166         return 0;
167 }