为windows32窗体加入滚动条,代码如下:
#include<windows.h>
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
static TCHAR szAppName[]=TEXT("AppName");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WindowProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass)){
MessageBox(NULL,TEXT("This program requires Windows NT"),szAppName,MB_ICONERROR);
}
hwnd=CreateWindow(
szAppName, // registered class name
TEXT("This is title"), // window name
WS_OVERLAPPEDWINDOW|WS_VSCROLL, // window style
CW_USEDEFAULT, // horizontal position of window
CW_USEDEFAULT, // vertical position of window
CW_USEDEFAULT, // window width
CW_USEDEFAULT, // window height
NULL, // handle to parent or owner window
NULL, // menu handle or child identifier
hInstance, // handle to application instance
NULL // window-creation data
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static int cxClient,cyClient,cxChar,cyChar,iVscrollPos;
HDC hdc;
PAINTSTRUCT ps;
int i=50,j=20;
TCHAR szBuffer[20];
TEXTMETRIC tm;
int numberline=30;
switch(uMsg){
case WM_CREATE:
//hdc=GetDC(hwnd);
//TextOut(hdc,0,0,TEXT("hello,world"),strlen("hello,world"));
//ReleaseDC(hwnd,hdc);
hdc=BeginPaint(hwnd,&ps);
GetTextMetrics(hdc,&tm);
SetScrollRange(hwnd,SB_VERT,0,numberline-1,FALSE);
SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
cyChar=(tm.tmHeight+tm.tmExternalLeading);
EndPaint(hwnd,&ps);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetTextMetrics(hdc,&tm);
TextOut(hdc,iVscrollPos,iVscrollPos,szBuffer,wsprintf(szBuffer,TEXT("%d"),tm.tmAveCharWidth));//字符宽度
TextOut(hdc,iVscrollPos+30,iVscrollPos+40,szBuffer,wsprintf(szBuffer,TEXT("%d"),tm.tmHeight+tm.tmExternalLeading));//字符长度
EndPaint(hwnd,&ps);
return 0;
case WM_VSCROLL:
switch(LOWORD(wParam)){
case SB_LINEUP:
iVscrollPos-=100;//为了效果更明显由iVscrollPos-=1;改成了iVscrollPos-=100;
break;
case SB_LINEDOWN:
iVscrollPos+=100;
break;
case SB_PAGEUP:
iVscrollPos-=cyClient/cyChar;//cyClient/cyChar表示一页最多几行或一列最多包括多少个字符
break;
case SB_PAGEDOWN:
iVscrollPos+=cyClient/cyChar;
break;
case SB_THUMBPOSITION:
iVscrollPos=HIWORD(wParam);
break;
default:
break;
}
iVscrollPos=max(0,min(iVscrollPos,numberline-1));
if(iVscrollPos!=GetScrollPos(hwnd,SB_VERT)){
SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
case WM_SIZE:
cyClient=HIWORD(lParam);
return 0;
/*case WM_SIZE:
hdc=BeginPaint(hwnd,&ps);
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
GetTextMetrics(hdc,&tm);
TextOut(hdc,0,100,szBuffer,wsprintf(szBuffer,TEXT("%d"),cxClient/tm.tmAveCharWidth));//一行最多显示多少个字符
TextOut(hdc,0,150,szBuffer,wsprintf(szBuffer,TEXT("%d"),cyClient/(tm.tmHeight+tm.tmExternalLeading)));//一列最多显示多少字符
EndPaint(hwnd,&ps);
return 0;*/
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
截图如下: