Total Pageviews

Wednesday, May 12, 2010

Lesson 10 - Windows - How to change frame and caption window styles at run-time?

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

Code:

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).
Code:

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

No comments:

Post a Comment