CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Hybrid View

  1. #1
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    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.

    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
       }
    }
    Last edited by Andreas Masur; September 18th, 2005 at 05:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured