DEMO4_4.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:9k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo4_4 --- ROP2 demo */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include <math.h>
- 7 #include "demo4_4.h"
- 8
- 9
- 10 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 11 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 12
- 13 COLORREF MakeRgbColor(int);
- 14 void DrawGraph(HDC, int, int);
- 15
- 16 int Color1ID = IDM_RED1;
- 17 int Color2ID = IDM_BLUE2;
- 18 int ROP2ID = IDM_R2_13;
- 19
- 20 /****************************************************************/
- 21 /* WinMain() */
- 22 /****************************************************************/
- 23
- 24 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 25 LPSTR lpszCmdLine, int nCmdShow)
- 26 {
- 27 WNDCLASS wclass;
- 28 MSG msg;
- 29 HWND hWnd;
- 30 char szName[] = "Demo4_3";
- 31
- 32 if (!hPrevInstance)
- 33 {
- 34 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 35 wclass.lpfnWndProc = MainWndProc;
- 36 wclass.cbClsExtra = 0;
- 37 wclass.cbWndExtra = 0;
- 38 wclass.hInstance = hInstance;
- 39 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 40 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 41 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 42 wclass.lpszMenuName = szName;
- 43 wclass.lpszClassName = szName;
- 44
- 45 if (!RegisterClass (&wclass))
- 46 return (FALSE);
- 47 }
- 48
- 49 hWnd = CreateWindow(
- 50 szName,
- 51 "ROP2 demo" ,
- 52 WS_OVERLAPPEDWINDOW,
- 53 CW_USEDEFAULT,
- 54 CW_USEDEFAULT,
- 55 CW_USEDEFAULT,
- 56 CW_USEDEFAULT,
- 57 NULL,
- 58 NULL,
- 59 hInstance,
- 60 NULL );
- 61
- 62 if (!hWnd)
- 63 return (FALSE);
- 64
- 65 ShowWindow(hWnd, nCmdShow);
- 66 UpdateWindow(hWnd);
- 67
- 68 while (GetMessage(&msg, NULL, NULL,NULL))
- 69 {
- 70 TranslateMessage(&msg);
- 71 DispatchMessage(&msg);
- 72 }
- 73 return (msg.wParam);
- 74 }
- 75
- 76
- 77
- 78 /****************************************************************/
- 79 /* MainWndProc() */
- 80 /****************************************************************/
- 81
- 82 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 83 WORD wParam, LONG lParam)
- 84 {
- 85 HDC hDC;
- 86 HMENU hMenu;
- 87 PAINTSTRUCT ps;
- 88 static int CX, CY;
- 89
- 90 switch (message)
- 91 {
- 92 case WM_CREATE :
- 93 hMenu = GetMenu(hWnd);
- 94 CheckMenuItem(hMenu, IDM_RED1, MF_CHECKED);
- 95 CheckMenuItem(hMenu, IDM_BLUE2, MF_CHECKED);
- 96 CheckMenuItem(hMenu, IDM_R2_13, MF_CHECKED);
- 97 return (0);
- 98
- 99 case WM_COMMAND :
- 100 hMenu = GetMenu(hWnd);
- 101 switch (wParam)
- 102 {
- 103 case IDM_RED1 :
- 104 case IDM_GREEN1 :
- 105 case IDM_BLUE1 :
- 106 case IDM_YELLOW1 :
- 107 case IDM_CYAN1 :
- 108 case IDM_MAGENTA1 :
- 109 case IDM_WHITE1 :
- 110 case IDM_GRAY1 :
- 111 case IDM_BLACK1 :
- 112
- 113 if (Color1ID == wParam)
- 114 return (0);
- 115
- 116 CheckMenuItem(hMenu, Color1ID,
- 117 MF_UNCHECKED);
- 118 Color1ID = wParam;
- 119 CheckMenuItem(hMenu, Color1ID,
- 120 MF_CHECKED);
- 121
- 122 InvalidateRect(hWnd, NULL, TRUE);
- 123 break;
- 124
- 125 case IDM_RED2 :
- 126 case IDM_GREEN2 :
- 127 case IDM_BLUE2 :
- 128 case IDM_YELLOW2 :
- 129 case IDM_CYAN2 :
- 130 case IDM_MAGENTA2 :
- 131 case IDM_WHITE2 :
- 132 case IDM_GRAY2 :
- 133 case IDM_BLACK2 :
- 134
- 135 if (Color2ID == wParam)
- 136 return (0);
- 137
- 138 CheckMenuItem(hMenu, Color2ID,
- 139 MF_UNCHECKED);
- 140 Color2ID = wParam;
- 141 CheckMenuItem(hMenu, Color2ID,
- 142 MF_CHECKED);
- 143
- 144 InvalidateRect(hWnd, NULL, TRUE);
- 145 break;
- 146
- 147 case IDM_R2_1 :
- 148 case IDM_R2_2 :
- 149 case IDM_R2_3 :
- 150 case IDM_R2_4 :
- 151 case IDM_R2_5 :
- 152 case IDM_R2_6 :
- 153 case IDM_R2_7 :
- 154 case IDM_R2_8 :
- 155 case IDM_R2_9 :
- 156 case IDM_R2_10:
- 157 case IDM_R2_11:
- 158 case IDM_R2_12:
- 159 case IDM_R2_13:
- 160 case IDM_R2_14:
- 161 case IDM_R2_15:
- 162 case IDM_R2_16:
- 163
- 164 if (ROP2ID == wParam)
- 165 return (0);
- 166
- 167 CheckMenuItem(hMenu, ROP2ID,
- 168 MF_UNCHECKED);
- 169 ROP2ID = wParam;
- 170 CheckMenuItem(hMenu, ROP2ID,
- 171 MF_CHECKED);
- 172
- 173 InvalidateRect(hWnd, NULL, TRUE);
- 174 break;
- 175 }
- 176 return (0);
- 177
- 178 case WM_SIZE :
- 179 CX = LOWORD(lParam);
- 180 CY = HIWORD(lParam);
- 181
- 182 InvalidateRect(hWnd, NULL, TRUE);
- 183 return (0);
- 184
- 185 case WM_PAINT :
- 186 hDC = BeginPaint(hWnd, &ps);
- 187
- 188 DrawGraph(hDC, CX, CY);
- 189
- 190 EndPaint(hWnd, &ps);
- 191 return (0);
- 192
- 193 case WM_DESTROY :
- 194 PostQuitMessage(0);
- 195 return (0);
- 196
- 197 default :
- 198 return(DefWindowProc(hWnd, message, wParam, lParam));
- 199 }
- 200 }
- 201
- 202
- 203
- 204 COLORREF MakeRgbColor(int ColorID)
- 205 {
- 206 COLORREF crColor;
- 207
- 208 switch (ColorID)
- 209 {
- 210 case IDM_RED1 :
- 211 case IDM_RED2 :
- 212 crColor = RGB(255, 0, 0);
- 213 break;
- 214
- 215 case IDM_GREEN1 :
- 216 case IDM_GREEN2 :
- 217 crColor = RGB(0, 255, 0);
- 218 break;
- 219
- 220 case IDM_BLUE1 :
- 221 case IDM_BLUE2 :
- 222 crColor = RGB(0, 0, 255);
- 223 break;
- 224
- 225 case IDM_YELLOW1 :
- 226 case IDM_YELLOW2 :
- 227 crColor = RGB(255, 255, 0);
- 228 break;
- 229
- 230 case IDM_CYAN1 :
- 231 case IDM_CYAN2 :
- 232 crColor = RGB(0, 255, 255);
- 233 break;
- 234
- 235 case IDM_MAGENTA1 :
- 236 case IDM_MAGENTA2 :
- 237 crColor = RGB(255, 0, 255);
- 238 break;
- 239
- 240 case IDM_WHITE1 :
- 241 case IDM_WHITE2 :
- 242 crColor = RGB(255, 255, 255);
- 243 break;
- 244
- 245 case IDM_GRAY1 :
- 246 case IDM_GRAY2 :
- 247 crColor = RGB(150, 150, 150);
- 248 break;
- 249
- 250 case IDM_BLACK1 :
- 251 case IDM_BLACK2 :
- 252 crColor = RGB(0, 0, 0);
- 253 break;
- 254 }
- 255
- 256 return (crColor);
- 257 }
- 258
- 259
- 260
- 261 void DrawGraph(HDC hDC, int CX, int CY)
- 262 {
- 263 HBRUSH hPreBrush;
- 264 HBRUSH hBrush1, hBrush2;
- 265 COLORREF crColor;
- 266
- 267 crColor = MakeRgbColor(Color1ID);
- 268 hBrush1 = CreateSolidBrush(crColor);
- 269 crColor = MakeRgbColor(Color2ID);
- 270 hBrush2 = CreateSolidBrush(crColor);
- 271
- 272 hPreBrush = SelectObject(hDC, hBrush2);
- 273 Rectangle(hDC, CX/3, CY/3, CX, CY);
- 274
- 275 SetROP2(hDC, ROP2ID-IDM_R2_1+1);
- 276
- 277 SelectObject(hDC, hBrush1);
- 278 Rectangle(hDC, 0, 0, CX*2/3, CY*2/3);
- 279
- 280 SelectObject(hDC, hPreBrush);
- 281 DeleteObject(hBrush1);
- 282 DeleteObject(hBrush2);
- 283 }