1、滚动条:
/*滚动条*/
SetScrollRange(hwnd,iBar,iMin,iMax,bRedraw);
//iBar为B_VERT或者SB_HORZ;想要Windows根据新范围重画滚动条,则设置bRedraw为TRUE。
SetScrollPos(hwnd,iBar,iPos,bRedraw);
Windows对滚动条的处理:
1、处理所有滚动条鼠标事件;
2、当使用者在滚动条内单击鼠标时,提供一种「反相显示」的闪烁;
3、当使用者在滚动条内拖动卷动方块时,移动卷动方块;
4、为包含滚动条窗口的窗口消息处理程序发送滚动条消息。
程序员的工作:
1、初始化滚动条的范围和位置;
2、处理窗口消息处理程序的滚动条消息;
3、更新滚动条内卷动方块的位置;
4、更改显示区域的内容以响应对滚动条的更改。
/*滚动条消息:Windows给窗口消息处理程序发送WM_VSCROLL(供上下移动)和WM_HSCROLL(供左右移动)消
息。wParam消息参数被分为一个低字组和一个高字组。wParam的低字组是一个数值,它指出了鼠标对滚动条进行的操作。这个数值被看作一个「通知码」。通知码的值由以SB(代表「scroll bar(滚动条)」)*/
/*WINUSER.H中定义的通知码:*/
#define SB_LINEUP 0
#define SB_LINELEFT 0
#define SB_LINEDOWN 1
#define SB_LINERIGHT 1
#define SB_PAGEUP 2
#define SB_PAGELEFT 2
#define SB_PAGEDOWN 3
#define SB_PAGERIGHT 3
#define SB_THUMBPOSITION 4
#define SB_THUMBTRACK 5
#define SB_TOP 6
#define SB_LEFT 6
#define SB_BOTTOM 7
#define SB_RIGHT 7
#define SB_ENDSCROLL 8
/*SYSMETS2.C:增加垂直卷动功能*/
#include<window.h>
#include"sysmets.h" //这个在哪里?
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE PrevhInstance,
PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[] = TEXT("SysMets2");
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("Get System Metrics No.2"),
WS_OVERLAPPEDWINDOW|WS_VSCROLL,// 窗口内定的样式和垂直滚动条
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;
}
LRESULE CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static int cxChar,cxCaps,cyChar,cyClient,iVscrollPos; //iVscrollPos-滚动条内卷动方块的目前位置。
HDC hdc;
int i,y;
PAINTSTRUCT ps;
TCHAR szBuffer[10];
TEXTMETRIC tm;
swich(message);
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar = tm.tmAveCharWith;//平均水平宽度
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2)*cxChar/2; //与&1得值为1时得3,即150%的cxChar.
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 = max(0,min(iVscrollPos,NUMLINES-1));
if(iVscrollPos != GetScrollPos(hwmd,SB_VERT))
{
SetScorllPos (hwnd,SB_VERT,iVscrollPos,TRUE);
InvalidateRect (hwnd,NULL,TRUE);
}
return 0;
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].seDesc,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);
}
注意:SYSMETS1在处理WM_PAINT消息时,每一行的y坐标计算公式为cyChar*i;SYSMETS2则公式为:cyChar*(i-iVscrollPos).
单词:drag thumb拖拽滑块、redraw刷新屏幕、track轨道或旅行、
可编译代码:
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include"sysmets.h"
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("SysMets");
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_DBLCLKS; /* 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("SysMets"), /* Title Text */
WS_OVERLAPPEDWINDOW|WS_VSCROLL, /* 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,cyClient,iVscrollPos; //iVscrollPos-滚动条内卷动方块的目前位置。
HDC hdc;
int i,y;
PAINTSTRUCT ps;
TCHAR szBuffer[10];
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; //与&1得值为1时得3,即150%的cxChar.
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 = max(0,min(iVscrollPos,NUMLINES-1));
if(iVscrollPos != GetScrollPos(hwnd,SB_VERT))
{
SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
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); /* 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;
}