程序代码:
#include <windows.h>
#include <string>
#include <vector>
#include "resource.h"
#pragma comment(lib, "winmm")
#define NUMLINES 1000
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
TCHAR szAppName[] = TEXT ("IconDemo") ;
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 (hInstance, MAKEINTRESOURCE (IDI_ICON1)) ;
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 ("窗口"),
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 ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
static int i=0 ,cxChar,cxCaps,cyChar,iMaxWidth,cxClient,cyClient,iVscrollPos;
static int nummessage=0;
static TCHAR szBuffer[NUMLINES][50];
static TCHAR *pmessage[]=
{
TEXT("WM_KEYDOWN"),TEXT("WM_KEYUP"),TEXT("WM_CHAR"),TEXT("WM_DEADCHAR"),
TEXT("WM_SYSKEYDOWN"),TEXT("WM_SYSKEYUP"),TEXT("WM_SYSCHAR"),TEXT("WM_SYSDEADCHAR")
};
// CreateWindow(TEXT("button"),TEXT(""))创建案件子窗口~~~
TEXTMETRIC tm;
SCROLLINFO si;
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) ;
return 0 ;
case WM_SIZE:
cxClient = LOWORD (lParam) ;
cyClient = HIWORD (lParam) ;
// Set vertical scroll bar range and page size
//Windows会把最大的滚动条位置限制为si.nMax - si.nPage +1
si.cbSize = sizeof (si) ;
si.fMask = SIF_RANGE | SIF_PAGE ;
si.nMin = 0 ;
si.nMax = NUMLINES - 1 ;
si.nPage = cyClient / cyChar ;
SetScrollInfo (hwnd, SB_VERT, &si, TRUE);
SetScrollPos (hwnd, SB_VERT, iVscrollPos, TRUE) ;
return 0 ;
case WM_VSCROLL:
switch (LOWORD (wParam))
{
case SB_TOP:
iVscrollPos = si.nMin ;
break ;
case SB_BOTTOM:
iVscrollPos = si.nMax ;
break ;
case SB_LINEUP:
iVscrollPos -= 1 ;
break ;
case SB_LINEDOWN:
iVscrollPos += 1 ;
break ;
case SB_PAGEUP:
iVscrollPos-= si.nPage ;
break ;
case SB_PAGEDOWN:
iVscrollPos += si.nPage ;
break ;
case SB_THUMBTRACK:
iVscrollPos = si.nTrackPos ;
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=GetDC(hwnd);
for (int i=0;i<nummessage;i++)
{
TextOut(hdc,0,i*cyChar,szBuffer[i],50);
}
ReleaseDC(hwnd,hdc);
}
return 0 ;
case WM_DESTROY :
PostQuitMessage (0) ;
return 0 ;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_CHAR:
case WM_DEADCHAR:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_SYSCHAR:
case WM_SYSDEADCHAR:
hdc=GetDC(hwnd);
wsprintf(szBuffer[nummessage],TEXT("%s---%d----%d"),pmessage[message-WM_KEYDOWN],wParam,lParam);
nummessage++;
if (nummessage>NUMLINES)
{
MessageBox(hwnd,0,0,0);
return 0;
}
ReleaseDC(hwnd,hdc);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
完整代码
[
本帖最后由 gwcome 于 2013-4-5 20:38 编辑 ]