
圖一、沒有"關(guān)閉"按鈕的浮動工具條
一、實現(xiàn)方法
首先,針對刪除工具條上"關(guān)閉"按鈕的問題我們來確定一下實現(xiàn)的思路,因為我們要實現(xiàn)的功能與工具條有關(guān),所以涉及的類肯定與 CToolBar有關(guān)。其次,為了判斷工具條浮動后"關(guān)閉"按鈕的狀態(tài),我們需要一個表示狀態(tài)的成員變量;第三,實現(xiàn)工具條的浮動特性,需要對工具條窗口的ON_WM_WINDOWPOSCHANGED消息進行處理。這個消息負責(zé)CToolBar窗口大小、位置或Z坐標(biāo)的變化,這里為什么不用WM_SIZE/OnSize來處理窗口大小的改變呢?因為僅當(dāng)涉及窗口大小改變而不涉及窗口位置也改變時才這樣調(diào)用,所以當(dāng)浮動工具條時不一定每次都調(diào)用處理WM_SIZE/OnSize。我們可以利用 基類CControlBar的m_pDockBar成員變量來調(diào)用GetParent()以獲取工具條的父窗口。從而改變父窗口的式樣--屏蔽系統(tǒng)菜單,來達到我們的目的--去掉"關(guān)閉"按鈕。
有了思路,下面來看看具體的實現(xiàn)代碼,我們先從CToolBar派生一個新類CToolBarEx,因為我們的目的是在浮動工具條時去掉"關(guān)閉"按鈕,所以僅僅判斷工具條是否浮動就可以了,這一點用CControlBar類的屬性判斷函數(shù)可以做到:BOOL IsFloating() const。在新類中添加一個成員變量,用它表示"關(guān)閉"按鈕的狀態(tài):BOOL m_bMenuRemoved,當(dāng)我們?nèi)サ糁骺蚣艿南到y(tǒng)菜單屬性時,它的值為TRUE。接著,我們用到了 m_pDockBar,用它來判斷父窗口類型是否是CMiniFrameWnd,該類代表了浮動工具條周圍的框架窗口。經(jīng)過上述處理后,我們就可以放心地從CToolBar中去掉系統(tǒng)菜單了。下面是處理ON_WM_WINDOWPOSCHANGED消息的代碼:
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;
}
}
二、編程步驟
1、 啟動Visual C++6.0,生成一個單文檔視圖結(jié)構(gòu)的應(yīng)用程序,將程序命名為"FltTBClsBtn";
2、 使用Class Wizard在項目中添加一個新類CtoolBarEx,其基類選擇為CToolBar;
3、 在CMainFrme類中將成員變量m_wndToolBar的類型修改為CtoolBarEx;
4、 添加代碼,編譯運行程序。
三、程序代碼/////////////////////////////////////////////////////////////////////////////
// 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);
// 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;
}
}
四、小結(jié)
本實例通過一個自定義類,處理Windows中窗口位置變化消息,實現(xiàn)了刪除浮動工具條上"關(guān)閉"按鈕的目的,在上述過程中,最主要的工作還是判斷當(dāng)前窗口的父窗口是否為浮動工具條的框架窗口類-CMiniFrameWnd類。