MFC Tabbed Documents - how to enable middle-mouse button to close document? - mfc

If you create a new MFC application (with MFC Feature Pack), and using all the defaults, click Finish. It creates an MDI application with the new "Tabbed Documents" style.
I think these are great except it really annoys me that I can't close a Tabbed Document window by middle-clicking on the tab.
This is possible in Firefox, IE, Chrome and more importantly VS2008. But clicking the middle-button on a tab doesn't do anything.
I cannot figure out how to override the tab bar to allow me to handle the ON_WM_MBUTTONDOWN message. Any ideas?
Edit: Guessing I need to subclass the CMFCTabCtrl returned from CMDIFrameWndEx::GetMDITabs...

No subclassing needed (phew). Managed to get it working by hijacking the PreTranslateMessage of the mainframe. If the current message is a middle-mouse-button message, I check the location of the click. If it was on a tab then I close that tab.
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
switch (pMsg->message)
{
case WM_MBUTTONDBLCLK:
case WM_MBUTTONDOWN:
{
//clicked middle button somewhere in the mainframe.
//was it on a tab group of the MDI tab area?
CWnd* pWnd = FromHandle(pMsg->hwnd);
CMFCTabCtrl* tabGroup = dynamic_cast<CMFCTabCtrl*>(pWnd);
if (tabGroup)
{
//clicked middle button on a tab group.
//was it on a tab?
CPoint clickLocation = pMsg->pt;
tabGroup->ScreenToClient(&clickLocation);
int tabIndex = tabGroup->GetTabFromPoint(clickLocation);
if (tabIndex != -1)
{
//clicked middle button on a tab.
//send a WM_CLOSE message to it
CWnd* pTab = tabGroup->GetTabWnd(tabIndex);
if (pTab)
{
pTab->SendMessage(WM_CLOSE, 0, 0);
}
}
}
break;
}
default:
{
break;
}
}
return CMDIFrameWndEx::PreTranslateMessage(pMsg);
}

Related

How to activate a button (CButton) located in a disabled window (CWnd)?

I have this code :
m_pBtnCom = new CButton();
m_pBtnCom->Create(_T("Push"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_TEXT|BS_VCENTER|BS_CENTER, rc, this, BTN_CMT);
Where:
this = my derived CWnd class
rc = CRect button position
BTN_CMT = button id
Current context:
If I disable the parent CWnd by calling EnableWindow(FALSE), even if I call the function EnableWindow(TRUE) on the button (m_pBtnCom->EnableWindow(TRUE)), the latter remains disabled; Therefore, nothing works on it: click, tooltip, ...
I tried to remove WS_CHILD, without success
Question:
Is it possible to activate the button when the window (argument this in my code) is disabled?
Child window can't be independently enabled when parent window is disabled. You can instead enable all children, then go back and enable the particular button.
Note, if you have IDCANCEL button, and you disable it, then the dialog's close button is not functioning either and it gets confusing. You may want to avoid disabling the cancel button and override OnCancel
void CMyDialog::enable_children(bool enable)
{
auto wnd = GetWindow(GW_CHILD);
while (wnd)
{
wnd->EnableWindow(enable);
wnd = wnd->GetWindow(GW_HWNDNEXT);
}
}
BOOL CMyDialog::OnInitDialog()
{
CDialog::OnInitDialog();
enable_children(FALSE);
//re-enable one button
if(GetDlgItem(IDCANCEL)) GetDlgItem(IDCANCEL)->EnableWindow(TRUE);
return TRUE;
}
void OnCancel()
{
MessageBox(L"cancel...");
CDialog::OnCancel();
}

MFC: Create tooltip for a Tab in CTabView

I want to create a ToolTip for a tab on a CTabView which is in one of the CSplitterWnd panes. In this case the tab is a CHtmlView. I put the CToolTipCtrl m_ToolTip in the CMainFrame as public. I create it on OnCreate(), then attempt to add test text to it on OnInitialUpdate() of the CHtmlView tab item. But when hovering over the tab or client area of the tab, no tool tip shows. What am I doing wrong or missing?
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// ...
m_ToolTip.Create(this);
return 0;
}
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
// create spliter windows - one of which will end up with CTabView
}
// One of the tabs in the CTabView is based on CHtmlView
void CMyHtmlView::OnInitialUpdate()
{
__super::OnInitialUpdate();
CToolTipCtrl &tooltip=((CMainFrame*) AfxGetMainWnd())->m_ToolTip;
tooltip.AddTool(this, _T("Test Tooltip"));
tooltip.Activate(TRUE);
}
Abandoning the above, I implemented the suggestion on page 941-942 in the book Programming Windows with MFC second Edition to derive from CToolTipCtrl and implement new functions that use TTF_SUBCLASS. I tried it with in the CMyTabView on the client area of the tabs. It sort of works with the Edit and Rich Views but not HTML view. By sort of, I mean, it works maybe twice then doesn't come up again. Here's that part:
int CMyTabView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CTabView::OnCreate(lpCreateStruct) == -1)
return -1;
AddView(RUNTIME_CLASS(CMyEditView), _T("Tab1"));
AddView(RUNTIME_CLASS(CMyRichView), _T("Tab2"));
AddView(RUNTIME_CLASS(CMyHtmlView), _T("Tab3"));
m_ToolTip.Create(this, TTS_ALWAYSTIP);
CMFCTabCtrl &tabctrl=GetTabControl();
for (int i=0; i < tabctrl.GetTabsNum(); ++i) {
m_ToolTip.AddWindowTool(tabctrl.GetTabWnd(i), _T("Test Tool Tip"));
}
return 0;
}
Not working on the HTML View was fine because I really just want it on the tab. So I change it up and got rid of the stuff above (except the .Create) and added:
void CMyTabView::OnInitialUpdate()
{
CTabView::OnInitialUpdate();
CMFCTabCtrl &tabctrl=GetTabControl();
CRect rc;
tabctrl.GetTabsRect(rc);
if (!rc.IsRectEmpty()) {
m_ToolTip.AddRectTool(&tabctrl, _T("test tip"), &rc, (UINT_PTR) tabctrl.GetSafeHwnd());
}
}
But that doesn't work. It tried using this as well, still didn't work. So while something worked, still can't get it to work on the tabs? Any ideas?

