如何更改MFC的颜色 [英] How to Change the color of an MFC
本文介绍了如何更改MFC的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个MFC应用程序,我需要将MFC的背景更改为其他颜色。另外我在MFC中有一些按钮我需要为每个按钮提供不同的颜色。任何人都可以帮助我。谢谢提前
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
推荐答案
参考本节,你可以得到你的问题的解决方案。
< a href =http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/88cfec47-8b09-483c-8a03-a9c33859d2bb> http://social.msdn.microsoft.com/Forums / en-US / vcgeneral / thread / 88cfec47-8b09-483c-8a03-a9c33859d2bb [ ^ ]
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[^]
CPP文件
* **********
CPP FILE
***********
// 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文件
*********
*************************
*.h file
*********
// 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;
};
这是解决方案
afx_msg BOOL OnEraseBkgnd(CDC *);
afx_msg HBRUSH OnCtlColor(CDC *,CWnd *,UINT);
这两项功能将改变背景
谢谢
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
如何使用基础类更改窗口背景颜色 [ ^ ]
CColorBox [ ^ ]
这篇关于如何更改MFC的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文