GDI+ is the graphical library used in the .NET Framework.
Good Resources
http://www.functionx.com/vccli/index.htm
How To Draw Graphics On A Form
Include the namespace:
System::Drawing
Add the following to the forms constructor
bmpDrawingArea = gcnew Bitmap(Width, Height);
graphDrawingArea = Graphics::FromImage(bmpDrawingArea);
Then
Create the forms paint event (select form > select the properties lighting bolt icon > double click event) and add:
e->Graphics->DrawImage(bmpDrawingArea, 0, 0);
Select the form and set is DoubleBuffered property to true, to avoid blinking when its updated
Then in the function that you want to draw all the graphics bits in use this at the beginning:
graphDrawingArea->Clear(this->BackColor);
Then do all your drawing bits and bobs
Then use this to cause the form to be updated
Invalidate(); //Trigger the paint event
Getting a Graphic Object
Every Windows control automatically inherits a method called CreateGraphics(), which gives you access to the graphic part of a control. The CreateGraphics() method returns the Graphics object of the variable you call it from. An example of getting the Graphics object of a form:
System::Void button1_Click(System::Object ^ sender,
System::EventArgs ^ e)
{
Graphics ^ graph = this->CreateGraphics();
Another technique is to call the Graphics::FromHwnd() static method. Its syntax is:
System::Void button1_Click(System::Object ^ sender,
System::EventArgs ^ e)
{
Graphics ^ graph = Graphics::FromHwnd(this->Handle);
Stopping blinking of the form
Select the form and set its DoubleBuffered property to true.
Pen Colour
Specifying by name
BackColor = Color::Turquoise;
Specifying from RGB values
BackColor = Color::FromArgb(26, 69, 174);
Creating a variable to hold colour
Color clrBlue = Color::Blue;
Solid Brush Colour
SolidBrush^ lightColour = gcnew SolidBrush(Color::Lime);
lightColour->Color = Color::FromArgb(5, 10, 20);
Pen
The most basic graphic tool
Pen ^ penRed = gcnew Pen(Color::Blue);
Defining a pen to use to change colour
Pen ^ penColour = gcnew Pen(Color::Blue);
penColour->Color = Color::Red;
Drawing Rectangles
Declare two variables as follows
private:
/// <summary>
/// Required designer variable.
/// </summary>
Graphics^ graphDrawingArea;
Bitmap^ bmpDrawingArea;
In the form load event
bmpDrawingArea = gcnew Bitmap(Width, Height);
graphDrawingArea = Graphics::FromImage(bmpDrawingArea);
Create the paint event for the form and put this in it
e->Graphics->DrawImage(bmpDrawingArea, 0, 0);
Then to draw rectangles use the following
graphDrawingArea->Clear(this->BackColor);
graphDrawingArea->DrawRectangle(gcnew Pen(Color::Red), 20, 20, 40, 60); //colour, x, y, width, height - x,y is top left corner
Invalidate(); //Trigger the paint event
//or use
graphDrawingArea->FillRectangle(gcnew SolidBrush(Color::Lime), 20, 20, 40, 60); //colour, x, y, width, height - x,y is top left corner
Drawing Elipses / Circles
Note that the X and Y coords define the top left corner of a rectangle that encloses the elipse.
graphDrawingArea->DrawEllipse(gcnew Pen(Color::Red), Rectangle(
targets_start_x + (targets_width >> 1) + x_coord,
targets_start_y + y_coord,
target_diameter,
target_diameter
)); //colour, x, y, width, height - x,y is top left corner