欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

第四章 文本输出

程序员文章站 2023-09-20 12:32:39
1 //SYSMETS.H -- System metrics display structure 2 #include 3 4 #define NUMLINES ((int) (sizeof(sysmetrics) / sizeof(sysmetrics[0]))) 5 6 ......
第四章 文本输出
 1 //SYSMETS.H -- System metrics display structure
 2 #include <Windows.h>
 3 
 4 #define NUMLINES ((int) (sizeof(sysmetrics) / sizeof(sysmetrics[0])))
 5 
 6 struct  
 7 {
 8     int iIndex;
 9     TCHAR *szLabel;
10     TCHAR *szDesc;
11 }
12 sysmetrics[] =
13 {
14     SM_CXSCREEN,            TEXT("SM_CXSCREEN"),            TEXT("Screen width in pixels"),
15     SM_CYSCREEN,            TEXT("SM_CYSCREEN"),            TEXT("Screen height in pixels"),
16     SM_CXVSCROLL,            TEXT("SM_CXVSCROLL"),            TEXT("Vertical scroll width"),
17     SM_CYHSCROLL,            TEXT("SM_CYHSCROLL"),            TEXT("Horizontal scroll height"),
18     SM_CYCAPTION,            TEXT("SM_CYCAPTION"),            TEXT("Caption bar height"),
19     SM_CXBORDER,            TEXT("SM_CXBORDER"),            TEXT("Window border width"),
20     SM_CYBORDER,            TEXT("SM_CYBORDER"),            TEXT("Window border height"),
21     SM_CXFIXEDFRAME,        TEXT("SM_CXFIXEDFRAME"),        TEXT("Dialog window frame width"),
22     SM_CYFIXEDFRAME,        TEXT("SM_CYFIXEDFRAME"),        TEXT("Dialog window frame height"),
23     SM_CYVTHUMB,            TEXT("SM_CYVTHUMB"),            TEXT("Vertical scroll thumb height"),
24     SM_CXHTHUMB,            TEXT("SM_CXHTHUMB"),            TEXT("Horizontal scroll thumb width"),
25     SM_CXICON,                TEXT("SM_CXICON"),                TEXT("Icon width"),
26     SM_CYICON,                TEXT("SM_CYICON"),                TEXT("Icon height"),
27     SM_CXCURSOR,            TEXT("SM_CXCURSOR"),            TEXT("Cursor width"),
28     SM_CYCURSOR,            TEXT("SM_CYCURSOR"),            TEXT("Cursor height"),
29     SM_CYMENU,                TEXT("SM_CYMENU"),                TEXT("Menu bar height"),
30     SM_CXFULLSCREEN,        TEXT("SM_CXFULLSCREEN"),        TEXT("Full screen client area width"),
31     SM_CYFULLSCREEN,        TEXT("SM_CYFULLSCREEN"),        TEXT("Full screen client area height"),
32     SM_CYKANJIWINDOW,        TEXT("SM_CYKANJIWINDOW"),        TEXT("Kanji window height"),
33     SM_MOUSEPRESENT,        TEXT("SM_MOUSEPRESENT"),        TEXT("Mouse present flag"),
34     SM_CYVSCROLL,            TEXT("SM_CYVSCROLL"),            TEXT("Vertical scroll arrow height"),
35     SM_CXHSCROLL,            TEXT("SM_CXHSCROLL"),            TEXT("Horizontal scroll arrow width"),
36     SM_DEBUG,                TEXT("SM_DEBUG"),                TEXT("Debug version flag"),
37     SM_SWAPBUTTON,            TEXT("SM_SWAPBUTTON"),            TEXT("Mouse buttons swapped flag"),
38     SM_CXMIN,                TEXT("SM_CXMIN"),                TEXT("Minimum window width"),
39     SM_CYMIN,                TEXT("SM_CYMIN"),                TEXT("Minimum window height"),
40     SM_CXSIZE,                TEXT("SM_CXSIZE"),                TEXT("Min/Max/Close button width"),
41     SM_CYSIZE,                TEXT("SM_CYSIZE"),                TEXT("Min/Max/Close button height"),
42     SM_CXSIZEFRAME,            TEXT("SM_CXSIZEFRAME"),            TEXT("Window sizing frame width"),
43     SM_CYSIZEFRAME,            TEXT("SM_CYSIZEFRAME"),            TEXT("Window sizing frame height"),
44     SM_CXMINTRACK,            TEXT("SM_CXMINTRACK"),            TEXT("Minimum window tracking width"),
45     SM_CYMINTRACK,            TEXT("SM_CYMINTRACK"),            TEXT("Minimum window tracking height"),
46     SM_CXDOUBLECLK,            TEXT("SM_CXDOUBLECLK"),            TEXT("Double click x tolerance"),
47     SM_CYDOUBLECLK,            TEXT("SM_CYDOUBLECLK"),            TEXT("Double click y tolerance"),
48     SM_CXICONSPACING,        TEXT("SM_CXICONSPACING"),        TEXT("Horizontal icon spacing"),
49     SM_CYICONSPACING,        TEXT("SM_CYICONSPACING"),        TEXT("Vertical icon spacing"),
50     SM_MENUDROPALIGNMENT,    TEXT("SM_MENUDROPALIGMENT"),    TEXT("Left or right menu drop"),
51     SM_PENWINDOWS,            TEXT("SM_PENWINDOWS"),            TEXT("Pen extensions installed"),
52     SM_DBCSENABLED,            TEXT("SM-DBCSENABLED"),            TEXT("Double-Byte Char Set enable"),
53     SM_CMOUSEBUTTONS,        TEXT("SM_CMOUSEBUTTONS"),        TEXT("Number of mouse buttons"),
54     SM_SECURE,                TEXT("SM_SECURE"),                TEXT("Security present flag"),
55     SM_CXEDGE,                TEXT("SM_CXEDGE"),                TEXT("3-D border width"),
56     SM_CYEDGE,                TEXT("SM_CYEDGE"),                TEXT("3-D border height"),
57     SM_CXMINSPACING,        TEXT("SM_CXMINSPACING"),        TEXT("Minimized window spacing width"),
58     SM_CYMINSPACING,        TEXT("SM-CYMINSPACING"),        TEXT("Minimized window spacing height"),
59     SM_CXSMICON,            TEXT("SM_CXSMICON"),            TEXT("Small icon width"),
60     SM_CYSMICON,            TEXT("SM_CYSMICON"),            TEXT("Small icon height"),
61     SM_CYSMCAPTION,            TEXT("SM_CYSMCAPTION"),            TEXT("Small caption height"),
62     SM_CXSMSIZE,            TEXT("SM_CXSMSIZE"),            TEXT("Small caption button width"),
63     SM_CYSMSIZE,            TEXT("SM_CYSMSIZE"),            TEXT("Small caption button height"),
64     SM_CXMENUSIZE,            TEXT("SM_CXMENUSIZE"),            TEXT("Menu bar button width"),
65     SM_CYMENUSIZE,            TEXT("SM_CYMENUSIZE"),            TEXT("Menu bar button height"),
66     SM_ARRANGE,                TEXT("SM_ARRANGE"),                TEXT("How minimized windows arranged"),
67     SM_CXMINIMIZED,            TEXT("SM_CXMINIMIZED"),            TEXT("Minimized window width"),
68     SM_CYMINIMIZED,            TEXT("SM_CYMINIMIZED"),            TEXT("Minimized window height"),
69     SM_CXMAXTRACK,            TEXT("SM_CXMAXTRACK"),            TEXT("Maximum draggable width"),
70     SM_CYMAXTRACK,            TEXT("SM_CYMAXTRACK"),            TEXT("Maximum draggable height"),
71     SM_CXMAXIMIZED,            TEXT("SM_CXMAXIMIZED"),            TEXT("Width of maximized window"),
72     SM_CYMAXIMIZED,            TEXT("SM_CYMAXIMIZED"),            TEXT("Height of maximized windwo"),
73     SM_NETWORK,                TEXT("SM_NETWORK"),                TEXT("Network present flag"),
74     SM_CLEANBOOT,            TEXT("SM_CLEANBOOT"),            TEXT("How system was booted"),
75     SM_CXDRAG,                TEXT("SM_CXDRAG"),                TEXT("Avoid drag x tolerance"),
76     SM_CYDRAG,                TEXT("SM_CYDRAG"),                TEXT("Avoid drag x tolerance"),
77     SM_SHOWSOUNDS,            TEXT("SM_SHOWSOUNDS"),            TEXT("Present sounds visually"),
78     SM_CXMENUCHECK,            TEXT("SM_CXMENUCHECK"),            TEXT("Menu check-mark width"),
79     SM_CYMENUCHECK,            TEXT("SM_CYMENUCHECK"),            TEXT("Menu check-mark height"),
80     SM_SLOWMACHINE,            TEXT("SM_SLOWMACHINE"),            TEXT("Slow processor flag"),
81     SM_MIDEASTENABLED,        TEXT("SM_MIDEASTENABLE"),        TEXT("Hebrew and Arabic enabled flag"),
82     SM_MOUSEWHEELPRESENT,    TEXT("SM_MOUSEWHEELPRESENT"),    TEXT("Mouse wheel present flag"),
83     SM_XVIRTUALSCREEN,        TEXT("SM_XVIRTUALSCREEN"),        TEXT("Virtual screen x origin"),
84     SM_YVIRTUALSCREEN,        TEXT("SM_YVIRTUALSCREEN"),        TEXT("Virtual screen y origin"),
85     SM_CXVIRTUALSCREEN,        TEXT("SM_CXVIRTUALSCREEN"),        TEXT("Virtual screen width"),
86     SM_CYVIRTUALSCREEN,        TEXT("SM_CYVIRTUALSCREEN"),        TEXT("Virtual screen height"),
87     SM_CMONITORS,            TEXT("SM_CMONITORS"),            TEXT("Number of monitors"),
88     SM_SAMEDISPLAYFORMAT,    TEXT("SM_SAMEDISPLAYFORMAT"),    TEXT("Same color format flag")
89 };
SYSMETS.H
第四章 文本输出
  1 //SYSMETS3.C--System Metrics Display Program No.3(c) Charles Petzold, 1998
  2 #include <Windows.h>
  3 #include "SYSMETS.h"
  4 
  5 LRESULT CALLBACK WndPorc(HWND, UINT, WPARAM, LPARAM);
  6 
  7 int WINAPI WinMain( __in HINSTANCE hInstance
  8                 , __in_opt HINSTANCE hPrevInstance
  9                 , __in LPSTR lpCmdLine
 10                 , __in int nShowCmd )
 11 {
 12     static TCHAR szAppName[] = TEXT("Sysmets3");
 13     HWND hwnd;
 14     MSG msg;
 15     WNDCLASS wndclass;
 16 
 17     wndclass.style = CS_HREDRAW | CS_VREDRAW;
 18     wndclass.lpfnWndProc = WndPorc;
 19     wndclass.cbClsExtra = 0;
 20     wndclass.cbWndExtra = 0;
 21     wndclass.hInstance = hInstance;
 22     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 23     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 24     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 25     wndclass.lpszClassName = szAppName;
 26     wndclass.lpszMenuName = NULL;
 27 
 28     if (!RegisterClass(&wndclass))
 29     {
 30         MessageBox(NULL, TEXT("Program requires windows NT!")
 31             , szAppName, MB_ICONERROR);
 32         return 0;
 33     }
 34 
 35     hwnd = CreateWindow(szAppName, TEXT("Get System Metrics No.3")
 36         , WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL
 37         , CW_USEDEFAULT, CW_USEDEFAULT
 38         , CW_USEDEFAULT, CW_USEDEFAULT
 39         , NULL, NULL, hInstance, NULL);
 40 
 41     ShowWindow(hwnd, nShowCmd);
 42     UpdateWindow(hwnd);
 43 
 44     while (GetMessage(&msg, hwnd, 0, 0))
 45     {
 46         TranslateMessage(&msg);
 47         DispatchMessage(&msg);
 48     }
 49 
 50     return msg.wParam;
 51 }
 52 
 53 LRESULT CALLBACK WndPorc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 54 {
 55     static int cxChar, cxCaps, cyChar, cxClient, cyClient, iMaxWidth;
 56     HDC hdc;
 57     int i, x, y, iVertPos, iHorzPos, iPaintBeg, iPaintEnd;
 58     PAINTSTRUCT ps;
 59     SCROLLINFO si;
 60     TCHAR szBuffer[10];
 61     TEXTMETRIC tm;
 62 
 63     switch (message)
 64     {
 65     case WM_CREATE:
 66         hdc = GetDC(hwnd);
 67         GetTextMetrics(hdc, &tm);
 68         cxChar = tm.tmAveCharWidth;
 69         cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
 70         cyChar = tm.tmHeight + tm.tmExternalLeading;
 71         ReleaseDC(hwnd, hdc);
 72 
 73         //save the width of the three columns
 74         iMaxWidth = 40 * cxChar + 22 * cxCaps;
 75         return 0;
 76 
 77     case WM_SIZE:
 78         cxClient = LOWORD(lParam);
 79         cyClient = HIWORD(lParam);
 80         
 81         //set vertical scroll bar range and page size
 82         si.cbSize = sizeof(SCROLLINFO);
 83         si.fMask = SIF_RANGE | SIF_PAGE;
 84         si.nMin = 0;
 85         si.nMax = NUMLINES - 1;
 86         si.nPage = cyClient / cyChar;
 87         SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
 88 
 89         //set horizontal scroll bar range and page size
 90         si.cbSize = sizeof(SCROLLINFO);
 91         si.fMask = SIF_RANGE | SIF_PAGE;
 92         si.nMin = 0;
 93         si.nMax = iMaxWidth / cxChar;
 94         si.nPage = cxClient / cxChar;
 95         SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
 96 
 97         return 0;
 98 
 99     case WM_VSCROLL:
100         //get all the vertical scroll bar information
101         si.cbSize = sizeof(SCROLLINFO);
102         si.fMask = SIF_ALL;
103         GetScrollInfo(hwnd, SB_VERT, &si);
104 
105         //save the position for comparison later on
106         iVertPos = si.nPos;
107 
108         switch (LOWORD(wParam))
109         {
110         case SB_TOP:
111             si.nPos = si.nMin;
112             break;
113         case SB_BOTTOM:
114             si.nPos = si.nMax;
115             break;
116         case SB_LINEUP:
117             si.nPos -= 1;
118             break;
119         case SB_LINEDOWN:
120             si.nPos += 1;
121             break;
122         case SB_PAGEUP:
123             si.nPos -= si.nPage;
124             break;
125         case SB_PAGEDOWN:
126             si.nPos += si.nPage;
127             break;
128         case SB_THUMBTRACK:
129             si.nPos = si.nTrackPos;
130             break;
131         default:
132             break;
133         }
134 
135         //set the position and then retrieve it. Due to adjustments
136         //by windows it may not be the same as the value set.
137         si.fMask = SIF_POS;
138         SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
139         GetScrollInfo(hwnd, SB_VERT, &si);
140 
141         //if the position has changed, scroll the window and update it
142         if (si.nPos != iVertPos)
143         {
144             ScrollWindow(hwnd, 0, cyChar * (iVertPos - si.nPos)
145                 , NULL, NULL);
146             UpdateWindow(hwnd);
147         }
148 
149         return 0;
150 
151     case WM_HSCROLL:
152         //get all the vertical scroll bar information
153         si.cbSize = sizeof(SCROLLINFO);
154         si.fMask = SIF_ALL;
155 
156         //save the position for comparison later on
157         GetScrollInfo(hwnd, SB_HORZ, &si);
158         iHorzPos = si.nPos;
159 
160         switch (LOWORD(wParam))
161         {
162         case SB_LINELEFT:
163             si.nPos -= 1;
164             break;
165         case SB_LINERIGHT:
166             si.nPos += 1;
167             break;
168         case SB_PAGELEFT:
169             si.nPos -= si.nPage;
170             break;
171         case SB_PAGERIGHT:
172             si.nPos += si.nPage;
173         case SB_THUMBPOSITION:
174             si.nPos = si.nTrackPos;
175             break;
176         default:
177             break;
178         }
179 
180         //set the position and then retrieve it. Due to adjustments
181         //by windows it may not be the same as the value set.
182         si.fMask = SIF_POS;
183         SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
184         GetScrollInfo(hwnd, SB_HORZ, &si);
185 
186         //if the position has changed, scroll the window
187         if (si.nPos != iHorzPos)
188         {
189             ScrollWindow(hwnd, cxChar * (iHorzPos - si.nPos)
190                 , 0, NULL, NULL);
191         }
192 
193         return 0;
194 
195     case WM_PAINT:
196         hdc = BeginPaint(hwnd, &ps);
197 
198         //get vertical scroll bar position
199         si.cbSize = sizeof(SCROLLINFO);
200         si.fMask = SIF_POS;
201         GetScrollInfo(hwnd, SB_VERT, &si);
202         iVertPos = si.nPos;
203 
204         //get horizontal scroll bar position
205         GetScrollInfo(hwnd, SB_HORZ, &si);
206         iHorzPos = si.nPos;
207 
208         //find painting limits
209         iPaintBeg = max(0, iVertPos + ps.rcPaint.top / cyChar);
210         iPaintEnd = min(NUMLINES - 1, iVertPos + ps.rcPaint.bottom / cyChar);
211 
212         for (i = iPaintBeg; i <= iPaintEnd; ++i)
213         {
214             x = cxChar * (1 - iHorzPos);
215             y = cyChar * (i - iVertPos);
216 
217             TextOut(hdc, x, y, sysmetrics[i].szLabel
218                 , lstrlen(sysmetrics[i].szLabel));
219 
220             TextOut(hdc, x + 22 * cxCaps, y
221                 , sysmetrics[i].szDesc, lstrlen(sysmetrics[i].szDesc));
222 
223             SetTextAlign(hdc, TA_RIGHT | TA_TOP);
224 
225             TextOut(hdc, x + 22 * cxCaps + 40 * cxChar, y, szBuffer
226                 , wsprintf(szBuffer, TEXT("%5d"), GetSystemMetrics(sysmetrics[i].iIndex)));
227 
228             SetTextAlign(hdc, TA_LEFT | TA_TOP);
229         }
230 
231         EndPaint(hwnd, &ps);
232         return 0;
233 
234     case WM_DESTROY:
235         PostQuitMessage(0);
236         return 0;
237     }
238 
239     return DefWindowProc(hwnd, message, wParam, lParam);
240 }
SYSMETS3.C