I am trying to change color of a Picture Control box Which I added to a Dialog box in Microsoft Visual C++ environment. I did these Procedure:
- In Resource file-> Dialog editor I added a Picture Control box(Type=Rectangle)
- I had a look at this problem and tried to keep continuing the way.
- Right-Click on Picture Control box and Add a variable.
4.In MainDlg.Cpp file and In Onpainting(); section, the following code Has is added:
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
dc.FillSolidRect(&rect, RGB(120,255,0));
However I added above command to the following command which I think belongs to the Dialog painting(I don't know If it make problem or not):
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();
In main.Cpp file and in function OnInitDialog(); this code added:
m_PictureControlVariable.SubclassDlgItem(IDC_STATIC_PictureControl, this);
(Where IDC_STATIC_PictureControl is Picture Control ID in dialog editor)
I am going to call Picture Contorl color be changed when an Event happens, I mean in a Callback Function which Sends Message to a Dialog I am calling this Picture Control as:
m_PictureControlVariable.EnableWindow();
Now I recieve a debug error!!
Updated:
I added a MFC Class with a CStatic type(In Menu bar-> Project-> Add Class): A code With following .Cpp and .H included to original project:
#include "stdafx.h"
#include "Main.h"
#include "PICTURECTRL.h"
// PICTURECTRL
IMPLEMENT_DYNAMIC(PICTURECTRL, CStatic)
PICTURECTRL::PICTURECTRL()
{
}
PICTURECTRL::~PICTURECTRL()
{
}
BEGIN_MESSAGE_MAP(PICTURECTRL, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
// PICTURECTRL message handlers
void PICTURECTRL::OnPaint()
{
CPaintDC dc(this); // device context for painting
//@TG
//#3073
// TODO: Add your message handler code here
// Do not call CStatic::OnPaint() for painting messages
CRect rect;
GetClientRect(&rect);
dc.FillSolidRect(&rect, RGB(0,0,255));
}
and as you can see I added OnPaint() message handler manually from Resource View-> Class View-> Properties-> Message.
Header of derived Class is:
#pragma once
// PICTURECTRL (Picture Control)
class PICTURECTRL : public CStatic
{
DECLARE_DYNAMIC(PICTURECTRL)
public:
PICTURECTRL();
virtual ~PICTURECTRL();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();
};
And in Original code after Adding a Variable to Picture Control these Commands Added to MainDlg.Cpp MainDlg.H, sequently:
void CMainDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMainDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
DDX_Control(pDX, IDC_PROGRESS1, M_progressBar1);
DDX_Control(pDX, IDC_PictureControl, m_PicCtrl);
}
and in Header file of Original file just CStatic m_PicCtrl;
added to the public part of Main class, I mean:
class CMainDlg : public CDialog
{
public:
protected:
public:
CStatic m_PicCtrl;
}