SYSTEMS_5.h
#define NUMLINES ( (int) (sizeof (sysmetrics) / sizeof(sysmetrics[0] ) ) )
struct
{
int iIndex;
TCHAR * szLabel;
TCHAR * szDesc;
}
sysmetrics[] =
{
SM_CXSCREEN, TEXT("SM_CXSREEN"),
TEXT("Screen width in pixels"),
SM_CYSCREEN, TEXT("SM_CySREEN"),
TEXT("Screen height in pixels"),
SM_CXVSCROLL, TEXT("SM_CXVSCROLL"),
TEXT("Vertical scroll width"),
SM_CYVSCROLL, TEXT("SM_CYVSCROLL"),
TEXT("Vertical scroll height"),
SM_CYCAPTION, TEXT("SM_CYCAPTION"),
TEXT("Horizontal scroll height"),
SM_CXBORDER, TEXT("SM_CXBORDER"),
TEXT("Window border width"),
SM_CYBORDER, TEXT("SM_CYBORDER"),
TEXT("Window border height"),
SM_CXFIXEDFRAME, TEXT("SM_CXFIXEDFRAME"),
TEXT("Dialog window frame widht"),
SM_CYFIXEDFRAME, TEXT("SM_CXFIXEDFRAME"),
TEXT("Dialog window frame height"),
SM_CYVTHUMB, TEXT("SM_CYVTHUMB"),
TEXT("Veritical scroll thumb height"),
SM_CXHTHUMB, TEXT("SM_CXHTHUMB"),
TEXT("Veritical scroll thumb width"),
SM_CXICON, TEXT("SM_CXICON"),
TEXT("Icon width"),
SM_CYICON, TEXT("SM_CYICON"),
TEXT("Icon height"),
SM_CXCURSOR, TEXT("SM_CXCURSOR"),
TEXT("Cursor width"),
SM_CYCURSOR, TEXT("SM_CYCURSOR"),
TEXT("Cursor heifht"),
SM_CYMENU, TEXT("SM_CYMENU"),
TEXT("Menu bar height"),
SM_CXFULLSCREEN, TEXT("SM_CFULLSCREEN,"),
TEXT("Full screen client area width"),
SM_CYFULLSCREEN, TEXT("SM_CYFULLSCREEN"),
TEXT("Full screen client area height"),
SM_CYKANJIWINDOW, TEXT("SM_CYKANJIWINDOW"),
TEXT("Kanji window height"),
SM_MOUSEPRESENT, TEXT("SM_MOUSEPRESENT"),
TEXT("Mouse present flag"),
SM_CYVSCROLL, TEXT("SM_CYVSCROLL"),
TEXT("Vertical scroll arrow height"),
SM_CXHSCROLL, TEXT("SM_CXHSCROLL"),
TEXT("Horizontal scroll arrow width"),
SM_DEBUG, TEXT("SM_DEBUG"),
TEXT("Debug version flag"),
SM_SWAPBUTTON, TEXT("SM_SWAPBUTTON"),
TEXT("Mouse buttons swapped flag"),
SM_CXMIN, TEXT("SM_CXMIN"),
TEXT("Minimum window weidth"),
SM_CYMIN, TEXT("SM_CYMIN"),
TEXT("Minimum window height"),
SM_CXSIZE, TEXT("SM_CXSIZE"),
TEXT("Min/Max/Colose button width"),
SM_CYSIZE, TEXT("SM_CYSIZE"),
TEXT("Min/Max/Colose button height"),
SM_CXSIZEFRAME, TEXT("SM_CXSIZEFRAME"),
TEXT("Windows sizing frame width"),
SM_CYSIZEFRAME, TEXT("SM_CYSIZEFRAME"),
TEXT("Windows sizing frame height"),
SM_CXMINTRACK, TEXT("SM_CXMINTRACK"),
TEXT("Minimum windows tracking width"),
SM_CYMINTRACK, TEXT("SM_CYMINTRACK"),
TEXT("Minimum windows tracking height"),
SM_CXDOUBLECLK, TEXT("SM_CXDOUBLECLK"),
TEXT("Double click x tolerance"),
SM_CYDOUBLECLK, TEXT("SM_CYDOUBLECLK"),
TEXT("Double click y tolerance"),
};
SYSTEMS_5
#include<windows.h>
#include"SYSTEMS_5.h"
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow )
{
static TCHAR szAppName[] = TEXT("SysMets1");
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 | MB_OK );
return 0;
}
//产生窗口
hwnd = CreateWindow( szAppName, //窗口类名称
TEXT("Get System Metrics No. 1"), //窗口标题
WS_OVERLAPPEDWINDOW | WS_VSCROLL, //窗口风格/样式
CW_USEDEFAULT, //初始 x 坐标
CW_USEDEFAULT, //初始 y 坐标
CW_USEDEFAULT, //初始 x 方向尺寸
CW_USEDEFAULT, //初始 x 方向尺寸
NULL, //父窗口句柄
NULL, //窗口菜单句柄
hInstance, //程序实例句柄
NULL); //创建参数
ShowWindow( hwnd, iCmdShow );//显示窗口
UpdateWindow( hwnd );//更新窗口
//消息循环
while ( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return msg.wParam;
//MessageBox( NULL, TEXT("Hello, Windoows 98!"), TEXT("HelloMsg"), 0);
return 0;
}
//窗口过程
LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
static int cxChar,cxCaps,cyChar,cyClient,iVscrollPos;
HDC hdc;//设备环境句柄
int i = 0,y = 0;
PAINTSTRUCT ps;//windows为每个窗口维护的“绘制信息结构”
TCHAR szBuffer[10];
TEXTMETRIC tm;
//RECT rect;
switch( message )
{
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 );
//初始化滚动条的范围和位置
SetScrollRange( hwnd, SB_VERT, 0, NUMLINES - 1, FALSE );//设置滚动条的范围
SetScrollPos( hwnd, SB_VERT, iVscrollPos, TRUE );//指定滚动条的位置
return 0;
}
case WM_SIZE:
{
cyClient = HIWORD( lParam );//客户区高度
return 0;
}
//处理传给窗口过程的滚动条消息
case WM_VSCROLL:
{
switch( LOWORD( wParam ) )
{
case SB_LINEUP://向下滚动一行
{
iVscrollPos -= 1;
break;
}
case SB_LINEDOWN://向上滚动一行
{
iVscrollPos += 1;
break;
}
case SB_PAGEUP://向下滚动一页
{
iVscrollPos -= cyClient / cyChar;
break;
}
case SB_PAGEDOWN://向上滚动一页
{
iVscrollPos += cyClient / cyChar;
break;
}
case SB_THUMBPOSITION://松开鼠标时,滑块的最终位置
{
iVscrollPos = HIWORD( wParam );
break;
}
default :
{
break;
}
}
//保证iVscrollPos在滚动条范围之内
iVscrollPos = max( 0, min( iVscrollPos, NUMLINES - 1 ) ) ;
if( iVscrollPos != GetScrollPos( hwnd, SB_VERT ) )//如果滚动条的位置变化了
{
SetScrollPos ( hwnd, SB_VERT, iVscrollPos, TRUE );//更新滚动条位置
InvalidateRect( hwnd, NULL, TRUE );//使整个窗口无效,并产生WM_PAINT消息
}
}
case WM_PAINT:
{
hdc = BeginPaint( hwnd, &ps );
for( i = 0; i < NUMLINES; i++ )
{
y = cyChar * ( i - iVscrollPos );
TextOut( hdc, 0, y,
sysmetrics[i].szLabel,
lstrlen(sysmetrics[i].szLabel) );
TextOut( hdc, 22 * cxCaps, y,
sysmetrics[i].szDesc,
lstrlen( sysmetrics[i].szDesc) );
SetTextAlign( hdc, TA_RIGHT | TA_TOP );
TextOut( hdc, 22 * cxCaps + 40 * cxChar, y, szBuffer,
wsprintf( szBuffer, TEXT("%5d"),
GetSystemMetrics( sysmetrics[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 );
}