How to remove the Close button in a floating bar

Source: Internet
Author: User
Tags bool

In the C++/MFC framework application, when the floating bar is dragged off the main window, there is generally a "close" button, as shown in Figure one:

Figure I

Originally this button was designed in accordance with the UI design specifications of the Windows application, which means that all windows should have a place for users to close the window. But some users do not want this "close" button. Since the user has this need, then we will find a way to achieve it ...

First, we will determine the implementation of the idea, because we want to implement the function is related to the tool bar, so the class involved is certainly related to CToolBar. Secondly, in order to judge the state of the "close" button after the bar is floating, we need a member variable to represent the State; Thirdly, to implement the floating characteristics of the sidebar, it is necessary to handle the on_wm_windowposchanged message of the tool bar window. This message is responsible for CToolBar window size, position, or Z coordinate changes, why not use wm_size/onsize to handle the window size change? This is only called when the window size changes and not the window position is changed, so it is not always necessary to call the handle wm_size/onsize when the floating toolbar is changed. We can use the m_pDockBar member of the CControlBar base class to invoke GetParent () to get the toolbar's parent window. Thus changing the style of the parent window--The Screen system menu--to achieve our purpose--Remove the "close" button.

With the idea, let's look at the specific implementation code, we first derive a new class Ctoolbarex from CToolBar, since our goal is to remove the "close" button while floating the toolbar, so just judge if the bar is floating. This can be done with the attribute-judging function of the CControlBar class: BOOL IsFloating() const;    Add a member variable to the new class that represents the state of the "close" button: BOOL m_bMenuRemoved; when we remove the System menu attribute from the main frame, its value is true.

Next, we need a pointer to the m_pDockBar member's parent window to check whether it is really cdockframewnd, and we use m_pDockBar to determine the parent window type, and to ensure that it is a valid pointer. This allows us to safely remove the system menu from the CToolBar. Here's the code to process the on_wm_windowposchanged message: The void CToolBarEx::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos)
{
  CToolBar::OnWindowPosChanged(lpwndpos);
  
  // should only be called once, when floated.
  if( IsFloating() )
  {
    if( m_pDockBar && !m_bMenuRemoved )
    {
      CWnd* pParent = m_pDockBar->GetParent();
      if( pParent->IsKindOf(RUNTIME_CLASS(CMiniFrameWnd)))
      {
        pParent->ModifyStyle( WS_SYSMENU, 0, 0 );
        m_bMenuRemoved = TRUE;
      }
    }
  }
  else if( m_bMenuRemoved ) {
    m_bMenuRemoved = FALSE;
  }
}   
following figure is the final run result chart:

Figure II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.