定义
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
添加事件
BEGIN_MESSAGE_MAP(CLicenseModuleMgr, CDialog)
ON_WM_CTLCOLOR()
ON_WM_VSCROLL()
END_MESSAGE_MAP()
实现
初始化函数:
BOOL CxxxMgr::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//获取系统分辨率,判断是否显示进度条
int iWide = 0;
int iHeight = 0;
iWide = GetSystemMetrics(SM_CXSCREEN); //屏幕宽度
iHeight = GetSystemMetrics(SM_CYSCREEN); //屏幕高度
RECT rt;
SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&rt, 0); //获得工作区大小
//任务栏在下面的高度
int iTaskBottomHeight = iHeight - rt.bottom;
//计算当前屏幕高度
if ((iHeight - rt.bottom) > 0)
{
iHeight = rt.bottom;
}
//任务栏在上面的高度
if (rt.top > 0)
{
iHeight = iHeight - rt.top;
}
//获取当前窗口高度
CRect rect;
this->GetWindowRect(&rect);
SCROLLINFO vinfo;
vinfo.cbSize =sizeof(vinfo);
vinfo.fMask =SIF_ALL;
vinfo.nPage = 10;//滚动块自身的长短,通常有如下关系:其长度/滚动条长度(含两个箭头)=nPage/(nMax+2),
//另外nPage取值-1时,滚动条会不见了。
if (rect.Height() > iHeight - 10)
{
vinfo.nMax = 25;//滚动条所能滚动的最大值
vinfo.nMin = 5;//滚动条所能滚动的最小值
}
else
{
vinfo.nMax = 20;//滚动条所能滚动的最大值
vinfo.nMin = 20;//滚动条所能滚动的最小值
}
vinfo.nTrackPos =0;
vinfo.nPos = 0;
SetScrollInfo(SB_VERT | SBS_RIGHTALIGN, &vinfo);//即使上述步骤一不做,使用此条语句也可以显示滚动条
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
OnVScroll函数实现:
void CLicenseModuleMgr::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
SCROLLINFO scrollinfo;
GetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
switch (nSBCode)
{
case SB_BOTTOM: //滑块滚动到最底部
ScrollWindow(0,(scrollinfo.nPos-scrollinfo.nMax)*10); //滚动屏幕
scrollinfo.nPos = scrollinfo.nMax; //设定滑块新位置
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL); //更新滑块位置
break;
case SB_TOP: //滑块滚动到最顶部
ScrollWindow(0,(scrollinfo.nPos-scrollinfo.nMin)*10);
scrollinfo.nPos = scrollinfo.nMin;
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
break;
case SB_LINEUP: //单击上箭头
scrollinfo.nPos -= 1;
if (scrollinfo.nPos<scrollinfo.nMin)
{
scrollinfo.nPos = scrollinfo.nMin;
break;
}
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
ScrollWindow(0,10);
break;
case SB_LINEDOWN: //单击下箭头
scrollinfo.nPos += 1;
if (scrollinfo.nPos>scrollinfo.nMax)
{
scrollinfo.nPos = scrollinfo.nMax;
break;
}
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
ScrollWindow(0,-10);
break;
case SB_PAGEUP: //单击滑块上方空白区域
scrollinfo.nPos -= 5;
if (scrollinfo.nPos<scrollinfo.nMin)
{
scrollinfo.nPos = scrollinfo.nMin;
break;
}
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
ScrollWindow(0,10*5);
break;
case SB_PAGEDOWN: //单击滑块下方空白区域
scrollinfo.nPos += 5;
if (scrollinfo.nPos>scrollinfo.nMax)
{
scrollinfo.nPos = scrollinfo.nMax;
break;
}
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
ScrollWindow(0,-10*5);
break;
case SB_ENDSCROLL: //鼠标离开滑块,结束滑块拖动
// MessageBox("SB_ENDSCROLL");
break;
case SB_THUMBPOSITION:
// ScrollWindow(0,(scrollinfo.nPos-nPos)*10);
// scrollinfo.nPos = nPos;
// SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
break;
case SB_THUMBTRACK: //拖动滑块
ScrollWindow(0,(scrollinfo.nPos-nPos)*10);
scrollinfo.nPos = nPos;
SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
break;
}
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}