Click here to Skip to main content
Click here to Skip to main content

Sliding Dialog Windows

By , 13 Feb 2001
 

Sample Image - SlidingDialog.jpg

Introduction

This Source Code is for building a sliding dialog, one that slide's out from under another dialog. I have seen someone who already did it, but this code is easier to understand and I have implemented it in a different (and better) way. The OnTop class can be used with any Dialog. There can even be more than one sliding dialog associated with parent-dialog.

Steps To A Working Sliding Dialog

  1. Create a MFC dialog based application.
  2. Create a dialog resource using the resource editor, and then create the dialog class to associate with the dialog resource using class wizard.
  3. Add SlidingSon.h and SlidingSon.cpp to your Workspace
  4. Add a SlideSon.h include directive to your dialog class header.
  5. In your newlly created dialog class replace the base class in the associated header file to CSlideSon from CDialog.
  6. Do the same thing in the dialog's implementation file. Do not forget to replace class name in the BEGIN_MESSAGE_MAP declaration.
  7. In order to make this dialog modeless, add a create member function to your dialog header with no parameters.
  8. In the dialog's implementation file add the create function as follows:
    return CSlideSon::Create(<<Your dialog class name>>::IDD);
  9. Add a button to your main application dialog to open / close the sliding dialog, or use any other event to open the sliding dialog.
  10. I recommend adding a boolean value to your main application dialog class which will indicate the state of the SlidingSon. Because we create a modeless dialog, you will have to add a member variable that will point to your sliding dialog class object.
    <<Your sliding dialog class name>>* m_pModeless;
  11. When you wish to slide the dialog out, use the following code in your application dialog event handlers:
    m_pModeless = new <<Your sliding dialog class name>>(this);
    if (<<Your sliding dialog class name>> ->GetSafeHwnd()==0)
    {
       m_pModeless->Create();
       m_pModeless->SetSlideSpeed(10);
       m_pModeless->SetSlideDirection(RIGHT);
       m_pModeless->StartSlide();
    }
  12. Note that you can control the sliding speed with integer values and slide direction (RIGHT, LEFT)
  13. When you wish to close the dialog use the following code:
    m_pModeless->Close();
    delete m_pModelles;
  14. To slide the dialog back when you press the cancel\ok button of the sliding dialog, add the following line after
    ON_MESSAGE(WM_CLOSE_SLIDING,OnClose)  // OnClose or any event handling 
                                          // function
  15. You can change the ratio between the sliding dialog and its father in the SlidingSon.cpp
  16. Catch the OnMove() event for the main application dialog, and call m_pModelled->RePosition(), to update the sliding dialog’s position.

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