MFC: How do you implement a context menu for CRichEditView in a CTabView tab?

I have a CTabView with one of the tabs a CRichEditView. Rich text is added to the control and shows fine. If I select text within the CRichEditView the toolbar edit items work fine (for example, copy highlights, and if I click on it, it copies to the clipboard). However, I found that if I selected text and right click there is no context menu with a CRichEditView like there was with CEditView. Searching the Internet, I found an implementation for CRichEditView::GetContextMenu() to try and use. It first had an assert failure because the CDocument is not a rich text type, so for testing, I removed it (commented out below) and ended up with the following:
HMENU CMyRichView::GetContextMenu(WORD seltyp, LPOLEOBJECT lpoleobj, CHARRANGE* lpchrg)
{
// TODO: Add your specialized code here and/or call the base class
/*
CRichEditCntrItem* pItem = GetSelectedItem();
if (pItem == NULL || !pItem->IsInPlaceActive())*/
{
CMenu menuText;
menuText.LoadMenu(IDR_CONTEXT_EDIT_MENU);
CMenu* pMenuPopup = menuText.GetSubMenu(0);
menuText.RemoveMenu(0, MF_BYPOSITION);
return pMenuPopup->Detach();
}
}
Where the IDR_CONTEXT_EDIT_MENU is:
IDR_CONTEXT_EDIT_MENU MENU
BEGIN
POPUP "edit"
BEGIN
MENUITEM "&Copy\tCtrl+C", ID_EDIT_COPY
END
END
Now when I right-click I see the context menu. However, when I choose "copy", nothing happens. So I mapped the ID_EDIT_COPY so I could set a break point to see if it was called.
void CMyRichView::OnEditCopy()
{
// TODO: Add your command handler code here
ASSERT_VALID(this);
GetRichEditCtrl().Copy();
}
It's not if the context item is used, but it is if the toolbar is used.
What am I missing and doing wrong?
TIA!!
If the message goes to CTabView then add CTabView::OnEditCopy handler.
Otherwise, you can override PreTranslateMessage as shown below, this will make sure the message is sent to CMyRichEditView::OnEditCopy.
BOOL CMyRichEditView::PreTranslateMessage(MSG *msg)
{
if(msg->message == WM_CONTEXTMENU || msg->message == WM_RBUTTONDOWN)
{
CMenu menu;
menu.LoadMenu(IDR_CONTEXT_EDIT_MENU);
int c = menu.GetMenuItemCount();
CMenu* popup = menu.GetSubMenu(0);
popup->TrackPopupMenu(0, msg->pt.x, msg->pt.y, this, NULL);
return TRUE;
}
return CRichEditView::PreTranslateMessage(msg);
}

Example code for CMFCMenuButton?

