Click here to Skip to main content
Licence 
First Posted 6 May 2003
Views 163,414
Bookmarked 65 times

How to implement a scrollable child dialog

By | 11 May 2003 | Article
How to implement a scrollable child dialog

Sample Image - ScrollableChildDialog.jpg

Introduction

Many times it happened to me (and I hope to you too!) the need for a kind of child dialog, to use as a control inside a more bigger dialog or window or view etc. Filling this child dialog of controls is easy. Set the size of it to fit inside the parent window is not so easy. So, here is the scrollable child dialog, which i found veeeeery useful in my applications.

The code

The code is really simple and is strongly based on the MSDN article HOWTO: Create a Resizable Dialog Box with Scroll Bars (Q262954). I simply created the scrollable dialog template choosing the WS_CHILD and WS_VSCROLL styles and used class wizard to add WM_SIZE, WM_MOUSEWHEEL and WM_VSCROLL messages. To use the code in your project follow these steps:

  1. Add dlgscrollable.h and dlgscrollable.cpp to your project
  2. Create your own dialog resource template and replace the resource identifier in the header file
  3. In the parent window, add a member variable like CDlgScrollable* m_pdlgScrollable and in the OnInitDialog function, create the new CDlgScrollable object.
  4. In the parent dialog resource template, you can use a static as a placeholder to calc the rectangle of my scrollable child control.
  5. You can easily change the "hand" cursors, loading different resources as needed

It's obviously really simple add support for horizontal scroll, it's symmetric and I didn't really need that... :P

And so...

That's all! I'm sorry for my bad English (I'm Italian), this is my first article. Hope not my last one! Thanx to DD and GM for their suggestions and comments, this is the last version of the code, or at least the last version for the next two months! Have fun!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Massimiliano Conte

Software Developer (Senior)
Selex SI
Italy Italy

Member

Hi Smile | :)
I was born in 1970 (Augusta - Italy).
I live in Taranto - Italy.
I work in Taranto - Italy.
I like computer science!!!
That's all!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralExplanation of some magic numbers PinmemberBati910:50 2 Dec '08  
GeneralRe: Explanation of some magic numbers PinmemberMassimiliano Conte20:57 2 Dec '08  
QuestionEvent handling in the parent Dialog? PinmemberSeife3:59 13 Sep '07  
Questionpb when using your code !!! [modified] Pinmemberaabdoos1:43 15 Dec '06  
QuestionIDC_PLACEHOLDER what's it? Pinmembertlbf2:16 10 Nov '06  
AnswerRe: IDC_PLACEHOLDER what's it? PinmemberJonathanLivingstone3:02 10 Nov '06  
QuestionCenter dialog on startup? Pinmemberrturrentine6:03 25 May '05  
GeneralSome notes and adjustements PinmemberDavide Zaccanti20:32 27 Mar '05  
I've found this code searching for a solution simply to use and not so complex as CRollupCtrl (also in CodeProject).
 
I need to add some other fields (rarely used and loaded automatically) to a CDialog that is already dimensioned at maximum screeb size for our specification.
 
This code is very simple and can inserted in few cut and paste, but:
 
1) I need to scroll automatically when users move with TAB os Shift+TAB
2) My control is a part of a more complex dialog and I need to maintain TAB sequence between scrolling dialog and main dialog
 
Ok, these doenst requires long work, but I want to share this enhancements, maybe some find it usefull...
 
1) Set "Control" property in dialog design to TRUE and after child creation insert a
::SetWindowPos( m_pdlgScroll->GetSafeHwnd(), GetDlgItem( ID_PREVIUOUS_CONTROL)->GetSafeHwnd(),
0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
where ID_PREVIOUS_CONTROL is the ID of any control already present in parend dialog (in the sample you can try with IDCANCEL)
 
2) This is a little more tricky, and can depend on controls present in child window, in my case EDITs and BUTTONs.
I've added two notification handler
ON_CONTROL_RANGE(EN_SETFOCUS, 0, 0xFFFFFFFF, OnSetFocus)
ON_CONTROL_RANGE(BN_SETFOCUS, 0, 0xFFFFFFFF, OnSetFocus)
(remember that buttons have notification disabled by default so you need to activate in dialog design mode)
 
