DEMO2_1.C
上传用户:cncajx
上传日期:2007-01-03
资源大小:190k
文件大小:14k
源码类别:

GDI/图象编程

开发平台:

Visual C++

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