Sorry for the newbie question, but can anyone point me at sample code that illustrates the use of the CMFCMenuButton? The Microsoft help refers to "New Controls samples", but these samples seem to be in the Visual Studio 2008 "Feature Pack", and this refuses to install on my system since I'm running VS 2013 and don't have VS 2008. I haven't been able to find the samples as stand-alone code.
To be specific, I have a dialog bar in which I want a button labelled Save with drop-down options of Save All and Save Visible (with Save All the default). But any working code would at least get me started.
Declare data members:
CMFCMenuButton m_button_menu;
CMenu m_menu;
Also add the button's id to message map and data exchange:
BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
ON_BN_CLICKED(IDC_MFCMENUBUTTON1, OnButtonMenu)
...
END_MESSAGE_MAP
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_MFCMENUBUTTON1, m_button_menu);
}
Define:
BOOL CMyDialog::OnInitDialog()
{
CDialogEx::OnInitDialog();
//...
m_menu.LoadMenu(IDR_MENU1);
m_button_menu.m_hMenu = m_menu.GetSubMenu(0)->GetSafeHmenu();
return TRUE;
}
Where IDR_MENU1 is a regular menu bar and we get its first submenu. For example:
IDR_MENU1 MENU
BEGIN
POPUP "Dummy"
BEGIN
MENUITEM "&Item1", ID_FILE_ITEM1
MENUITEM "&Item2", ID_FILE_ITEM2
END
END
If button's drop-down arrow is clicked, a popup menu appears, menu result is passed to OnButtonMenu. If left side of button is clicked, then OnButtonMenu is called directly, without showing a popup menu.
void CMyDialog::OnButtonMenu()
{
CString str;
switch (m_button_menu.m_nMenuResult)
{
case ID_FILE_ITEM1:
str = L"first menu item clicked";
break;
case ID_FILE_ITEM2:
str = L"second menu item clicked";
break;
default:
str = L"Button click (popup menu did not appear, or menu ID is not handled)";
break;
}
MessageBox(str);
}
** When working with docking controls, dialog bars, etc. MFC may run its own subclass, I don't think DoDataExchange gets called. m_button_menu could be invalid. GetDlgItem can be used to find the correct pointer:
CMFCMenuButton* CMyDlgBar::GetButtonMenu()
{
CMFCMenuButton* pButton = &m_button_menu;
if (!IsWindow(pButton->m_hWnd))
pButton = (CMFCMenuButton*)GetDlgItem(IDC_MFCMENUBUTTON1);
return pButton;
}
Everywhere else we use GetButtonMenu() instead of m_button_menu. For example:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
//...
m_dlgbar.Create(...);
m_dlgbar.m_menu.LoadMenu(IDR_MENU1);
m_dlgbar.GetButtonMenu()->m_hMenu = m_dlgbar.m_menu.GetSubMenu(0)->GetSafeHmenu();
return 0;
}
void CMainFrame::OnButtonMenu()
{
CString str;
switch (GetButtonMenu()->m_nMenuResult)
...
}
What if the Drop-Down Arrow does not show?
Then read the answer here that explains the changes needed to your RC file.

How to prevent an application from not displaying multiple cancel messagebox's?

I am having a propertysheet and it has three pages (page1, page2, page3) respectively.For which I added as messagebox whenever Cancel button is pressed or [X] is clicked or Esc is pressed.
Steps followed:
1.Ran the application.
Pressed Cancel button and message box is popped up. (Did not cancel the messagebox).
Now go to the taskbar and right click on the application icon and click "close window". Exactly here the problem arose; i.e, one more message box window is popped up.
Actually this should not happen, right? It should be restricted to only one message box.
//This is being triggered when close window or cancel button is pressed.
BOOL OnQueryCancel()
{
if(IDOK == ::MessageBox(m_hWnd, L"Closing the application",
L"Warning", MB_OKCANCEL | MB_ICONWARNING))
{
return TRUE;
}
return FALSE;
}
How can I prevent from not displaying multiple messagebox's? I should show focus to the already opened messagebox.
First, you should use AfxMessageBox, which makes it easier in MFC. Second, this is normal operation in Windows -- it's just responding to the close messages. I would add a variable to indicate the box is displayed already:
//Part of your class
BOOL m_bIsPromptActive;
BOOL OnQueryCancel()
{
if( !m_bIsPromptActive)
{
m_bIsPromptActive = TRUE;
if(IDOK == ::MessageBox(m_hWnd, L"Closing the application",
L"Warning", MB_OKCANCEL | MB_ICONWARNING))
{
return TRUE;
}
m_bIsPromptActive = FALSE;
}
else
{
// Message is already displayed. Set the focus to this window
::SetFocus( m_hWnd ); // or this->SetFocus();
// You can also look at ::BringWindowToFront()
}
return FALSE;
}