void CDlgScrollable::OnSetFocus( UINT nID)
{
CWnd * pWnd = GetDlgItem( nID);
if ( pWnd == NULL)
return;
CRect rc;
 
pWnd->GetWindowRect( rc);
ScreenToClient( &rc);
 
// We don't need to scroll...
if ( rc.top >= 0 && rc.bottom <= m_nCurHeight)
return;
 
// This is a check since I have edit that can be longer than the child window
if ( rc.Height() > m_nCurHeight && ( rc.top >= 0 || rc.bottom <= m_nCurHeight))
return;
 
// The first 2 cases just to have a smooth scroll, maybe you want less scrolls and use directy 3rd option
int nTop;

if ( rc.bottom > m_nCurHeight && ( rc.bottom - m_nCurHeight) < m_nCurHeight / 4) {
nTop = max( 0, m_nScrollPos +( rc.bottom - m_nCurHeight) +3);
TRACE( "Scoll in basso - top a %d\n", nTop);
 
} else if ( rc.top < 0 && abs( rc.top) < m_nCurHeight / 4) {
nTop = max( 0, m_nScrollPos +rc.top -3);
TRACE( "Scoll in alto - top a %d\n", nTop);

} else {
nTop = max( 0, rc.top +m_nScrollPos -3);
TRACE( "Riposiziona top a %d\n", nTop);
}
 
// Sure not to scroll to much (ie in last line) and leave at least one page
if ( nTop +m_nCurHeight > m_rcOriginalRect.Height())
nTop = m_rcOriginalRect.Height() -m_nCurHeight;
 
// Just scroll
int nDelta = nTop - m_nScrollPos;

m_nScrollPos += nDelta;

SetScrollPos( SB_VERT, m_nScrollPos, TRUE);
ScrollWindow( 0, -nDelta);
}
 
Hope this help.. and thanxs to the author.
 

 

GeneralGreat but... Pinmemberpgiustinoni14:01 28 Feb '05  
GeneralResizing original dialog Pinmemberandrew_lowry3:50 15 Apr '04  
Generalfix for embedded visual c++ 4.0 PinmemberSpellcaster7:42 14 Mar '04  
QuestionHow can I implement this for a Dialog and not a for a child of a Dialog? PinmemberDanYELL7:54 8 Feb '04  
AnswerRe: How can I implement this for a Dialog and not a for a child of a Dialog? PinmemberJonathanLivingstone22:46 8 Feb '04  
QuestionHow can I get this to work with a CFormView? PinmemberDanYELL6:05 6 Feb '04  
GeneralTab Key and Scroll Dialog box PinsussAnonymous22:10 28 Jan '04  
Generalscrollbar position on start Pinmemberteamghost1:47 16 Oct '03  
Generalit helped me Pinmembersanskypotov0:01 3 Sep '03  
GeneralRe: it helped me PinmemberNiFF6:07 21 Jun '05  
GeneralExact same problem as Keith Pinmemberyklam5:00 15 Jul '03  
GeneralSolution Found Pinmemberyklam4:50 21 Jul '03  
QuestionHow to use this powerful class in embedded visual c++ 3.0? Pinmemberkeithse11:38 26 Jun '03  
AnswerRe: How to use this powerful class in embedded visual c++ 3.0? PinmemberKaiserle3:44 21 Jul '03  
GeneralMousewheel does not work. PinmemberBrian van der Beek5:08 22 May '03  
GeneralRe: Mousewheel does not work. PinmemberJonathanLivingstone1:40 23 May '03  
GeneralRe: Mousewheel does not work. Pinmembersonu_2241:50 13 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 12 May 2003
Article Copyright 2003 by Massimiliano Conte
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid