How to correctly display static text on an MFC CToolBar using TBSTYLE_FLAT?

I am using VS2005 and MFC. I added some code to add static text to the toolbar. However, with the style set to TBSTYLE_FLAT or (TBSTYLE_FLAT | TBSTYLE_TRANSPARENT), the delimiter becomes visible and looks like a small mark above the text. Is there a better way to show static text on a CToolBar or make the separator invisible if behind the text? Thanks!

screenshot

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT | TBSTYLE_TRANSPARENT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } //Create Static Text CRect rect; int nIndex = m_wndToolBar.GetToolBarCtrl().CommandToIndex(ID_STATIC_TEST); m_wndToolBar.SetButtonInfo(nIndex, ID_STATIC_TEST, TBBS_SEPARATOR, 40); m_wndToolBar.GetToolBarCtrl().GetItemRect(nIndex, &rect); rect.top = 5; rect.right = rect.left + 50; if(!m_static.Create("Test", WS_CHILD | WS_VISIBLE | SS_CENTER, rect, &m_wndToolBar)) { TRACE(_T("Failed to create Static Text\n")); return FALSE; } // TODO: Delete these three lines if you don't want the toolbar to be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); return 0; } 
+4
source share
1 answer

It may be too late for you, but the TabControl example that ships with MFC shows you how to do this. Basically, you use the CToolbarLabel class (which is implemented in the example for you) in the same way as you use CMFCToolBarButton and your spouses:

 m_wndToolBar.ReplaceButton(ID_LABEL, CToolbarLabel(ID_LABEL, L"Some label: ")); 

For completeness, here is the CToolbarLabel implementation:

Title:

 #pragma once class CToolbarLabel : public CMFCToolBarButton { DECLARE_SERIAL(CToolbarLabel) public: CToolbarLabel (UINT nID = 0, LPCTSTR lpszText = NULL); virtual void OnDraw (CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE, BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE); }; 

Implementation File:

 #include "stdafx.h" #include "ToolbarLabel.h" IMPLEMENT_SERIAL(CToolbarLabel, CMFCToolBarButton, 1) CToolbarLabel::CToolbarLabel (UINT nID, LPCTSTR lpszText) { if (lpszText != NULL) { m_strText = lpszText; } m_bText = TRUE; m_nID = nID; m_iImage = -1; } void CToolbarLabel::OnDraw (CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz, BOOL /*bCustomizeMode*/, BOOL /*bHighlight*/, BOOL /*bDrawBorder*/, BOOL /*bGrayDisabledButtons*/) { UINT nStyle = m_nStyle; m_nStyle &= ~TBBS_DISABLED; CMFCToolBarButton::OnDraw (pDC, rect, pImages, bHorz, FALSE, FALSE, FALSE, FALSE); m_nStyle = nStyle; } 

As you can see, it is very lightweight and just uses what the button already provides, so it also works for visual themes.

+1
source

Source: https://habr.com/ru/post/1392109/


All Articles