Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this program that embeds controls programatically to the tool bar. Th problem is that the static control was not trasnparent and so was altering the colour of the tool bar around it. So I wrote my own tool bar class and my own static control class each inheriting the corresponding mfc classes and I handled WM_CtlColor. Rather that that solving the problem, my static control, though now transparent, flickers, disappears and reappears, but mostly stays invisible.
So, I whent on to handle all other kinds of messages in a bid to solve the problem, but the problem persited. Initially I did the handling in the Toolbar class, when that did not work, I started handling in the static class as well. later I handled in both classes all to no avail.

What could be the ptoblem and the solution. MainFrame,my Toolbar, and my static classes are shown below:

C++
#pragma once
#include"MyToolBar.h"
class CMainFrame : public CMDIFrameWndEx
{
	DECLARE_DYNAMIC(CMainFrame)
public:
	CMainFrame();

// Attributes
public:

// Operations
public:

// Overrides
public:
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	virtual BOOL LoadFrame(UINT nIDResource, DWORD dwDefaultStyle = WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, CWnd* pParentWnd = NULL, CCreateContext* pContext = NULL);

// Implementation
public:
	virtual ~CMainFrame();
	afx_msg void OnTest();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:  // control bar embedded members
	CMFCMenuBar       m_wndMenuBar;
	CMyToolBar        m_wndToolBar;
	CMFCStatusBar     m_wndStatusBar;
	CMFCToolBarImages m_UserImages;
private:
		
public:
	MyCommand GetCommand() { return m_eCommand;}
public:
	
	// Generated message map functions
protected:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnWindowManager();
	afx_msg void OnViewCustomize();
	afx_msg LRESULT OnToolbarCreateNew(WPARAM wp, LPARAM lp);
	DECLARE_MESSAGE_MAP()

};


C++
#pragma once
#include "MyStatic.h"

// MyToolBar

class CMyToolBar : public CMFCToolBar
{
	DECLARE_DYNAMIC(CMyToolBar)

public:
	CMyToolBar();
	virtual ~CMyToolBar();
		
public:
	void EmbedControls();
	CMyStatic m_pStatic3;
	CComboBox m_pCombo1,m_pCombo2,m_pCombo3;
	CWnd m_pEdit1;

protected:
	DECLARE_MESSAGE_MAP()
public:
	//afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
	//afx_msg void OnPaint();
	afx_msg void OnSize(UINT nType, int cx, int cy);
};

#include "stdafx.h"
#include "MyDoc.h"
#include "MyToolBar.h"


// MyToolBar

IMPLEMENT_DYNAMIC(CMyToolBar, CMFCToolBar)

CMyToolBar::CMyToolBar()
{
}

CMyToolBar::~CMyToolBar()
{
}


BEGIN_MESSAGE_MAP(CMyToolBar, CMFCToolBar)
	//ON_WM_CTLCOLOR()
	//ON_WM_PAINT()
	ON_WM_SIZE()
END_MESSAGE_MAP()



// MyToolBar message handlers

void CMyToolBar::EmbedControls()
{
	int iy = 1,icy = 19;
	
	int ixCombo1 =  230,icxCombo1 = 100;
	int ixCombo2 = ixCombo1 + icxCombo1 + 5,icxCombo2 = 100;
	int ixCombo3 = ixCombo2 + icxCombo2 + 5,icxCombo3 = 100;
		
	int ixStatic1 = ixCombo3 + icxCombo3 + 5,icxStatic1 = 55;
	int ixEdit1 = ixStatic1 + icxStatic1 + 5,icxEdit1 = 200;

	CRect rcCombo1(ixCombo1,iy,ixCombo1 + icxCombo1,iy + icy);
	m_pCombo1.Create(WS_VISIBLE|WS_CHILD|CBS_DROPDOWNLIST,rcCombo1,(CWnd *)this,0);
	
	CRect rcCombo2(ixCombo2,iy,ixCombo2 + icxCombo2,iy + icy);
	m_pCombo2.Create(WS_VISIBLE|WS_CHILD|CBS_DROPDOWNLIST,rcCombo2,(CWnd *)this,0);

	CRect rcCombo3(ixCombo3,iy,ixCombo3 + icxCombo3,iy + icy);
	m_pCombo3.Create(WS_VISIBLE|WS_CHILD|CBS_DROPDOWNLIST,rcCombo3,(CWnd *)this,0);
	
	CRect rcStatic1(ixStatic1,iy,ixStatic1 + icxStatic1|SS_CENTER,iy + icy);
	m_pStatic3.CreateEx(WS_EX_TRANSPARENT,_T("STATIC"),_T("Formula"),WS_VISIBLE|WS_CHILD,rcStatic1,(CWnd *)this,0);
	
	CRect rcEdit1(ixEdit1,iy,ixEdit1 + icxEdit1,iy + icy);
	m_pEdit1.CreateEx(WS_EX_CLIENTEDGE,_T("EDIT"),_T(""),WS_VISIBLE|WS_CHILD,rcEdit1,(CWnd *)this,0);
	
}


/*HBRUSH CMyToolBar::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CMFCToolBar::OnCtlColor(pDC, pWnd, nCtlColor);
	if(pWnd->GetExStyle() & WS_EX_TRANSPARENT)
	{
		pDC->SetBkMode(TRANSPARENT);
		hbr = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
	}
	
	return hbr;
}*/


/*void CMyToolBar::OnPaint()
{
	CMFCToolBar::OnPaint();
	m_pStatic3.SendMessage(WM_PAINT);

}*/


void CMyToolBar::OnSize(UINT nType, int cx, int cy)
{
	CMFCToolBar::OnSize(nType, cx, cy);
   if (::IsWindow(m_pStatic3.GetSafeHwnd()))
   {
	   //m_pStatic3.Invalidate();
   }
	
}


C++
#pragma once


// CMyStatic

class CMyStatic : public CWnd
{
	DECLARE_DYNAMIC(CMyStatic)

public:
	CMyStatic();
	virtual ~CMyStatic();

protected:
	DECLARE_MESSAGE_MAP()
public:
HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); 

afx_msg void OnSize(UINT nType, int cx, int cy);
};


#include "stdafx.h"
#include "MyDoc.h"
#include "MyStatic.h"


// CMyStatic

IMPLEMENT_DYNAMIC(CMyStatic, CWnd)

CMyStatic::CMyStatic()
{

}

CMyStatic::~CMyStatic()
{
}


BEGIN_MESSAGE_MAP(CMyStatic, CWnd)
//	ON_WM_CTLCOLOR()
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_SIZE()
END_MESSAGE_MAP()



// CMyStatic message handlers


HBRUSH CMyStatic::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	pDC->SetBkMode(TRANSPARENT);
	pDC->SetTextColor(RGB( 0, 0, 0 ));
	return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
}





void CMyStatic::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);
	//Invalidate();
	// TODO: Add your message handler code here
}
Posted
Comments
[no name] 1-Oct-14 13:47pm    
Check for the WS_CLIPSIBLINGS window style in the frame window class and WS_CLIPCHILDREN window style in the static control.

I can usually avoid flicker problems by returning zero on WM_ERASEBKGND and let the WM_PAINT code paint both foreground and background - through an intermediate MEMDC if necessary. This isn't the same as transparency but you can mimic the effects.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900