首页 新闻 会员 周边 捐助

ScrollWindow函数的使用

0
悬赏园豆:15 [待解决问题]

请高手看一下下面代码绿色注释部分为何只用ScrollWindow函数时,在WM_PAINT消息中输出的信息中不能更新显示的滚动条位置信息,而用InvalidateRect函数时就显示正常?

代码可以直接在一个Windows程序中测试的。
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

int i=1;int iStrLength;WCHAR szBuffer[20];
static HDC hdc;
static PAINTSTRUCT ps;
static int xChar,yChar,xUpper;
TEXTMETRIC tm;
static RECT rect;
static int cx,cy;//x,y
用于保存获得的矩形区域的左上角顶点;
static int xClient,yClient;//cxClient,yClient
用于保存获得的矩形区域的宽度和高度;
//
设置与滚动条有关信息:
int x,y;
static int xPos; // current horizontal scrolling position
static int yPos; // current vertical scrolling position
SCROLLINFO si;
int temp;
#define LINES 50
static TCHAR *abc[] = {
TEXT("1"),TEXT("2"),TEXT("3"),TEXT("4"),TEXT("5"),
TEXT("6"),TEXT("7"),TEXT("8"),TEXT("9"),TEXT("10"),
TEXT("11"),TEXT("12"),TEXT("13"), TEXT("14"),TEXT("15"),
TEXT("16"),TEXT("17"),TEXT("18"),TEXT("19"),TEXT("20"),
TEXT("21"),TEXT("22"),TEXT("23"),TEXT("24"),TEXT("25"),
TEXT("26"),TEXT("27"),TEXT("28"),TEXT("29"),TEXT("30"),
TEXT("31"),TEXT("32"),TEXT("33"),TEXT("34"),TEXT("35"),
TEXT("36"),TEXT("37"),TEXT("38"),TEXT("39"),TEXT("40"),
TEXT("41"),TEXT("42"),TEXT("43"),TEXT("44"),TEXT("45"),
TEXT("46"),TEXT("47"),TEXT("48"),TEXT("49"),TEXT("50")
};
HRESULT hr;
size_t abcLength; // length of an abc[] item
int FirstLine; // first line in the invalidated area
int LastLine; // last line in the invalidated area
POINT pt;

switch (message)
{
case WM_CREATE:
hdc=GetDC(hWnd);
GetTextMetrics(hdc,&tm);
xChar=tm.tmAveCharWidth;
xUpper=(tm.tmPitchAndFamily?3:2)/2*xChar;
yChar=tm.tmExternalLeading+tm.tmHeight;
ReleaseDC(hWnd,hdc);

return 0;
case WM_SIZE:
xClient=LOWORD(lParam);
yClient=HIWORD(lParam);
si.cbSize=sizeof(SCROLLINFO);
si.fMask=SIF_RANGE|SIF_PAGE;
si.nMin=0;
si.nMax=LINES-1;
si.nPage=yClient/yChar;
SetScrollInfo(hWnd,SB_VERT,&si,TRUE);
case WM_VSCROLL:
si.cbSize=sizeof(SCROLLINFO);
si.fMask=SIF_ALL;
GetScrollInfo(hWnd,SB_VERT,&si);
yPos=si.nPos;
switch(LOWORD(wParam))
{
case SB_TOP:
si.nPos=si.nMin;
break;
case SB_BOTTOM:
si.nPos=si.nMax;
break;
case SB_LINEUP:
si.nPos-=1;
break;
case SB_LINEDOWN:
si.nPos+=1;
break;
case SB_PAGEUP:
si.nPos-=si.nPage;
break;
case SB_PAGEDOWN:
si.nPos+=si.nPage;
break;
case SB_THUMBTRACK:
si.nPos=si.nTrackPos;
break;
default:
break;
}
si.fMask = SIF_POS;
SetScrollInfo (hWnd, SB_VERT, &si, TRUE);
GetScrollInfo (hWnd, SB_VERT, &si);

if(si.nPos!=yPos)
{
ScrollWindow(hWnd,0,yChar*(yPos-si.nPos),NULL,NULL);
//InvalidateRect(hWnd,NULL,TRUE);//
用此句命令时,客户区可以正确显示滚动条位置;
UpdateWindow(hWnd);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
si.cbSize=sizeof(SCROLLINFO);
si.fMask=SIF_POS;
GetScrollInfo(hWnd,SB_VERT,&si);
yPos=si.nPos;
FirstLine = max (0, yPos + ps.rcPaint.top / yChar);
LastLine = min (LINES - 1, yPos + ps.rcPaint.bottom / yChar);
for(i=0;i<LINES;i++)
{
x =0 ;
y = yChar * (i - yPos);
TextOut(hdc, x, y, abc[i], lstrlen(abc[i]));


}
si.fMask = SIF_POS;
GetScrollInfo (hWnd, SB_VERT, &si);
wsprintf(szBuffer,TEXT("
滚动条当前位置为%d"),si.nPos);
TextOut(hdc,50,100-yChar*yPos, szBuffer,lstrlen(szBuffer));
EndPaint(hWnd, &ps);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

C++
烨杰的主页 烨杰 | 初学一级 | 园豆:187
提问于:2012-07-01 23:59
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册