GDI+ question

If this is the wrong forum for a GDI+ question, please move it where it should be, thanks!

I am leaning to use GDI+ in a simple C++/CLI windows application, and I can draw lines and boxes to my hearts content in a window, but as soon as I move the window off the sceen/minimize it, etc, the picture that I have drawn disappears. I have a feeling there is a simple reason for this involving binding the image appropriately to the window, but I can't figure it out. All I do is make a button that runs the code below when it is clicked:

Graphics^ FormGraphic;
FormGraphic=this->CreateGraphics();
for (int i=100;i<=280;i+=20)
{
for (int j=100; j<=280;j+=20)
{
if (((i/20)-4+(j/20)-4)%2==0)
DrawingBrush = gcnew SolidBrush(Color::Red);
else
DrawingBrush = gcnew SolidBrush(Color::Black);
FormGraphic->FillRectangle(DrawingBrush,i,j,20,20);
}

BTW, any links to good tutorials on GDI+ would be appreciated!

Thanks,

Ryan

[1019 byte] By [MrT25] at [2007-12-23]
# 1
Yes, it's OK. You have to implement the Paint event (like GDI) to redraw your graphics when the system need (when windows exit out of screen and then reenter, for example)...

A good link to start is the MSDN:

http://windowssdk.msdn.microsoft.com/en-us/library/ms533802.aspx

Cheers,

- AGPX

AGPX at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 2

Yeah, I did see that, but I am trying the C++/CLI language that comes with the express edition, and I don't know where the equivalent code for that is.

MrT25 at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 3

In a CLR window form, I don't know where the paint is implemented, and I don't know where the winmain is either, so it is a little confusing.

MrT25 at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 4

The event could be automatically generated by IDE. Basically you have to double click on Form1.h (supposing that your window form is called Form1) in order to show your window form. Then in the properties window click on the lightning icon (Events) and select 'Paint' in the list that appears. Now, double click on it and IDE automatically generate the code for the event.

It's not so hard to convert code from C++ to C++/CLR, so that link could be quite useful.

Regards,

- AGPX

AGPX at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...
# 5

Thanks guys, I just figured that out a moment ago once I knew that the Paint event was my issue: here is what I did, which is exactly what you said:

In the windows form design, I went to the properties for the control, and set the paint event to call an arbirary function name, Res, which autogenerated the code:

private: System::Void Res(System::Object^ sender, system::Windows::Forms::PaintEventArgs^ e)
{

}

Then I could just add in the drawing code here, so everytime something needs repainting, it calls this function.

By the way, I ran into this issue because I was trying to learn from some of the examples from "Visual C++ 2005 Express Edition Programming for the Absolute Beginner" because it seemed like a decent place to learn CLR Windows Forms. Apparantly though, the author took a lot of shortcuts and doesn't include the information needed to make robust forms, or even code suitable for a beginner, in my opinion.

MrT25 at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: Graphics...