Introduction
Here is a simple idea which brings a mole of relief to those who are uncomfortable with property pages. This trick which I used will allow you to make scrollable property pages within a property sheet. So now forget the worry of using too many controls within a property page and yet keeping them aligned. The simple idea it uses is changing the size of the property page dialog when it is Initialized.
Apart from that i have also given the code for setting up scroll bars in the code dynamically.
Background
When I started working with VC++ I had a dilemma with using tab controls and property pages and its pros and cons. Later when I used property pages I found myself struck up in a lurch. My application had lot of controls within a property page dialog, but not enough space for them. I searched a lot for making a property page scrollable but could not find one. Later I came up with a simple solution which I have presented here.
The Main Chunk
I will refer to the code taking my demo project as reference. The demo project is a dialog-based application. There are 2 property pages: 1) small property page and 2) sarge property page. A large amount of controls are added to large property page. It looks like this :
Now open the resource file of the application into a text editor. Change the width and height of larger property page (IDD_PROPPAGE_LARGE
) to same as that of small property page (IDD_PROPPAGE_SMALL
). Finally, the large property page dialog will look like this in your VC++ editor.
Now When this Large property page dialog is initialized set the size of the larger property page such that it covers all the required controls. Get the size of the rectangle covering the property page and multiply it to the x
variable. This x
variable should be such that all the controls in your property page are visible.
BOOL CProppageLarge::OnInitDialog()
{
Cpropertypage::OnInitDialog();
m_nVscrollPos = 0;
GetClientRect(&m_ClientRect);
m_ClientRect.bottom *= 3;
ResetScrollbars();
SetupScrollbars();
return TRUE;
}
Now when the larger property page is scrolled it should be handled via a WM_VSCROLL
message as below:
#define VERT_PTS 5
void CProppageLarge::OnVScroll(UINT nSBCode, UINT nPos,
CScrollBar* pScrollBar)
{
int nInc;
switch (nSBCode)
{
case SB_TOP: nInc = -m_nVscrollPos; break;
case SB_BOTTOM: nInc = m_nVscrollMax-m_nVscrollPos; break;
case SB_LINEUP: nInc = -1; break;
case SB_LINEDOWN: nInc = 1; break;
case SB_PAGEUP: nInc = min(-1, -m_nVertInc); break;
case SB_PAGEDOWN: nInc = max(1, m_nVertInc); break;
case SB_THUMBTRACK: nInc = nPos - m_nVscrollPos; break;
default: nInc = 0;
}
nInc = max(-m_nVscrollPos, min(nInc, m_nVscrollMax - m_nVscrollPos));
if (nInc)
{
m_nVscrollPos += nInc;
int iMove = -VERT_PTS * nInc;
ScrollWindow(0, iMove, NULL, NULL);
SetScrollPos(SB_VERT, m_nVscrollPos, TRUE);
}
Cpropertypage::OnVScroll(nSBCode, nPos, pScrollBar);
}
Here I have added the handler only for vertical scrolling but it can be updated for horizontal scrolling or both as per your need in the type of property page.
Conclusion
This is my first article on CodeProject and I have yet to learn a lot. I would definitely like to thank Mr. Trilok Soni (Tectona SoftSolutions(P) Ltd) for inspiring me to write articles and Mr. Paras Shah (VIP Benefits) for giving a major spur to this article. I assure all of you of very interesting articles in the very near future. I would definitely like to hear valuable suggestions from not only those who use this but all the senior programmers too, as it will improve my learning curve.
Jemin is Working as Software Engineer in Tectona SoftSolutions(P) Ltd, Ahmedabad.