第五章 绘图基础(DEVCAPS1)

获取设备环境的信息

  1 //DEVCAPS1.C--Device Capabilities Display Program No.1 (c) Charles Petzold, 1998
  2 #include <Windows.h>
  3 
  4 #define NUMLINES ((int) (sizeof(devcaps) / sizeof(devcaps[0])))
  5 struct  
  6 {
  7     int iIndex;
  8     TCHAR *szLabel;
  9     TCHAR *szDesc;
 10 }
 11 devcaps[] =
 12 {
 13     HORZSIZE,        TEXT("HORZSIZE"),        TEXT("Width in millimeters:"),
 14     VERTSIZE,        TEXT("VERTSIZE"),        TEXT("Height in milimeters:"),
 15     HORZRES,        TEXT("HORZRES"),        TEXT("Width in pixels:"),
 16     VERTRES,        TEXT("VERTRES"),        TEXT("Height in raster lines:"),
 17     BITSPIXEL,        TEXT("BITSPIXEL"),        TEXT("Color bits per pixel:"),
 18     PLANES,            TEXT("PLANES"),            TEXT("Number of color planes:"),
 19     NUMBRUSHES,        TEXT("NUMBRUSHES"),        TEXT("Number of device brushes"),
 20     NUMPENS,        TEXT("NUMPENS"),        TEXT("Number of device pens:"),
 21     NUMMARKERS,        TEXT("NUMMARKERS"),        TEXT("Number of device markers:"),
 22     NUMFONTS,        TEXT("NUMFONTS"),        TEXT("Number of device fonts:"),
 23     NUMCOLORS,        TEXT("NUMCOLORS"),        TEXT("Number of device colors:"),
 24     PDEVICESIZE,    TEXT("PDEVICESIZE"),    TEXT("Size of device structure:"),
 25     ASPECTX,        TEXT("ASPECTX"),        TEXT("Relative width of pixel:"),
 26     ASPECTY,        TEXT("ASPECTY"),        TEXT("Relative height of pixel:"),
 27     ASPECTXY,        TEXT("ASPECTXY"),        TEXT("Relative diagonal of pixel:"),
 28     LOGPIXELSX,        TEXT("LOGPIXELSX"),        TEXT("Horizontal dots per inch:"),
 29     LOGPIXELSY,        TEXT("LOGPIXELSY"),        TEXT("Vertical dots per inch:"),
 30     SIZEPALETTE,    TEXT("SIZEPALETTE"),    TEXT("Number of palette entries:"),
 31     NUMRESERVED,    TEXT("NUMRESERVED"),    TEXT("Reserved palette entries:"),
 32     COLORRES,        TEXT("COLORRES"),        TEXT("Actual color resolution:")
 33 };
 34 
 35 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 36 
 37 int WINAPI WinMain( __in HINSTANCE hInstance
 38                     , __in_opt HINSTANCE hPrevInstance
 39                     , __in LPSTR lpCmdLine
 40                     , __in int nShowCmd )
 41 {
 42     static TCHAR szAppName[] = TEXT("DevCaps1");
 43     HWND hwnd;
 44     MSG msg;
 45     WNDCLASS wndclass;
 46 
 47     wndclass.style = CS_HREDRAW | CS_VREDRAW;
 48     wndclass.lpfnWndProc = WndProc;
 49     wndclass.cbClsExtra = 0;
 50     wndclass.cbWndExtra = 0;
 51     wndclass.hInstance = hInstance;
 52     wndclass.hIcon= LoadIcon(NULL, IDI_APPLICATION);
 53     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 54     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 55     wndclass.lpszMenuName = NULL;
 56     wndclass.lpszClassName = szAppName;
 57 
 58     if (!RegisterClass(&wndclass))
 59     {
 60         MessageBox(NULL, TEXT("This program requires Window NT!")
 61             , szAppName, MB_ICONERROR);
 62         return 0;
 63     }
 64 
 65     hwnd = CreateWindow(szAppName, TEXT("Device Capabilities")
 66         , WS_OVERLAPPEDWINDOW
 67         , CW_USEDEFAULT, CW_USEDEFAULT
 68         , CW_USEDEFAULT, CW_USEDEFAULT
 69         , NULL, NULL, hInstance, NULL);
 70 
 71     ShowWindow(hwnd, nShowCmd);
 72     UpdateWindow(hwnd);
 73 
 74     while (GetMessage(&msg, NULL, 0, 0))
 75     {
 76         TranslateMessage(&msg);
 77         DispatchMessage(&msg);
 78     }
 79 
 80     return msg.wParam;
 81 }
 82 
 83 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 84 {
 85     static int cxChar, cxCaps, cyChar;
 86     TCHAR szBuffer[10];
 87     HDC hdc;
 88     int i;
 89     PAINTSTRUCT ps;
 90     TEXTMETRIC tm;
 91 
 92     switch (message)
 93     {
 94     case WM_CREATE:
 95         hdc = GetDC(hwnd);
 96         GetTextMetrics(hdc, &tm);
 97         cxChar = tm.tmAveCharWidth;
 98         cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
 99         cyChar = tm.tmHeight + tm.tmExternalLeading;
100         ReleaseDC(hwnd, hdc);
101         return 0;
102 
103     case WM_PAINT:
104         hdc = BeginPaint(hwnd, &ps);
105         for (i = 0; i != NUMLINES; ++i)
106         {
107             TextOut(hdc, 0, cyChar * i, devcaps[i].szLabel
108                 , lstrlen(devcaps[i].szLabel));
109             TextOut(hdc, 14 * cxCaps, cyChar * i, devcaps[i].szDesc
110                 , lstrlen(devcaps[i].szDesc));
111             SetTextAlign(hdc, TA_RIGHT | TA_TOP);
112             TextOut(hdc, 14 * cxCaps + 35 * cxChar, cyChar * i, szBuffer
113                 , wsprintf(szBuffer, TEXT("%5d"), GetDeviceCaps(hdc, devcaps[i].iIndex)));
114             SetTextAlign(hdc, TA_LEFT | TA_TOP);
115         }
116         EndPaint(hwnd, &ps);
117         return 0;
118 
119     case WM_DESTROY:
120         PostQuitMessage(0);
121         return 0;
122     }
123 
124     return DefWindowProc(hwnd, message, wParam, lParam);
125 }
DEVCAPS1.C

猜你喜欢

转载自www.cnblogs.com/web1013/p/8940743.html