VC + + Remove the "close" button in the floating bar

Source: Internet
Author: User
Tags bool

In the MFC framework application that is automatically generated by Visual C + +, when the floating bar is dragged off the main frame window, there is usually a "close" button in the upper-right corner of the toolbar, which should be said to be designed exactly according to the user interface (UI) design specification of the Windows application. That means that all windows should have a place where users can close the window. But some developers do not want this "close" button. This example implements the purpose of removing the "close" button in the toolbar by customizing an extended tool bar class. The interface effect after compiling the program is as shown in Figure one:

Figure I, floating bar with no "close" button

One, the realization method

First of all, for the "Close" button on the removal of the bar on the problem we determine the implementation of the idea, because we want to implement the function and 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 variable of the base class CControlBar 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 goal-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 using the attribute-judging function of the CControlBar class: BOOL isfloating () const. Adds a member variable to the new class that represents the status of the Close button: BOOL m_bmenuremoved, when we remove the System menu property of the main frame, its value is true. We then used the m_pDockBar to determine whether the parent window type is CMiniFrameWnd, which represents the frame window around the floating toolbar. After the above treatment, we can safely remove the system menu from the CToolBar. Here is the code to process the on_wm_windowposchanged message:

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;
 }
}

Second, programming steps

1, start visual c++6.0, generate a single document view structure of the application, the program named "Flttbclsbtn";

2, using Class Wizard in the project to add a new class Ctoolbarex, the base class selection for CToolBar;

3. Modify the type of member variable m_wndToolBar to Ctoolbarex in Cmainfrme class;

4, add code, compile and run the program.

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.