how to draw a rectangle in visual C++ 6.0

Sep 24, 2009 at 5:56am
Dear all,

I have a problem in Visual C++ 6.0.
I made a project using Dialog Based, not Single Document Type.
I want to make a rectangle that its color able to change when I clicked a button.

For draw the rectangle, I used this code.

dc.Rectangle(10,10,208,92);
CRect RectBackground(10,10,208,92);

CBrush brush;
brush.CreateSolidBrush(RGB(red,green,blue));

dc.FillRect(RectBackground,&brush);


I entered those code to class::OnPaint(), so OnPaint function become:

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

SendMessage(WM_ICONERASEBKGND, (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
{
CPaintDC dc(this); // device context for painting
[color = blue]
// Background Rectangle
dc.Rectangle(10,10,208,92);
CRect RectBackground(10,10,208,92);

CBrush BrushSilver;
BrushSilver.CreateSolidBrush(RGB(red,green,blue));

dc.FillRect(RectBackground,&BrushSilver);
[\color]
CDialog::OnPaint();
}
}


It produce a black rectangle when I added
red = green = blue = 0;

in class::OnInitDialog() fuction.

I think, when I add a button n change "red", "green", and "blue" values, the color of rectangle will change. But it doesn't work.
Code in my button function are:

void CGambarDlg::OnButton1()
{
red = green = blue = 255;
OnPaint();
}


Please help me to solve this problem...

Thank you before..

Eka Candra
Topic archived. No new replies allowed.