MFC General: How to change frame and caption window styles at run-time?

Q: How to change frame and caption window styles at run-time?
I want to remove at run-time the window caption (title bar). I called GetWindowLong to get the old style, removed the WS_CAPTION flag, then called SetWindowLong to set the new style. But this seems to have no effect. What must be done?


A: After 'SetWindowLong()' to set the new style you must call 'SetWindowPos()' with 'SWP_DRAWFRAME' or 'SWP_FRAMECHANGED' flag set.

void CMyWindow::RemoveCaption()
{
LONG nOldStyle
= ::GetWindowLong(m_hWnd, GWL_STYLE);
LONG nNewStyle = nOldStyle & ~WS_CAPTION;
::SetWindowLong(m_hWnd, GWL_STYLE, nNewStyle);
SetWindowPos(NULL, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
SWP_DRAWFRAME)
;
}

When using MFC, an easier way to modify window styles is to call 'CWnd::ModifyStyle()' or 'CWnd::ModifyStyleEx()' respectively.

Unfortunately, the first time programmers use these functions, most of them ignore the last parameter ('nFlags'). The solution is to pass 'SWP_DRAWFRAME' or 'SWP_FRAMECHANGED'.

If 'nFlags' is not zero, 'CWnd::ModifyStyle()'/'CWnd::ModifyStyleEx()' combine it with 'SWP_NOZORDER', 'SWP_NOMOVE', 'SWP_NOSIZE', and 'SWP_NOACTIVATE' flags, then call 'SetWindowPos()'.

The example below, shows/hides the control-menu box from title bar (adds/removes 'WS_SYSMENU' style).

void CMyWindow::ShowSystemMenu(bool bShow /*=true*/)
{
if(true == bShow)
{
ModifyStyle(
0, WS_SYSMENU, SWP_FRAMECHANGED); // show
}
else
{
ModifyStyle(WS_SYSMENU,
0, SWP_FRAMECHANGED); // remove
}
}

Note: Most of the material in this article is taken from codeguru.com


Recommended Reading :





Wills - Vegas Hotel - New York Hotels - Debt Help