1、DEVCAPS1.C
/*DEVCAPS1.C*/
#include<windows.h>
#define NUMLINE(int) (sizeof devcaps/sizeof devcaps[0]) //定义数组元素个数
struct
{
int iIndex;
TCHAR*szLabel;
TCHAR*szDesc; //这个有什么用?
}
devcaps[] =
{
HORZSIZE,TEXT("HORZSIZE"),TEXT("Width in milimeters:"), //毫米宽
VERTSIZE, TEXT ("VERTSIZE"),TEXT ("Height in millimeters:"),
HORZRES, TEXT ("HORZRES"), TEXT ("Width in pixels:"), //像素宽
VERTRES, TEXT ("VERTRES"), TEXT ("Height in raster lines:"),//光栅线高度
BITSPIXEL, TEXT ("BITSPIXEL"),TEXT ("Color bits per pixel:"),//<span style="font-family: Arial, Helvetica, sans-serif;">//每像素颜色位数</span>
PLANES, TEXT ("PLANES"), TEXT ("Number of color planes:"), //彩色平面数目
NUMBRUSHES, TEXT ("NUMBRUSHES"), TEXT ("Number of device brushes:"), //设备刷数量
NUMPENS, TEXT ("NUMPENS"), TEXT ("Number of device pens:"),
NUMMARKERS, TEXT ("NUMMARKERS"), TEXT ("Number of device markers:"),//设备标记数量
NUMFONTS, TEXT ("NUMFONTS"), TEXT ("Number of device fonts:"),
NUMCOLORS, TEXT ("NUMCOLORS"), TEXT ("Number of device colors:"),
PDEVICESIZE, TEXT ("PDEVICESIZE"),TEXT ("Size of device structure:"),//设备结构的尺寸
ASPECTX, TEXT ("ASPECTX"), TEXT ("Relative width of pixel:"),//相对宽度的像素
ASPECTY, TEXT ("ASPECTY"), TEXT ("Relative height of pixel:"),
ASPECTXY, TEXT ("ASPECTXY"), TEXT ("Relative diagonal of pixel:"),//相对对角线像素
LOGPIXELSX, TEXT ("LOGPIXELSX"), TEXT ("Horizontal dots per inch:"),//每英寸水平点(典型值96)
LOGPIXELSY, TEXT ("LOGPIXELSY"), TEXT ("Vertical dots per inch:"), //每英寸高度点<span style="font-family: Arial, Helvetica, sans-serif;">(典型值120)</span>
SIZEPALETTE, TEXT ("SIZEPALETTE"),TEXT ("Number of palette entries:"),//调色盘条目
NUMRESERVED, TEXT ("NUMRESERVED"),TEXT ("Reserved palette entries:"), //保留调色盘条目
COLORRES, TEXT ("COLORRES"), TEXT ("Actual color resolution:")//当前颜色分辨率(该值表示一种色彩,指定了红绿蓝,即RGB色彩)
}
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("DevCaps1") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc= WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName= NULL ;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("Device Capabilities"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar ;
TCHAR szBuffer[10] ;
HDC hdc ;
int i ;
PAINTSTRUCT ps ;
TEXTMETRIC tm ;
switch (message)
{
case WM_CREATE:
hdc = GetDC (hwnd) ;
GetTextMetrics (hdc, &tm) ;
cxChar= tm.tmAveCharWidth ;
cxCaps= (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2 ; //width of capital letters 大写字母的宽度,先判断字母是否为变宽字体。是为低位1,否为0。
cyChar= tm.tmHeight + tm.tmExternalLeading ;
ReleaseDC (hwnd, hdc) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
for (i = 0 ; i < NUMLINES ; i++)
{
TextOut ( hdc, 0, cyChar * i,
devcaps[i].szLabel,
lstrlen (devcaps[i].szLabel)) ;
TextOut ( hdc, 14 * cxCaps, cyChar * i,
devcaps[i].szDesc,
lstrlen (devcaps[i].szDesc)) ;
SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;
TextOut (hdc, 14*cxCaps+35*cxChar, cyChar*i, szBuffer,
wsprintf (szBuffer, TEXT ("%5d"),
GetDeviceCaps(hdc, devcaps[i].iIndex))) ;
SetTextAlign (hdc, TA_LEFT | TA_TOP) ;
}
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
单词:Capabilities能力、dots点、resolution分辨率、actual当前、planes平面、marker、pixel像素、entry数目、palette调色板
编译代码:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#define NUMLINES (int)(sizeof devcaps/sizeof devcaps[0])
struct
{
int iIndex;
TCHAR *szLable;
TCHAR *szDesc;
}
devcaps[] =
{
HORZSIZE,TEXT("HORZSIZE"),TEXT("Width in milimeters:"), //毫米宽
VERTSIZE, TEXT ("VERTSIZE"),TEXT ("Height in millimeters:"),
HORZRES, TEXT ("HORZRES"), TEXT ("Width in pixels:"), //像素宽
VERTRES, TEXT ("VERTRES"), TEXT ("Height in raster lines:"),//光栅线高度
BITSPIXEL, TEXT ("BITSPIXEL"),TEXT ("Color bits per pixel:"),//<span style="font-family: Arial, Helvetica, sans-serif;">//每像素颜色位数</span>
PLANES, TEXT ("PLANES"), TEXT ("Number of color planes:"), //彩色平面数目
NUMBRUSHES, TEXT ("NUMBRUSHES"), TEXT ("Number of device brushes:"), //设备刷数量
NUMPENS, TEXT ("NUMPENS"), TEXT ("Number of device pens:"),
NUMMARKERS, TEXT ("NUMMARKERS"), TEXT ("Number of device markers:"),//设备标记数量
NUMFONTS, TEXT ("NUMFONTS"), TEXT ("Number of device fonts:"),
NUMCOLORS, TEXT ("NUMCOLORS"), TEXT ("Number of device colors:"),
PDEVICESIZE, TEXT ("PDEVICESIZE"),TEXT ("Size of device structure:"),//设备结构的尺寸
ASPECTX, TEXT ("ASPECTX"), TEXT ("Relative width of pixel:"),//相对宽度的像素
ASPECTY, TEXT ("ASPECTY"), TEXT ("Relative height of pixel:"),
ASPECTXY, TEXT ("ASPECTXY"), TEXT ("Relative diagonal of pixel:"),//相对对角线像素
LOGPIXELSX, TEXT ("LOGPIXELSX"), TEXT ("Horizontal dots per inch:"),//每英寸水平点(典型值96)
LOGPIXELSY, TEXT ("LOGPIXELSY"), TEXT ("Vertical dots per inch:"), //每英寸高度点<span style="font-family: Arial, Helvetica, sans-serif;">(典型值120)</span>
SIZEPALETTE, TEXT ("SIZEPALETTE"),TEXT ("Number of palette entries:"),//调色盘条目
NUMRESERVED, TEXT ("NUMRESERVED"),TEXT ("Reserved palette entries:"), //保留调色盘条目
COLORRES, TEXT ("COLORRES"), TEXT ("Actual color resolution:")//当前颜色分辨率(该值表示一种色彩,指定了红绿蓝,即RGB色彩)
};
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("Devcaps");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_HREDRAW|CS_VREDRAW; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Devcaps"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar,cxCaps,cyChar;
TCHAR szBuffer[10];
HDC hdc;
int i;
PAINTSTRUCT ps;
TEXTMETRIC tm;
switch (message) /* handle the messages */
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily &1?3:2)*cxChar/2;
cyChar = tm.tmHeight +tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
for(i = 0;i< NUMLINES;i++)
{
TextOut(hdc,0,cyChar*i,devcaps[i].szLable,
lstrlen(devcaps[i].szLable));
TextOut(hdc,14*cxCaps,cyChar*i,devcaps[i].szDesc,
lstrlen(devcaps[i].szDesc));
SetTextAlign(hdc,TA_RIGHT|TA_TOP);
TextOut(hdc,14*cxCaps+35*cxChar,cyChar*i,szBuffer,
wsprintf(szBuffer,TEXT("%5d"), //注意这里与上面的第4个参数不同,它用wspringtf()函数,同时与缓冲区搭配
GetDeviceCaps(hdc,devcaps[i].iIndex)));
SetTextAlign(hdc,TA_LEFT|TA_TOP);
}
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}