Ran
Israel Israel
Member
C++ Developer

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCould you please tell me how to slide multiple dialog in mfc.membernarmada Padhy10 Feb '11 - 0:01 
Could you please tell me how to slide multiple dialog in mfc. i-e let us suppose we have two dialog dialog1 and dialo2. i want when i click on left dialog1 will move to left and dialog2 will come in place on dialog1. Cry | :((
Generalbugmemberguyuewuhua13 Aug '09 - 23:18 
if you have three dialogs,it does't work ,when your have an child dialog,and the child dialog work with the Slide Dialog!
QuestionRestore Dialog doesnt workmemberSiHot3 Jul '07 - 3:42 
Hello u did a great job!
 
I got a little question,
 
after i open the slidedialog i cant restore my mainapplication
if it lost focus and has been sent to taskbar.
 
How to fix it? Or do i have to keep it "ontop" once i did slide
the dialog out?
 
Thanks,
SiHot
GeneralVery Nice Application....memberRajeevSahu23 May '07 - 17:45 
Hi... the application is very good... But if some one wants that the window slides downwards then what should be done... Using this current application we can only make the dialog to slide right way(->)... but i want to know is it possible to slide it downwards... If yes then kindly suggests me some possibilities...or some codes that i'll insert to slide it downwards...ThanksSmile | :)
 
Rajeev

Generaltop and bottommemberpekto28 Nov '06 - 21:47 
what if you want the son to move from the top or the bottom?
GeneralNeat codes!memberIntelliBot3 Oct '06 - 17:32 
The codes are very nicely done!
I tried setting speed to -ve, and the program hangs.. No offence!Roll eyes | :rolleyes:
 
"Ask and you shall be given, Seek and you shall find, Knock and the door shall be open unto you"
Matthew 7:7

GeneralGood Work...membersony_synco12 Aug '06 - 4:15 
Thanks.. Cuz of using your Classes. Big Grin | :-D
 
R. MalboosBaf
GeneralNice, but little bugmembersheny11 Jul '06 - 21:12 
1.Try to set sliding position to LEFT
2.Move the main dialog
3.See what happens
 
a solution is to update CSlideSon::RePosition()
 
BOOL CSlideSon::RePosition()
{
m_pDlg->GetWindowRect(&m_MyPos);

int xPos;

switch (m_SlideDirection)
{
case RIGHT:
xPos = m_MyPos.right ;
SetWindowPos(&wndTop,xPos,m_MyPos.top+4,OrigPos.Width(),m_MyPos.Height()-8,SWP_SHOWWINDOW);
break;
case LEFT:
xPos = m_MyPos.left-OrigPos.Width();
SetWindowPos(&wndTop,xPos,m_MyPos.top+4,OrigPos.Width(),m_MyPos.Height()-8,SWP_SHOWWINDOW);
break;
}

return TRUE;
}
 

GeneralRe: Nice, but little bugmemberIntelliBot3 Oct '06 - 17:36 
Tested. Ya, you're right! RePosition() only cater for right movement!
Thanks for the additional codes.

 
"Ask and you shall be given, Seek and you shall find, Knock and the door shall be open unto you"
Matthew 7:7

GeneralAlso see AnimateWindow() - Win2K onlymemberRavi Bhavnani9 Oct '01 - 3:46 
Nice job!
 
On Win2K (and XP), AnimateWindow() can also be used to provide sliding and "unfurl" effects of various types a la the MSVC++ 6.0 splash screen.
 
/ravi
 
"There is always one more bug..."
http://www.ravib.com
ravib@ravib.com
GeneralGreat Job!memberRobJones25 Sep '01 - 10:31 
Very good job.. This will work great in a little app I have! I was just wondering.. Is there a way to permentaly attach the second dialog to the main window? So if you grab the second dialog it drags both?
 
Keep up the great coding Smile | :)
 
Rob
GeneralRe: Great Job!membernarmada Padhy9 Feb '11 - 22:04 
Could you please tell me how to slide multiple dialog. i-e let us suppose we have two dialog dialog1 and dialo2. i want when i click on left dialog1 will move to left and dialog2 will come in place on dialog1.
GeneralRe: Great Job!membernarmada Padhy9 Feb '11 - 22:05 
Could you please tell me how to slide multiple dialog in mfc. i-e let us suppose we have two dialog dialog1 and dialo2. i want when i click on left dialog1 will move to left and dialog2 will come in place on dialog1.
GeneralSize of the sliding windowmemberAnonymous1 Aug '01 - 4:23 
Let's say I want to slide out a dialog to the right. The width of the sliding window is bigger than the width of the dialog. Firstly, a part of the sliding window appears on the left side of the dialog. Can u hide that part of the sliding window somehow?
 

 
Stefan
GeneralSize of the sliding windowmemberAnonymous1 Aug '01 - 4:21 
Let's say I want to slide out a dialog to the right. The width of the sliding window is bigger than the width of the dialog. Then, there firstly appears a part of the sliding window on the left side of the dialog. Can u hide that part of the sliding window somehow?
 

 
Stefan
GeneralWM_CLOSE_SLIDINGmemberDesmond28 Feb '01 - 16:14 
Nice work! Wink | ;)
 
Btw, I've encountered a problem. When I close the sliding dialog for the second time (using the OK button on the sliding dialog), the application crashed. Any ideas? Confused | :confused:
 
Desmond
GeneralRe: WM_CLOSE_SLIDINGmemberNewton111 May '01 - 10:10 
Hi Desmond,
 
I believe you need to do the following m_pModelles = 0; in the OnClose() function.
What is happening is when the OnDestroy() function gets called and m_pModelles hasn't been set to '0'(NULL) it tries to delete the pointer again, which throws an exception.
 
Hopes this helps.
 
Sincerely,
Newton1
GeneralMemory leak detectedmemberNewton114 Feb '01 - 18:39 
Nice job! Very smooth transition.
 
One little problem occurs if the slider window is in the opened state and you click on OK or Cancel from the parent dialog. You will receive a small memory leak. All that is needed is to implement the following code (Override DestroyWindow) . Another thing is in your constructor initialize m_pModelles = 0; this way the following conditional if() statement will work just dandy.
 
BOOL CSlidingDemoDlg::DestroyWindow()
{
if(m_pModelles)
{
delete m_pModelles;
m_pModelles = 0; //safety from those danglers
}
return CDialog::DestroyWindow();
}
 
Once again, nice job.
 
Sincerely,
Newton1
 

GeneralRe: Memory leak detectedmemberAnonymous6 May '01 - 17:36 
If you want to delete m_pModelles, you have to use new operator before...
This is the sample code...
BOOL CSlidingDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
 
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
m_pModelles = new CSlide;

return TRUE; // return TRUE unless you set the focus to a control
}

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 14 Feb 2001
Article Copyright 2001 by Ran
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid