DEMO3_5.C
资源名称:winpaint.zip [点击查看]
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:14k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- 1 /****************************************************************/
- 2 /* Demo3_5 --- The Use of GetDeviceCaps */
- 3 /****************************************************************/
- 4
- 5 #include <windows.h>
- 6 #include "demo3_5.h"
- 7
- 8 int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
- 9 long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
- 10
- 11 void ShowDevCaps(HDC, HDC);
- 12 HDC CreateIC_Printer();
- 13
- 14 int CharX, CharY;
- 15 int OY = 0;
- 16
- 17
- 18 /****************************************************************/
- 19 /* WinMain() */
- 20 /****************************************************************/
- 21
- 22 int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
- 23 LPSTR lpszCmdLine, int nCmdShow)
- 24 {
- 25 WNDCLASS wclass;
- 26 MSG msg;
- 27 HWND hWnd;
- 28 char szName[] = "Demo3_5";
- 29
- 30 if (!hPrevInstance)
- 31 {
- 32 wclass.style = CS_VREDRAW;
- 33 wclass.lpfnWndProc = MainWndProc;
- 34 wclass.cbClsExtra = 0;
- 35 wclass.cbWndExtra = 0;
- 36 wclass.hInstance = hInstance;
- 37 wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- 38 wclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- 39 wclass.hbrBackground = GetStockObject(WHITE_BRUSH);
- 40 wclass.lpszMenuName = szName;
- 41 wclass.lpszClassName = szName;
- 42
- 43 if (!RegisterClass (&wclass))
- 44 return (FALSE);
- 45 }
- 46
- 47 hWnd = CreateWindow(
- 48 szName,
- 49 "The GetDeviceCaps" ,
- 50 WS_OVERLAPPEDWINDOW | WS_VSCROLL,
- 51 CW_USEDEFAULT,
- 52 CW_USEDEFAULT,
- 53 CW_USEDEFAULT,
- 54 CW_USEDEFAULT,
- 55 NULL,
- 56 NULL,
- 57 hInstance,
- 58 NULL );
- 59
- 60 if (!hWnd)
- 61 return (FALSE);
- 62
- 63 ShowWindow(hWnd, nCmdShow);
- 64 UpdateWindow(hWnd);
- 65
- 66 while (GetMessage(&msg, NULL, NULL,NULL))
- 67 {
- 68 TranslateMessage(&msg);
- 69 DispatchMessage(&msg);
- 70 }
- 71 return (msg.wParam);
- 72 }
- 73
- 74
- 75
- 76 /****************************************************************/
- 77 /* MainWndProc() */
- 78 /****************************************************************/
- 79
- 80 long FAR PASCAL MainWndProc(HWND hWnd, unsigned message,
- 81 WORD wParam, LONG lParam)
- 82 {
- 83 HDC hDC, hInfoDC;
- 84 HMENU hMenu;
- 85 PAINTSTRUCT ps;
- 86 TEXTMETRIC tm;
- 87 HFONT hFont;
- 88 int Temp;
- 89 static int DeviceID = IDM_SCREEN;
- 90 static int MaxLine;
- 91 static int CY;
- 92
- 93 switch (message)
- 94 {
- 95 case WM_CREATE :
- 96 hMenu = GetMenu(hWnd);
- 97 CheckMenuItem(hMenu, IDM_SCREEN, MF_CHECKED);
- 98
- 99 hDC = GetDC(hWnd);
- 100
- 101 hFont = GetStockObject(SYSTEM_FIXED_FONT);
- 102 SelectObject(hDC, hFont);
- 103
- 104 GetTextMetrics(hDC, &tm);
- 105 CharX = tm.tmAveCharWidth;
- 106 CharY = tm.tmHeight + tm.tmExternalLeading;
- 107
- 108 ReleaseDC(hWnd, hDC);
- 109
- 110 MaxLine = GetMaxLine();
- 111 break;
- 112
- 113 case WM_COMMAND :
- 114 switch(wParam)
- 115 {
- 116 case IDM_SCREEN :
- 117 if (DeviceID != IDM_SCREEN)
- 118 {
- 119 hMenu = GetMenu(hWnd);
- 120 CheckMenuItem(hMenu, DeviceID,
- 121 MF_UNCHECKED);
- 122 DeviceID = IDM_SCREEN;
- 123 CheckMenuItem(hMenu, DeviceID,
- 124 MF_CHECKED);
- 125 InvalidateRect(hWnd, NULL, TRUE);
- 126 }
- 127 break;
- 128
- 129 case IDM_PRINTER :
- 130 if (DeviceID != IDM_PRINTER)
- 131 {
- 132 hMenu = GetMenu(hWnd);
- 133 CheckMenuItem(hMenu, DeviceID,
- 134 MF_UNCHECKED);
- 135 DeviceID = IDM_PRINTER;
- 136 CheckMenuItem(hMenu, DeviceID,
- 137 MF_CHECKED);
- 138 InvalidateRect(hWnd, NULL, TRUE);
- 139 }
- 140 break;
- 141 }
- 142 break;
- 143
- 144 case WM_DEVMODECHANGE :
- 145 InvalidateRect(hWnd, NULL, TRUE);
- 146 break;
- 147
- 148 case WM_PAINT :
- 149 hDC = BeginPaint(hWnd, &ps);
- 150
- 151 if (DeviceID == IDM_SCREEN)
- 152 { hInfoDC = CreateIC("DISPLAY", NULL, NULL, NULL);
- 153 if (hInfoDC)
- 154 { ShowDevCaps(hDC, hInfoDC);
- 155 DeleteDC(hInfoDC);
- 156 }
- 157 }
- 158 else
- 159 { hInfoDC = CreateIC_Printer();
- 160 if (hInfoDC)
- 161 { ShowDevCaps(hDC, hInfoDC);
- 162 DeleteDC(hInfoDC);
- 163 }
- 164 }
- 165 EndPaint(hWnd, &ps);
- 166 break;
- 167
- 168 case WM_SIZE :
- 169
- 170 CY = HIWORD(lParam) / CharY;
- 171 SetScrollRange(hWnd, SB_VERT,
- 172 0, MaxLine-CY, TRUE);
- 173 OY = min(OY, MaxLine-CY);
- 174 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
- 175 break;
- 176
- 177 case WM_KEYDOWN :
- 178 switch (wParam)
- 179 {
- 180 case VK_HOME :
- 181 SendMessage(hWnd, WM_VSCROLL,
- 182 SB_TOP, 0L);
- 183 break;
- 184
- 185 case VK_END :
- 186 SendMessage(hWnd, WM_VSCROLL,
- 187 SB_BOTTOM, 0L);
- 188 break;
- 189
- 190 case VK_PRIOR :
- 191 SendMessage(hWnd, WM_VSCROLL,
- 192 SB_PAGEUP, 0L);
- 193 break;
- 194
- 195 case VK_NEXT :
- 196 SendMessage(hWnd, WM_VSCROLL,
- 197 SB_PAGEDOWN, 0L);
- 198 break;
- 199
- 200 case VK_UP :
- 201 SendMessage(hWnd, WM_VSCROLL,
- 202 SB_LINEUP, 0L);
- 203 break;
- 204
- 205 case VK_DOWN :
- 206 SendMessage(hWnd, WM_VSCROLL,
- 207 SB_LINEDOWN, 0L);
- 208 break;
- 209 }
- 210 break;
- 211
- 212 case WM_VSCROLL :
- 213 switch (wParam)
- 214 {
- 215 case SB_TOP :
- 216 Temp = 0;
- 217 break;
- 218
- 219 case SB_BOTTOM :
- 220 Temp = MaxLine - CY;
- 221 break;
- 222
- 223 case SB_LINEUP :
- 224 Temp = OY - 1;
- 225 break;
- 226
- 227 case SB_PAGEUP :
- 228 Temp = OY - CY;
- 229 break;
- 230
- 231 case SB_LINEDOWN :
- 232 Temp = OY + 1;
- 233 break;
- 234
- 235 case SB_PAGEDOWN :
- 236 Temp = OY + CY;
- 237 break;
- 238
- 239 case SB_THUMBPOSITION :
- 240 Temp = LOWORD(lParam);
- 241 break;
- 242
- 243 default :
- 244 return (NULL);
- 245 }
- 246
- 247 Temp = min(max(0, Temp), MaxLine-CY);
- 248 if (Temp == OY) break;
- 249
- 250 OY = Temp;
- 251 SetScrollPos(hWnd, SB_VERT, OY, TRUE);
- 252 InvalidateRect(hWnd, NULL, TRUE);
- 253 break;
- 254
- 255 case WM_DESTROY :
- 256 PostQuitMessage(0);
- 257 break ;
- 258
- 259 default :
- 260 return (DefWindowProc(hWnd, message, wParam, lParam));
- 261 }
- 262 return (NULL);
- 263 }
- 264
- 265
- 266 HDC CreateIC_Printer()
- 267 {
- 268 HDC hInfoDC;
- 269 char szProfile[70];
- 270 char *szDriver, *szDevice, *szOutput;
- 271
- 272 GetProfileString("windows", "device", "", szProfile, 70);
- 273
- 274 szDevice = (char *) strtok(szProfile, ",");
- 275 szDriver = (char *) strtok(NULL, ",");
- 276 szOutput = (char *) strtok(NULL, ",");
- 277
- 278 if (szDevice && szDriver && szOutput)
- 279 {
- 280 hInfoDC = CreateIC(szDriver, szDevice, szOutput, NULL);
- 281 return (hInfoDC);
- 282 }
- 283
- 284 return (NULL);
- 285 }
- 286
- 287
- 288 typedef struct {
- 289 int Index;
- 290 char *szMeaning;
- 291 } CAPS;
- 292
- 293 typedef struct {
- 294 int Mask;
- 295 char *szMeaning;
- 296 } TextCAPS;
- 297
- 298 CAPS Caps[] = {
- 299 DRIVERVERSION, "Version number",
- 300 HORZSIZE, "Width of the display in mm",
- 301 VERTSIZE, "Height of the display in mm",
- 302 HORZRES, "Width of the display in pixels",
- 303 VERTRES, "Height of the display in pixels",
- 304 LOGPIXELSX, "Horizontal pixels per logical inch",
- 305 LOGPIXELSY, "Vertical pixels per logical inch",
- 306 BITSPIXEL, "Num of color bits per pixel",
- 307 PLANES, "Num of color planes",
- 308 NUMBRUSHES, "Num of device-specific brushes",
- 309 NUMPENS, "Num of device-specific pens",
- 310 NUMMARKERS, "Num of device-specific markers",
- 311 NUMFONTS, "Num of device-specific fonts",
- 312 NUMCOLORS, "Num of device's color table entries",
- 313 ASPECTX, "Relative width of a device pixel",
- 314 ASPECTY, "Relative height of a device pixel",
- 315 ASPECTXY, "Diagonal width of the device pixel",
- 316 PDEVICESIZE, "Size of the PDEVICE structure",
- 317 CLIPCAPS, "Flag of clipping capabilities",
- 318 SIZEPALETTE, "Num of the palette entries",
- 319 NUMRESERVED, "Num of reserved palette entries",
- 320 COLORRES, "Actual color resolution (bits/pixel)",
- 321 };
- 322
- 323 TextCAPS TextCaps[] = {
- 324 TC_OP_CHARACTER,"Can do Char Output Precision",
- 325 TC_OP_STROKE, "Can do Stroke Output Precision",
- 326 TC_CP_STROKE, "Can do Stroke Clip Precision",
- 327 TC_CR_90, "Can do Char Rotation - 90-degree",
- 328 TC_CR_ANY, "Can do Char Rotation - any char",
- 329 TC_SF_X_YINDEP, "Can do Scaling - X_Y independent",
- 330 TC_SA_DOUBLE, "Can do Scaling - DOUBLE",
- 331 TC_SA_INTEGER, "Can do Scaling - INTEGER",
- 332 TC_SA_CONTIN, "Can do Scaling - ANY",
- 333 TC_EA_DOUBLE, "Can do Embolding - DOUBLE",
- 334 TC_IA_ABLE, "Can do Italisizing",
- 335 TC_UA_ABLE, "Can do Underlining",
- 336 TC_SO_ABLE, "Can do StrikeOuts",
- 337 TC_RA_ABLE, "Can do Raster Fonts",
- 338 TC_VA_ABLE, "Can do Vector Fonts"
- 339 };
- 340
- 341
- 342 int GetMaxLine()
- 343 { int MaxLine;
- 344
- 345 MaxLine = sizeof(Caps)/sizeof(CAPS) +
- 346 sizeof(TextCaps)/sizeof(TextCAPS) + 5;
- 347 return(MaxLine);
- 348 }
- 349
- 350
- 351 void ShowDevCaps(HDC hDC, HDC hInfoDC)
- 352 {
- 353 HFONT hFont;
- 354 int i, Val;
- 355 char szString[60];
- 356 int CapsNum;
- 357 int TextCapsNum;
- 358 BOOL MASKTRUE;
- 359
- 360 hFont = GetStockObject(SYSTEM_FIXED_FONT);
- 361 SelectObject(hDC, hFont);
- 362
- 363 CapsNum = sizeof(Caps) / sizeof(CAPS);
- 364 TextCapsNum = sizeof(TextCaps) / sizeof(TextCAPS);
- 365
- 366 SetMapMode(hDC, MM_ANISOTROPIC);
- 367 SetWindowOrg(hDC, 0, OY);
- 368 SetViewportOrg(hDC, 10, 0);
- 369 SetWindowExt(hDC, 1, 1);
- 370 SetViewportExt(hDC, CharX, CharY);
- 371
- 372 /* Show the common capabilities */
- 373
- 374 strcpy(szString, "The Basic Capabilities :");
- 375 TextOut(hDC, 0, 0, szString, strlen(szString));
- 376
- 377 for (i=0; i<CapsNum; i++)
- 378 {
- 379 Val = GetDeviceCaps(hInfoDC, Caps[i].Index);
- 380 if (Caps[i].Index == DRIVERVERSION)
- 381 sprintf(szString, "%-45s : %#5x",
- 382 Caps[i].szMeaning, Val);
- 383 else
- 384 sprintf(szString, "%-45s : %5d",
- 385 Caps[i].szMeaning, Val);
- 386
- 387 TextOut(hDC, 1, 2+i, szString, strlen(szString));
- 388 }
- 389
- 390
- 391 /* Show the Text capabilities */
- 392
- 393 strcpy(szString, "TEXTCAPS - Text Capabilities :");
- 394 TextOut(hDC, 0, CapsNum+3, szString, strlen(szString));
- 395
- 396 Val = GetDeviceCaps(hInfoDC, TEXTCAPS);
- 397
- 398 for (i=0; i<TextCapsNum; i++)
- 399 {
- 400 if (Val & TextCaps[i].Mask)
- 401 MASKTRUE = TRUE;
- 402 else
- 403 MASKTRUE = FALSE;
- 404
- 405 sprintf(szString, "%-45s : %5s", TextCaps[i].szMeaning,
- 406 (MASKTRUE==TRUE ? "YES" : "NO"));
- 407 TextOut(hDC, 1, CapsNum+5+i, szString,
- 408 strlen(szString));
- 409 }
- 410 }