Drawing Tutorial
for Visual C++ 2005
EE
5340/7340: Biomedical Instrumentation
Carlos
E. Davila
Electrical
Engineering Dept, SMU
There are a number of
tutorials for using Visual C++ 2005 on the Microsoft web page. Unfortunately, there
do not appear to be any complete tutorials that walk you through a drawing task
step by step from beginning to end. The following tutorial does that.
- Start up Visual C++ 2005 and from the
“File” menu, select “New” and then click on “Project”. You will see the
following window:

- Under “Project types” click on “CLR” and
then under “Visual studio installed templates” select “Windows Forms
Application”.
- Enter a project name and click “OK”.
- At this point you will see the Visual C++
IDE window, consisting of a “Solution Explorer” frame with some
pre-existing files already in place. You will also see “Form1.h[Design]”
window which contains a rudimentary form titled “Form1”.
- Right-click anywhere on “Form1” and select
the “Properties” item in the context menu. On the right side of the IDE,
you will see a table of Form properties.
- Click on the “Events” icon on top of the
“Form1” properties window (the small thunderbolt). This will list a series
of events that you can write event handlers for.

- In the box immediately to the right of
“Paint” write down the name of the Paint event handler, call it something
like “On_paint” and press “Enter”. This will
generate the skeleton for the event handler in a new tab titled “Form1.h”.
This is the header file that contains all of the class declarations for
the Form. The skeleton for the “On_draw” paint
handler looks like this:
private: System::Void
On_paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^
e) {
}
the variable “e” is a handle to the Graphics object associated
with the form. The Graphics class enables you do draw on Forms.
- At this point, you can proceed with any
one of the drawing examples given in the Microsoft web page. For example
if you go to http://msdn2.microsoft.com/en-us/library/x1d5a9f2.aspx,
you will see an example of drawing several shapes. Change the Graphics
object handle “e” to “pe” and enter (i.e. copy and
paste) the following code in your “On_paint”
hander:
private: System::Void
On_paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^
pe) {
Graphics^ g = pe->Graphics;
g->Clear(Color::AntiqueWhite);
Rectangle rect
= Form::ClientRectangle;
Rectangle
smallRect;
smallRect.X =
rect.X + rect.Width / 4;
smallRect.Y =
rect.Y + rect.Height / 4;
smallRect.Width = rect.Width / 2;
smallRect.Height = rect.Height / 2;
Pen^ redPen = gcnew Pen(Color::Red);
redPen->Width = 4;
g->DrawLine(redPen, 0, 0, rect.Width, rect.Height);
Pen^ bluePen =
gcnew Pen(Color::Blue);
bluePen->Width = 10;
g->DrawArc(
bluePen, smallRect, 90, 270 );
}
Then build and run the program.