DEMO5_1.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:11k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo5_1 --- Create palettes */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "Demo5_1.h"
- 7
- 8 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 9 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 10
- 11 HPALETTE MakePalette(int);
- 12 HPALETTE MakeRedPalette();
- 13 HPALETTE MakeGreenPalette();
- 14 HPALETTE MakeBluePalette();
- 15 HPALETTE MakeSysPalette();
- 16 void DrawGraph(HWND, HDC);
- 17
- 18 int PalTypeID = IDM_RED;
- 19 int ColorNum;
- 20 NPLOGPALETTE lpLP;
- 21
- 22 /****************************************************************/
- 23 /* WinMain() */
- 24 /****************************************************************/
- 25
- 26 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 27 LPSTR lpszCmdLine, int nCmdShow)
- 28 {
- 29 WNDCLASS wclass;
- 30 MSG msg;
- 31 HWND hWnd;
- 32 char szName[] = "Demo5_1";
- 33
- 34 if (!hPrevInstance)
- 35 {
- 36 wclass.style = CS_HREDRAW | CS_VREDRAW;
- 37 wclass.lpfnWndProc = MainWndProc;
- 38 wclass.cbClsExtra = 0;
- 39 wclass.cbWndExtra = 0;
- 40 wclass.hInstance = hInstance;
- 41 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 42 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 43 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 44 wclass.lpszMenuName = szName;
- 45 wclass.lpszClassName = szName;
- 46
- 47 if (!RegisterClass (&wclass))
- 48 return (FALSE);
- 49 }
- 50
- 51 hWnd = CreateWindow(
- 52 szName,
- 53 "Palette" ,
- 54 WS_OVERLAPPEDWINDOW,
- 55 CW_USEDEFAULT,
- 56 CW_USEDEFAULT,
- 57 CW_USEDEFAULT,
- 58 CW_USEDEFAULT,
- 59 NULL,
- 60 NULL,
- 61 hInstance,
- 62 NULL );
- 63
- 64 if (!hWnd)
- 65 return (FALSE);
- 66
- 67 ShowWindow(hWnd, nCmdShow);
- 68 UpdateWindow(hWnd);
- 69
- 70 while (GetMessage(&msg, NULL, NULL,NULL))
- 71 {
- 72 TranslateMessage(&msg);
- 73 DispatchMessage(&msg);
- 74 }
- 75 return (msg.wParam);
- 76 }
- 77
- 78
- 79
- 80 /****************************************************************/
- 81 /* MainWndProc() */
- 82 /****************************************************************/
- 83
- 84 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 85 WORD wParam, LONG lParam)
- 86 {
- 87 HDC hDC;
- 88 HMENU hMenu;
- 89 PAINTSTRUCT ps;
- 90 int nRasterCaps;
- 91 DWORD dwSize;
- 92 static HPALETTE hPalette;
- 93
- 94 switch (message)
- 95 {
- 96 case WM_CREATE :
- 97 hMenu = GetMenu(hWnd);
- 98 CheckMenuItem(hMenu, IDM_RED, MF_CHECKED);
- 99
- 100 hDC = GetDC(hWnd);
- 101 nRasterCaps = GetDeviceCaps(hDC, RASTERCAPS);
- 102 nRasterCaps = (nRasterCaps & RC_PALETTE) ?
- 103 TRUE : FALSE;
- 104
- 105 if (nRasterCaps)
- 106 ColorNum = GetDeviceCaps(hDC, SIZEPALETTE);
- 107 else
- 108 ColorNum = GetDeviceCaps(hDC, NUMCOLORS);
- 109
- 110 ReleaseDC(hWnd, hDC);
- 111
- 112 dwSize = sizeof(LOGPALETTE) +
- 113 ColorNum*sizeof(PALETTEENTRY);
- 114 lpLP = (NPLOGPALETTE)
- 115 LocalAlloc(LMEM_FIXED, dwSize);
- 116
- 117 return (0);
- 118
- 119 case WM_COMMAND :
- 120 switch (wParam)
- 121 {
- 122 case IDM_RED :
- 123 case IDM_GREEN :
- 124 case IDM_BLUE :
- 125 case IDM_SYS :
- 126 case IDM_DEF :
- 127
- 128 if (PalTypeID == wParam)
- 129 return (0);
- 130
- 131 hMenu = GetMenu(hWnd);
- 132 CheckMenuItem(hMenu, PalTypeID,
- 133 MF_UNCHECKED);
- 134 PalTypeID = wParam;
- 135 CheckMenuItem(hMenu, PalTypeID,
- 136 MF_CHECKED);
- 137
- 138 InvalidateRect(hWnd, NULL, TRUE);
- 139 break;
- 140
- 141 case IDM_REDRAW :
- 142 InvalidateRect(hWnd, NULL, TRUE);
- 143 break;
- 144
- 145 case IDM_EXIT :
- 146 DestroyWindow(hWnd);
- 147 break;
- 148 }
- 149 return (0);
- 150
- 151 case WM_SETFOCUS :
- 152
- 153 case WM_PAINT :
- 154 hDC = BeginPaint(hWnd, &ps);
- 155
- 156 if (PalTypeID != IDM_DEF)
- 157 {
- 158 hPalette = MakePalette(PalTypeID);
- 159 SelectPalette(hDC, hPalette, FALSE);
- 160 RealizePalette(hDC);
- 161 }
- 162
- 163 DrawGraph(hWnd, hDC);
- 164 EndPaint(hWnd, &ps);
- 165
- 166 if (PalTypeID != IDM_DEF)
- 167 DeleteObject(hPalette);
- 168 return (0);
- 169
- 170 case WM_DESTROY :
- 171 PostQuitMessage(0);
- 172 return (0);
- 173
- 174 default :
- 175 return(DefWindowProc(hWnd, message, wParam, lParam));
- 176 }
- 177 }
- 178
- 179
- 180
- 181 HPALETTE MakePalette(int nIndex)
- 182 {
- 183 HPALETTE hPalette;
- 184
- 185 switch (nIndex)
- 186 {
- 187 case IDM_RED :
- 188 hPalette = MakeRedPalette();
- 189 break;
- 190
- 191 case IDM_GREEN :
- 192 hPalette = MakeGreenPalette();
- 193 break;
- 194
- 195 case IDM_BLUE :
- 196 hPalette = MakeBluePalette();
- 197 break;
- 198
- 199 case IDM_SYS :
- 200 hPalette = MakeSysPalette();
- 201 break;
- 202 }
- 203
- 204 return (hPalette);
- 205 }
- 206
- 207
- 208
- 209 HPALETTE MakeRedPalette()
- 210 {
- 211 HPALETTE hPalette;
- 212 int i;
- 213
- 214 lpLP->palVersion = 0x300;
- 215 lpLP->palNumEntries = ColorNum;
- 216 for (i=0; i<ColorNum/2; i++)
- 217 {
- 218 lpLP->palPalEntry[i].peRed = (long)
- 219 i*255/(ColorNum/2);
- 220 lpLP->palPalEntry[i].peGreen = 0;
- 221 lpLP->palPalEntry[i].peBlue = 0;
- 222 lpLP->palPalEntry[i].peFlags = 0;
- 223 }
- 224
- 225 for (i=ColorNum/2; i<ColorNum; i++)
- 226 {
- 227 lpLP->palPalEntry[i].peRed = 255;
- 228 lpLP->palPalEntry[i].peGreen = (long)
- 229 255*(i-ColorNum/2)/(ColorNum/2);
- 230 lpLP->palPalEntry[i].peBlue = (long)
- 231 255*(i-ColorNum/2)/(ColorNum/2);
- 232 lpLP->palPalEntry[i].peFlags = 0;
- 233 }
- 234
- 235 hPalette = CreatePalette(lpLP);
- 236 return (hPalette);
- 237 }
- 238
- 239
- 240
- 241 HPALETTE MakeGreenPalette()
- 242 {
- 243 HPALETTE hPalette;
- 244 int i;
- 245
- 246 lpLP->palVersion = 0x300;
- 247 lpLP->palNumEntries = ColorNum;
- 248 for (i=0; i<ColorNum/2; i++)
- 249 {
- 250 lpLP->palPalEntry[i].peRed = 0;
- 251 lpLP->palPalEntry[i].peGreen = (long)
- 252 i*255/(ColorNum/2);
- 253 lpLP->palPalEntry[i].peBlue = 0;
- 254 lpLP->palPalEntry[i].peFlags = 0;
- 255 }
- 256
- 257 for (i=ColorNum/2; i<ColorNum; i++)
- 258 {
- 259 lpLP->palPalEntry[i].peRed = (long)
- 260 255*(i-ColorNum/2)/(ColorNum/2);
- 261 lpLP->palPalEntry[i].peGreen = 255;
- 262 lpLP->palPalEntry[i].peBlue = (long)
- 263 255*(i-ColorNum/2)/(ColorNum/2);
- 264 lpLP->palPalEntry[i].peFlags = 0;
- 265 }
- 266
- 267 hPalette = CreatePalette(lpLP);
- 268 return (hPalette);
- 269 }
- 270
- 271
- 272
- 273 HPALETTE MakeBluePalette()
- 274 {
- 275 HPALETTE hPalette;
- 276 int i;
- 277
- 278 lpLP->palVersion = 0x300;
- 279 lpLP->palNumEntries = ColorNum;
- 280 for (i=0; i<ColorNum/2; i++)
- 281 {
- 282 lpLP->palPalEntry[i].peRed = 0;
- 283 lpLP->palPalEntry[i].peGreen = 0;
- 284 lpLP->palPalEntry[i].peBlue = (long)
- 285 i*255/(ColorNum/2);
- 286 lpLP->palPalEntry[i].peFlags = 0;
- 287 }
- 288
- 289 for (i=ColorNum/2; i<ColorNum; i++)
- 290 {
- 291 lpLP->palPalEntry[i].peRed = (long)
- 292 255*(i-ColorNum/2)/(ColorNum/2);
- 293 lpLP->palPalEntry[i].peGreen = (long)
- 294 255*(i-ColorNum/2)/(ColorNum/2);
- 295 lpLP->palPalEntry[i].peBlue = 255;
- 296 lpLP->palPalEntry[i].peFlags = 0;
- 297 }
- 298
- 299 hPalette = CreatePalette(lpLP);
- 300 return (hPalette);
- 301 }
- 302
- 303
- 304
- 305 HPALETTE MakeSysPalette()
- 306 {
- 307 HPALETTE hPalette;
- 308 int i;
- 309
- 310 lpLP->palVersion = 0x300;
- 311 lpLP->palNumEntries = ColorNum;
- 312 for (i=0; i<ColorNum; i++)
- 313 {
- 314 *((WORD *) (&lpLP->palPalEntry[i].peRed)) = i;
- 315 lpLP->palPalEntry[i].peBlue = 0;
- 316 lpLP->palPalEntry[i].peFlags = PC_EXPLICIT;
- 317 }
- 318
- 319 hPalette = CreatePalette(lpLP);
- 320 return (hPalette);
- 321 }
- 322
- 323
- 324
- 325 void DrawGraph(HWND hWnd, HDC hDC)
- 326 {
- 327 int i;
- 328 RECT Client;
- 329 HBRUSH hBrush, hPreBrush;
- 330
- 331 GetClientRect(hWnd, &Client);
- 332
- 333 SetMapMode(hDC, MM_ANISOTROPIC);
- 334 SetWindowExt(hDC, ColorNum*20, 100);
- 335 SetViewportExt(hDC, Client.right, Client.bottom);
- 336
- 337 SelectObject(hDC, GetStockObject(NULL_PEN));
- 338 for (i=0; i<ColorNum; i++)
- 339 {
- 340 hBrush = CreateSolidBrush(PALETTEINDEX(i));
- 341 hPreBrush = SelectObject(hDC, hBrush);
- 342
- 343 PatBlt(hDC, i*20, 0, 20, 100, PATCOPY);
- 344
- 345 SelectObject(hDC, hPreBrush);
- 346 DeleteObject(hBrush);
- 347 }
- 348 }