How to use Graphics in Visual C++

The procedure contains three steps——start visual studio, new a project in visual studio and use graphics, as follows.

Step 1: Start Visual Studio

Ø         Click “start” button. Select “programs” menu, then select “Microsoft Visual Studio 6.0” in it. You will see an icon named “Microsoft Visual C++ 6.0 “. Click the icon to start Visual C++ 6.0.

Step 2: New a project in Visual Studio

Ø         Click “Close” button, If a dialog with title “Tip of the Day” pops out.

Ø         Select the “New” item in “File” menu. You will see the following figure.

Ø         Select “MFC AppWizard[exe]” in the left pane as the above figure.

Ø         Set the directory your project locates by changing the directory name in the TextField labeled with “Location” or clicking the button on the right of that TextField, for example “H:\”.

Ø         Input the project name in TextField labeled with “Project name”, for example “Test”.

Ø         Click “OK” button, then you will see the following figure.

Ø         Select “Single document”. Click “Finish” button.

Ø         You will see the following figure, then click “OK” button.

Ø         Now, a visual C++ project is set up. This project has five classes named CAboutDlg, CMainFrame, CTestApp, CTestDoc and CTestView (Test is my project’s name. If your project name is XXX, the last three files’ names will be CXXXApp, CXXXDoc and CXXXView). You can check this by double clicking the plus sign in the left pane, as shown in the following figure.

 

Use Graphics

Ø         Click the plus sign on the left of “CTestView”. You will see a list of methods.

Ø         Double click “OnDraw(CDC *pDC)”. The body of this method will be shown on the right pane. In default, the body of this method is :

void CTest1View::OnDraw(CDC* pDC)

{

       CTest1Doc* pDoc = GetDocument();

       ASSERT_VALID(pDoc);

       // TODO: add draw code for native data here

}

Ø         Class CDC provides many methods to draw shapes on the screen, so I call its methods to draw rectangle and circle. Insert the following codes between the line “//TODO: add draw code for native data here” and line “}”:

pDC->Rectangle(100,100,50,50);

pDC->Ellipse(100,100,200,200);

Ø         Select “Rebuild all” item under “Build” menu to compile the files.

Ø         Select “Execute Test.exe” under “Build” menu to run the project. The result is shown in the following figure.