Click to See Complete Forum and Search --> : help with re-paint
rowgram
December 12th, 2005, 06:07 PM
I need to create a filled rectangle with coordinates that loop :
for (j=0; j<max; j++)
{
Graphics* gr2 = CreateGraphics();
gr2->FilledRectangle(...j...);
}
How can I make it re-paint ?
I tried putting same code above into Form_Paint event handler, without the for loop, but the filled rectangles still 'tear' when a window is placed in front of it.
How can I repaint this object that changes its shape with variable j ?
Also I'm confused on the difference between
Graphics * gr2 = e->Graphics; & Graphics* gr = CreateGraphics();
Can someone clarify this ?
ak
rowgram
December 12th, 2005, 06:48 PM
I think the right thing to do is to use the PaintEventArgs :
Graphics * gr = e->Graphics;
// I draw a bitmap using gr->DrawImage
but gr->FillRectangle (...j...) doesn't seem to repaint, even though the bitmap does. Why ?
Marc G
December 13th, 2005, 07:08 AM
What exactly do you mean with "doesn't repaint"?
What do you want to achieve?
Can you post your entire paint handler?
rowgram
December 13th, 2005, 09:37 AM
Here is what I have :
private: System::Void Form2_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e)
{
Graphics * gr = e->Graphics;
if (bitmap != NULL)
{
gr->DrawImage(bitmap, 20, 10, 500, 500);
}
SolidBrush* sb = new SolidBrush(Color::Red);
gr->FillRectangle(sb, 300 + data[j] *10, 300 - data[j] *10, 2, 2);
}
private: System::Void button2_Click(System::Object * sender, System::EventArgs * e)
{
...
for (j=0;j<dwell_samples; j++)
{
Graphics* gr = CreateGraphics();
SolidBrush* sb = new SolidBrush(Color::Red);
gr->FillRectangle(sb, 300 + data[j] *10, 300 - data[j] *10, 2, 2);
}
...
}
The Bitmap refreshs (after minimizing, maximizing, moving other windows in front of the form), but the red rectangles do not refresh ?
Marc G
December 13th, 2005, 10:04 AM
All drawing code should happen in your paint handler. That's the way Windows work. When a part of you window need to be redrawn because you restored it or moved another window in front of it, Windows will send a WM_PAINT message to your window. In your case this WM_PAINT is handled byb your paint handler. That's why all the drawing code needs to be in that function.
rowgram
December 13th, 2005, 11:20 AM
OK - I've tried this & now no red rectangles appear (i.e. I have no image to refresh !) ... this is because the rectangles involve the var j, which every time is incremented, should cause a red rectangle to be painted. By putting all the code into the Paint Handler, the only time the red rectangles will be created is for WM_Paint messages !
How can I trigger the rectangles to paint inside
Also, I think CreateGraphics() method is meant for creating graphics objects outside of the Paint Event Handler - is this not correct ?
Marc G
December 14th, 2005, 03:06 AM
If you want to trigger your WM_PAINT handler you need to invalidate your window. You can use the Invalidate (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclassinvalidatetopic.asp) method for that.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.