Click here to Skip to main content
15,884,986 members
Articles / Desktop Programming / MFC
Tip/Trick

How to Hide <close> Menu in CDockablePane

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
13 Apr 2018CPOL1 min read 13K   691   6   5
A method of how to get rid of "close" menu from multiple CDockablePane panels

Image 1

Introduction

This trick will illustrate a method to get rid of close menu from CDockablePane. If if you have just one CDockablePane in your application, it is just enough to override CanBeClosed() method inside your pane, but if you have more panels, when you attach them one in another, then "close" menu button shows up again.

Background

Once, I had the same issue to solve, and guided by others, I had solved ... and because I spot several questions regarding this, I decided to write a little tip for those who have the same task.

Using the Code

You can use the CNonClosableDockablePane class inside of your project, and derive your own CDockablePane from this class ... this class will do everything it needs in order to hide the "close" menu.

Here, I list the source code of this CNonClosableDockablePane class:

C++
// NonClosableDockablePane
#define AFX_NON_CLOSE_DOCKING_PANE_STYLE AFX_CBRS_FLOAT | AFX_CBRS_RESIZE | AFX_CBRS_AUTOHIDE

// CNonClosableDockablePane
class CNonClosableDockablePane : public CDockablePane
{
 DECLARE_DYNAMIC(CNonClosableDockablePane)

public:
 CNonClosableDockablePane();
 virtual ~CNonClosableDockablePane();
 virtual CTabbedPane* CreateTabbedPane();

protected:
 virtual BOOL CanAdjustLayout() const {return ! m_bIsSliding || ! m_bIsHiding;}
 virtual BOOL CanBeClosed() const {return FALSE;}

// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(CNonClosableDockablePane)
 public:
 virtual BOOL PreTranslateMessage(MSG* pMsg);
 //}}AFX_VIRTUAL

protected:
 //{{AFX_MSG(CNonClosableDockablePane)
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

The implementation of this class can be found inside the sample project.

Then, all you need to do is to derive your CDockablePane from this class.

C++
#include "NonClosableDockablePane.h"
class CMyPanelWnd : public CNonClosableDockablePane
{
   ...
}

And when you create your panels, you setup AFX_NON_CLOSE_DOCKING_PANE_STYLE style for CDockablePane tabbed bar. Suppose you have 3 panels in your application ... then here is a little sample of how to handle them to get rid of "close" menu button:

C++
AddPane(&m_wndPane1);
AddPane(&m_wndPane2);
AddPane(&m_wndPane3);
m_wndPane1.EnableDocking(CBRS_ALIGN_ANY);
m_wndPane2.EnableDocking(CBRS_ALIGN_ANY);
m_wndPane3.EnableDocking(CBRS_ALIGN_ANY);
DockPane(&m_wndPane1);
CDockablePane* pTabbedBar = NULL;
m_wndPane2.AttachToTabWnd(&m_wndPane1, DM_SHOW, TRUE, &pTabbedBar);
m_wndPane3.AttachToTabWnd(&m_wndPane1, DM_SHOW, TRUE, &pTabbedBar);
EnableAutoHidePanes(CBRS_ALIGN_LEFT | CBRS_ALIGN_RIGHT);

pTabbedBar = (CDockablePane*)m_wndPane1.GetParentTabbedPane();
if(NULL != pTabbedBar->GetSafeHwnd()) // remove AFX_CBRS_CLOSE flag
 pTabbedBar->SetControlBarStyle(AFX_NON_CLOSE_DOCKING_PANE_STYLE);
pTabbedBar = (CDockablePane*)m_wndPane2.GetParentTabbedPane();
if(NULL != pTabbedBar->GetSafeHwnd()) // remove AFX_CBRS_CLOSE flag
 pTabbedBar->SetControlBarStyle(AFX_NON_CLOSE_DOCKING_PANE_STYLE);
pTabbedBar = (CDockablePane*)m_wndPane3.GetParentTabbedPane();
if(NULL != pTabbedBar->GetSafeHwnd()) // remove AFX_CBRS_CLOSE flag
 pTabbedBar->SetControlBarStyle(AFX_NON_CLOSE_DOCKING_PANE_STYLE);

In order to give a real sample of this method, I have attached a little sample project. Enjoy it!

Points of Interest

This solution has been tested on VS2008 and VS2010, but I am pretty sure that will work on newer Visual Studio versions ... if not, just give me a sign.

Update: I have uploaded a solution that will solve this issue when CDockablePane is part of CMainFrame.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCMFCOutlookBar Pin
jmw_ica20-Jun-20 15:26
jmw_ica20-Jun-20 15:26 
QuestionClose button shows up when reopening the application. Pin
steffi12313-Apr-18 1:46
steffi12313-Apr-18 1:46 
AnswerRe: Close button shows up when reopening the application. Pin
_Flaviu15-Apr-18 20:13
_Flaviu15-Apr-18 20:13 
AnswerRe: Close button shows up when reopening the application. Pin
_Flaviu16-Apr-18 1:33
_Flaviu16-Apr-18 1:33 
GeneralRe: Close button shows up when reopening the application. Pin
steffi12317-Apr-18 0:26
steffi12317-Apr-18 0:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.