In the visual
In the C ++ automatically generated MFC framework application, when the floating toolbar is dragged out of the main frame window, a "close" button is usually displayed in the upper right corner of the toolbar.
The User Interface (UI) of Windows applications is designed according to the design specifications. That is to say, all windows should have a place for users to close the window. But some developers have come up
You want this "close" button. In this example, you can customize an extended toolbar class to delete the "close" button in the toolbar. After the program is compiled and run, the interface effect 1 is shown as follows:
Figure 1 floating toolbar without the "close" button
I. Implementation Method
First, let's determine the implementation idea for the "close" button on the deleted toolbar, because the function we want to implement is related to the toolbar, so the classes involved must be
Ctoolbar. Second, to determine the status of the "close" button after the toolbar is floating, we need a member variable indicating the status. Third, to realize the floating feature of the toolbar, we need to set the toolbar window
The on_wm_windowposchanged message of is processed. This message is responsible for changing the size, position, or zcoordinate of the ctoolbar window. Why not use it here?
Wm_size/onsize to change the window size? This is called only when the size of the involved window is changed, and the position of the involved window is also changed. Therefore, when the floating toolbar is not always called
Wm_size/onsize. We can use
The m_pdockbar member variable of the base class ccontrolbar calls getparent () to obtain the parent window of the toolbar. To change the style of the parent window-shield the system menu,
To achieve our goal -- remove the "close" button.
With the idea, let's take a look at the specific implementation code. We will first derive a new class from ctoolbar
Ctoolbarex, because our purpose is to remove the "close" button when floating the toolbar, we can only determine whether the toolbar is floating. This uses the attributes of the ccontrolbar class.
The judgment function can be: bool isfloating () Const. Add a member variable to the new class to indicate the status of the "close" button: bool
M_bmenuremoved. When we remove the system menu attribute of the main framework, its value is true. Then, we use
M_pdockbar, which is used to determine whether the parent window type is cminiframewnd. This class represents the frame window around the floating toolbar. After the above processing, we can rest assured that
Remove the system menu from the ctoolbar. The following code processes 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;
}
}
Ii. programming steps
1. Start visual c ++ 6.0, generate an application with a single-document view structure, and name the application "flttbclsbtn ";
2. Use Class Wizard to add a new class ctoolbarex to the project, and select ctoolbar as the base class;
3. In the cmainfrme class, change the member variable m_wndtoolbar type to ctoolbarex;
4. Add code and compile and run the program.
3. program code
/////////////////////////////////////////////////////////////////////////////
// ToolBarEx.h : header file
#ifndef __TOOLBAREX_H__
#define __TOOLBAREX_H__
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
class CToolBarEx : public CToolBar
{
DECLARE_DYNAMIC(CToolBarEx)
// Construction
public:
CToolBarEx();
// Attributes
protected:
BOOL m_bMenuRemoved;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CToolBarEx)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CToolBarEx();
// Generated message map functions
protected:
//{{AFX_MSG(CToolBarEx)
afx_msg void OnWindowPosChanged(WINDOWPOS FAR* lpwndpos);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif // __TOOLBAREX_H__
//////////////////////////////////////////////////////////////////////// ToolBarEx.cpp : implementation file
#include "StdAfx.h"
#include "ToolBarEx.h"
#include
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CToolBarEx::CToolBarEx()
{
// TODO: add construction code here.
m_bMenuRemoved = FALSE;
}
CToolBarEx::~CToolBarEx()
{
// TODO: add destruction code here.
}
IMPLEMENT_DYNAMIC(CToolBarEx, CToolBar)
BEGIN_MESSAGE_MAP(CToolBarEx, CToolBar)
//{{AFX_MSG_MAP(CToolBarEx)
ON_WM_WINDOWPOSCHANGED()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CToolBarEx message handlers
void CToolBarEx::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos)
{
CToolBar::OnWindowPosChanged(lpwndpos);
// Shocould 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;
}
}
Iv. Summary
In this example, a custom class is used to process window location change messages in windows, and the "close" button on the floating toolbar is deleted, the main task is to determine whether the parent window of the current window is the framework window class of the floating toolbar-cminiframewnd class.