Click to See Complete Forum and Search --> : Catching OnClose message for CDialogBar
I'm trying to catch a WM_CLOSE or WM_DESTROY message for a control bar, but
something is not right.
I have a class MyClass that inherits from CDialogBar. An instance of MyClass is created in a class CMainFrame that inherits from CMDIFrameWnd. The control bar is than created using CDialogBar's Create member function. ShowControlBar()
is also called to display the bar.
My class has the following message map, with the OnClose()hook implemented.
BEGIN_MESSAGE_MAP(CMyClass, CDialogBar)
ON_WM_CLOSE()
END_MESSAGE_MAP()
Since CDialogBar inherits from CWnd, one would expet that catching the WM_CLOSE
would work.
For some reason OnClose() is never called. Help!!!
YunFeiLi
May 26th, 1999, 05:13 AM
You must overide the CDialog::OnCancel().
just like this:
void CMyDialog::OnCancel()
{
//Do something.
CDialog::OnCancel();
}
Josh Handley
May 26th, 1999, 09:08 AM
The reason that you don't get the WM_CLOSE message is that the CDialogBar object doesn't get a close message. It is the parent window of the CDialogBar that gets the WM_CLOSE. The parent is a CMiniFrameWnd when the dialog bar is floating and something else when it is docked. When the parent gets the WM_CLOSE message it actually hides the dialog bar window. You can catch the hide message by handling the WM_WINDOWPOSCHANGED message. To do this add ON_WM_WINDOWPOSCHANGED() to the message map of your CDialogBar derived class and add the following method:
afx_msg void OnWindowPosChanged( WINDOWPOS* lpwndpos );
void CMyDialogBar::OnWindowPosChanged( WINDOWPOS* lpwndpos )
{
if (lpwndpos->flags & SWP_HIDEWINDOW)
{
// Add code to handle DialogBar close here.
}
CDialogBar::OnWindowPosChanged(lpwndpos);
}
OnWindowPosChanged is called not only to show/hide the window but alos to move it so you need to check the flags passed in to make sure it is really a window hide.
I tried what you said, but it didn't work. Note that my class inherits from CDialogBar, not CDialog. I used Spy++ to view messages that are sent to my
Control bar upon clicking on the close button. No WM_CLOSE, WM_DESTROY, etc
messages are sent to this window.
Josh Handley,
Your reply was excellent, I never would of thought of that. However I'm now encountering another problem. I need to distinguish between when the Control bar is being docked vs closed. The SWP_HIDEWINDOW flag seems to be set in both cases. I tried using CDialogBar's IsFloating(), but it didn't work because the Control bar is still floating when I'm processing the WM_WINDOWPOSCHANGED() message.
The whole purpose of this is the press or depress a button on a toolbar depending on the state of the CDialog bar.
Josh Handley
May 26th, 1999, 01:04 PM
I see the problem. In my app the dialog bar is undockable so I didn't run into this. If you look in the MFC source code for CDockBar::DockControlBar (in BARDOCK.CPP) you will see where it calls SetWindowPos to hide the control bar before it starts to dock it. It does this to avoid flashing while it changes the control bar window.
Instead of calling IsFloating you could try looking at the control bar style to see if the CBRS_FLOATING is set by calling GetBarStyle(). CDockBar::DockControlBar changes this flag just before it hides the window.
Hope this helps,
Josh
YunFeiLi
May 26th, 1999, 10:24 PM
if you want to close a dialog bar,you can use
CDialogBar::DestroyWindow() from any where you want.Because the dialog bar is not a dialog,
so when you click the close button,no WM_CLOSE,and
WM_DESTROY messages are sent to this window(Because no any function is called after you click this button!),You must add a function to handle this click message come from close button,and use DestroyWindow(...)to destroy this window.
Josh Handley,
You've been very helpfull so I thought I'd ask you one more quick little question. Prior to me calling ShowControlBar() I want to disable the WM_WINDOWPOSCHANGED message temporarilly for my CDialogBar, and then re-enable it in an OnTimer(). How do I do this?
This is were I am....
// Initialize dialog bar m_wndGraphicsLegend
//
if (!m_wndGraphicsLegend.Create(this, CG_IDD_GRAPHICSLEGEND,
CBRS_LEFT | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_HIDE_INPLACE,
CG_ID_VIEW_GRAPHICSLEGEND))
{
TRACE0("Failed to create dialog bar m_wndGraphicsLegend\n");
return;
}
m_wndGraphicsLegend.EnableDocking(CBRS_ALIGN_LEFT | CBRS_ALIGN_RIGHT);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndGraphicsLegend);
//
// I want to disable the WM_WINDOWPOSCHANGED message here.....
//
ShowControlBar(&m_wndGraphicsLegend, FALSE, FALSE);
SetTimer(1,200,NULL)
}
void CMainFrame::OnTimer(UINT nIDEvent)
{
KillTimer(1);
//
// Re-enable the WM_WINDOWPOSCHANGED message for m_wndGraphicsLegend
//
}
Josh Handley
May 28th, 1999, 07:59 AM
I don't know of a way to stop your dialog bar from receiving the message. Wouldn't the simplest thing be just to set a flag in your dialog bar before you call ShowControlBar, check this flag in your handler for WM_WINDOWPOSCHANGED and then clear the flag in the OnTimer?
Josh,
Once again thank you for your help. I just thought that maybe there was a neat way to disable the message. I did implement the flag method already...
Oleksii
March 16th, 2012, 04:34 AM
I see the problem. In my app the dialog bar is undockable so I didn't run into this. If you look in the MFC source code for CDockBar::DockControlBar (in BARDOCK.CPP) you will see where it calls SetWindowPos to hide the control bar before it starts to dock it. It does this to avoid flashing while it changes the control bar window.
And can you tell how to make undockable dialog bar? I want to dock it to the top of window in the code, and after that how can I make it impossible to dock-undock it with the mouse when app is running?
VictorN
March 16th, 2012, 05:44 AM
Have a look at Locking Rebars and Toolbars (http://www.codeguru.com/cpp/controls/toolbar/miscellaneous/article.php/c9599)
krmed
March 16th, 2012, 05:56 AM
And can you tell how to make undockable dialog bar? I want to dock it to the top of window in the code, and after that how can I make it impossible to dock-undock it with the mouse when app is running?
It would be better to start a new thread for your question rather than reviving a thread that's 13 years old!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.