在做TabControl时,有一个子窗口内容很多,不能滚动,于是遇到了如何让子窗口滚动的问题。可能是许久不动VC的缘故,做起来有些木讷了:)
知道了做在CDialog里的WS_VSCORLL里就成了,反正不又不是什么创造性的劳动,参考了网上朋友的Code,(那人说他是参考的CodeProject的内容,“拿来主义”搞一下好了),做啥事情,只看结果不是(没有结果啥都不是:))。
页面的地址: http://blog.sina.com.cn/s/blog_53b603930100aadr.html
http://www.codeproject.com/dialog/scrollablechilddialog.asp
以下就是那页面的内容了:
HOWTO: Create a Resizeable Dialog Box with Scroll Bars
Q262954
--------------------------------------------------------------------------------
The information in this article applies to:
The Microsoft Foundation Classes (MFC), included with:
Microsoft Visual C++, 32-bit Enterprise Edition, version 4.2
Microsoft Visual C++, 32-bit Professional Edition, version 4.2
Microsoft Visual C++, 32-bit Enterprise Edition, version 5.0
Microsoft Visual C++, 32-bit Professional Edition, version 5.0
Microsoft Visual C++, 32-bit Enterprise Edition, version 6.0
Microsoft Visual C++, 32-bit Professional Edition, version 6.0
Microsoft Visual C++, 32-bit Learning Edition, version 6.0
--------------------------------------------------------------------------------
SUMMARY
This article describes how to create a resizeable dialog box with scroll bars. The process consists of four basic parts:
In Resource Editor, select the Horizontal Scroll and Vertical Scroll styles in the properties of the dialog box.
Select the Resizing Border style to make the dialog box resizeable.
Override the WM_VSCROLL and WM_HSCROLL message handlers.
Override the WM_SIZE message handler to set the scroll bar range if the size is reduced to smaller than the original size.
MORE INFORMATION
To create a resizeable dialog box with a vertical scroll bar, perform the following steps:
Use App Wizard to create a Microsoft Foundation Classes (MFC) dialog-based application.
In Resource Editor, add some controls to the dialog resource template, select Vertical Scroll in the properties of the dialog box, and choose Resizing as the Border style.
Add the following protected member variables to your dialog class:
int m_nCurHeight;
int m_nScrollPos;
CRect m_rect;
Use m_nScrollPos to store the current vertical scroll position. Use m_nCurHeight to store the current height of the dialog box, and to handle the scrolling in the OnVScroll method.
To get the original window size, add the following line to the OnInitDialog method:
GetWindowRect(m_rect);
m_nScrollPos = 0;
Add a message handler to the OnSize method for the WM_SIZE message to set the scroll bar range. Set the range to 0 if the size is increased to more than the original size.
void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here.
m_nCurHeight = cy;
int nScrollMax;
if (cy < m_rect.Height())
{
nScrollMax = m_rect.Height() - cy;
}
else
nScrollMax = 0;
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL; // SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS;
si.nMin = 0;
si.nMax = nScrollMax;
si.nPage = si.nMax/10;
si.nPos = 0;
SetScrollInfo(SB_VERT, &si, TRUE);
}
Add a message handler for the WM_VSCROLL message to the OnVScroll method:
void CTestDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default.
int nDelta;
int nMaxPos = m_rect.Height() - m_nCurHeight;
switch (nSBCode)
{
case SB_LINEDOWN:
if (m_nScrollPos >= nMaxPos)
return;
nDelta = min(nMaxPos/100,nMaxPos-m_nScrollPos);
break;
case SB_LINEUP:
if (m_nScrollPos <= 0)
return;
nDelta = -min(nMaxPos/100,m_nScrollPos);
break;
case SB_PAGEDOWN:
if (m_nScrollPos >= nMaxPos)
return;
nDelta = min(nMaxPos/10,nMaxPos-m_nScrollPos);
break;
case SB_THUMBPOSITION:
nDelta = (int)nPos - m_nScrollPos;
break;
case SB_PAGEUP:
if (m_nScrollPos <= 0)
return;
nDelta = -min(nMaxPos/10,m_nScrollPos);
break;
default:
return;
}
m_nScrollPos += nDelta;
SetScrollPos(SB_VERT,m_nScrollPos,TRUE);
ScrollWindow(0,-nDelta);
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
Build and run the application. Resize the dialog box to show the vertical scroll bar.
For horizontal scrolling, add the WM_HSCROLL message handler in a similar way, and add the necessary code to the OnSize and OnInitDialog methods.
Additional query words:
Keywords : kbDlg kbMFC kbScrollBar kbGrpDSMFCATL
Issue type : kbhowto
Technology : kbAudDeveloper kbMFC