Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have an MFC application, i need to change the background of the MFC to other color.Also I have some button inside the MFC i need to give different color for each button.can anyone pls help me in this .Thanks in advance
Posted
Comments
Mohibur Rashid 20-May-13 2:23am    
try this,
http://www.codeproject.com/Articles/7876/CColorBox

Refer this section, You may get the solution for your problem.
http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/88cfec47-8b09-483c-8a03-a9c33859d2bb[^]
 
Share this answer
 
CPP FILE
***********
C++
// Bitmap1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Bitmap1.h"
#include "Bitmap1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CBitmap1Dlg dialog



CBitmap1Dlg::CBitmap1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBitmap1Dlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBitmap1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_PICTURE, m_picture);
}

BEGIN_MESSAGE_MAP(CBitmap1Dlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_ERASEBKGND()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CBitmap1Dlg message handlers

BOOL CBitmap1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	//CBitmap Bit1;
//	HBITMAP hBitmap;
	m_picture.Create(NULL, WS_CHILD|WS_VISIBLE|BS_BITMAP,CRect(10,10,400,50), this, IDC_PICTURE);
	//BOOL bRet = Bit1.LoadBitmapW(MAKEINTRESOURCE(IDB_BITMAP1));
	hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));
	m_picture.SetBitmap(hBitmap);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBitmap1Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<wparam>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
		hBitmap = MakeBitMapTransparent(hBitmap);
		m_picture.SetBitmap(hBitmap);
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBitmap1Dlg::OnQueryDragIcon()
{
	return static_cast<hcursor>(m_hIcon);
}

BOOL CBitmap1Dlg::OnEraseBkgnd(CDC* pDC)
{
	CRect r;
	////GetClientRect gets the width & height of the client area of the Dialog
	GetClientRect(&r);
	CBrush br(RGB(0,100,0));
	pDC->SelectObject(br);
	pDC->FillRect(r,&br);

	//Make sure to return TRUE;
 //return CDialogEx::OnEraseBkgnd(pDC);
	return TRUE;
}
HBRUSH CBitmap1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
//	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 //return hbr;

 //Make sure to return the Brush color should be same as Dialog Background color
        CBrush br(RGB(0,255,0));
	return (HBRUSH)br;
}</hcursor></wparam>

*************************
*.h file
*********
C++
// Bitmap1Dlg.h : header file
//

#pragma once
#include "afxwin.h"
#include "afxext.h"


// CBitmap1Dlg dialog
class CBitmap1Dlg : public CDialog
{
// Construction
public:
	CBitmap1Dlg(CWnd* pParent = NULL);	// standard constructor
	HBITMAP hBitmap;

// Dialog Data
	enum { IDD = IDD_BITMAP1_DIALOG };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


// Implementation
protected:
	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg BOOL OnEraseBkgnd(CDC*);
	afx_msg HBRUSH OnCtlColor(CDC* , CWnd* , UINT );
	DECLARE_MESSAGE_MAP()
public:
	CBitmapButton m_picture;
};



This is the solution
The afx_msg BOOL OnEraseBkgnd(CDC*);
afx_msg HBRUSH OnCtlColor(CDC* , CWnd* , UINT );

These two function will change the background

Thanks
 
Share this answer
 
v3

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