[MFC] Cum desenez corect gripper-ul toolbar-ului ?

Despre MFC, ATL si alte biblioteci C++ de la Microsoft (forum moderat)
Post Reply
mesajflaviu
Membru++
Membru++
Posts: 684
Joined: 10 Sep 2008, 21:40
Judet: Cluj

[MFC] Cum desenez corect gripper-ul toolbar-ului ?

Post by mesajflaviu » 13 Mar 2013, 15:34

Problema: Gripper-ul toolbar-ului in aplicatii SDI/MDI nu se deseneaza corect cand aplicatia are setat un alt stil vizual decat cel default.

Rezolvare: Se deriveaza o clasa din CToolBar, sa zicem CToolBarExt. Apoi se suprascriu metodele urmatoare:

Code: Select all

// ToolBarExt.h : header file
class CToolBarExt : public CToolBar
{
// Construction
public:
	CToolBarExt();

// Operations
public:
	void DrawGripper(CDC* pDC, const CRect& rect);

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CToolBarExt)
	virtual void DoPaint(CDC* pDC);
	virtual CSize CalcDynamicLayout(int nLength, DWORD dwMode);
	//}}AFX_VIRTUAL

protected:
	//{{AFX_MSG(CToolBarExt)
	afx_msg void OnNcPaint();
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};

Code: Select all

// ToolBarExt.cpp : implementation file
BEGIN_MESSAGE_MAP(CToolBarExt, CToolBar)
	//{{AFX_MSG_MAP(CToolBarExt)
	ON_WM_NCPAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CToolBarExt::DoPaint(CDC* pDC)
{
	ASSERT_VALID(this);
	ASSERT_VALID(pDC);

	// Paint inside the client area.
	CRect rect;
	GetClientRect(rect);
	DrawBorders(pDC, rect);
	DrawGripper(pDC, rect);
}

void CToolBarExt::OnNcPaint() 
{
	// Get window DC that is clipped to the non-client area.
	CWindowDC dc(this);
	CRect rectClient;
	GetClientRect(rectClient);
	CRect rectWindow;
	GetWindowRect(rectWindow);
	ScreenToClient(rectWindow);
	rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
	dc.ExcludeClipRect(rectClient);

	// Draw the borders in the non-client area.
	rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);
	DrawBorders(&dc, rectWindow);

	// Erase the parts that are not drawn.
	dc.IntersectClipRect(rectWindow);
	SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);

	// Draw the gripper in the non-client area.
	DrawGripper(&dc, rectWindow);
}

void CToolBarExt::DrawGripper(CDC* pDC, const CRect& rect)
{
	pDC->FillSolidRect(&rect, ::GetSysColor(COLOR_BTNFACE)); // Fill in the background.

	CToolBar::DrawGripper(pDC,rect);
}
Acest control derivat se foloseste in locul celui default din CMainFrame:

Code: Select all

...
...
//	CToolBar	m_wndToolBar;
	CToolBarExt	m_wndToolBar;
Referinte: The gripper bar is drawn incorrectly when an MFC application uses Windows XP Visual Styles

Atasez aici si o mica aplicatie de test .
Attachments
TestToolbar.zip
(86.98 KiB) Downloaded 482 times



Post Reply