Using Visual C++ - SJSU



Using Visual C++

 

Projects

 

When you open Visual for the first time, you’ll see the Start Page:

 

[pic]

 

Click on New Project:

[pic]

 

Scroll down and select MFC application. MFC means Microsoft Foundation Classes (see the previous lecture).

Now you need to choose a name and location. In the lab, you will work in C:\temp and in subfolders you create there. DO NOT WORK on the J: drive!! First of all, you have a limited amount of space available there, and using it up can cause Visual C++ to crash. You might lose your work. Second, the network connection from MacQuarrie Hall (where your J: drive is) to the lab is very slow.

 

Therefore enter C:\temp in the location field (when in the lab), or whatever folder you choose at home.

 

Now, give your project a name. Usually the lab instructions will specify a name you are to use. Let's say the specified name was hello54. Then you type “hello54” into the name field. Note that Visual Studio automatically adds "\hello54" to the location, so your project will be created in

C:\temp\hello54. Thus you DO NOT need to create a special folder in advance for your project.

 

If you had typed in C:\temp\hello54 for the location, you might find your project in C:\temp\hello54\hello54, which is probably not what you want.

Click OK. Now you’ll see:

 

[pic]

 

From here you can change the project settings. Let’s change from “multiple document interface” (MDI) to “single document interface” (SDI). Select Application Type at the left of the window, and then select the correct radio button:

 

[pic]

 

Notepad, for example, is an SDI program: you can only have one file open at a time. Word is an MDI program: you can have many files open at the same time. Each file ("document") gets a window of its own.

 

Most of the exercises in this class will be SDI programs. Later on, you will learn what some of the other options are that can be changed from this “App Wizard” program. For now, just click Finish. That brings you back to the Start Page—close it, you don’t need it any more.

 

In fact, the Start Page is just for beginners. You can choose File | New and reach the same screens as above. I recommend that you go to Tools | Options | General | At Startup and set the option Load last loaded solution

instead of Show Start Page.

 

[pic]

 

Now, returning to where we were before changing options, you should see in one of the frames, the Solution Explorer

 

[pic]

 

This window shows all the files that AppWizard has created for you. There a lot of them! Note the tabs at the bottom—shift to Class View, and expand the node by hello54 by clicking on the + sign:

 

[pic]

 

This shows the classes that AppWizard has defined for you. Expanding the View Class node we see

 

[pic]

 

This lists some of the methods that AppWizard has written for you. With the help of AppWizard, you have just written a Windows program!

 

You don't believe it? Let's run the program. Up on the toolbar of Visual Studio you see something like this:

 

[pic]

 

The two buttons on the bottom row of this picture will each build your project. If you hold the mouse over a button you’ll get a small “tooltip” window that explains what the button does. Either of these buttons will work. (A “solution” can contain several projects, but our “solution” only has one project, so the buttons have the same effect here.)

 

Once it builds successfully, click that little blue triangle nest to the Debug box. That will run the program (in debug mode, which you want).

 

In the bottom frame you'll get some output showing what is happening: this or that file is being compiled, the program is being linked, etc. When it's done you can run the program. It really is a running Windows program:

 

• It has a menu.

• It has a toolbar.

• You can resize the window.

• You can close the program either from File | Exit or from the close box on the title bar.

• File | Save As works: you can save a file, giving it a name, and see it there available to open. File Open works too. Of course your files have no real content--there is no information to save. The corresponding toolbar buttons work too.

• File | Print lets you choose a printer and print. Of course the page printed is blank, but it does come out of the printer.

• Help brings up an About box complete with text and icon.

• It has a status bar at the bottom of the window, which contains text that changes as you move the mouse over the toolbar.

• It has ToolTips--little yellow windows that come up as you move the mouse over the toolbar.

 

Now let's look at the source code that App Wizard wrote for us. Here's the list of classes again. This list is called Class View:

 

 

They all begin with "C", which is characteristic of the MFC classes. The class CAboutDlg defines your About box, which comes up when Help is chosen. CMainFrame defines your "frame window". This is the program's main window, which holds the "document". There will be only one document at a time since this is an SDI application, so the document window will fill up the frame window. The two cannot really be visually separated in an SDI application, but they are nevertheless distinct windows. The classes CHello54App and CMainFrame contain some initialization code and some places you can put custom initialization code.

 

The main classes of interest are CHello54Doc and CHello54View. These are derived from CDoc and CView respectively. They are called your "document class" and your "view class". Exactly why there are these two classes will be discussed in another lecture on the Document-View architecture. In a nutshell: the document stores the data, while the view lets the user see it. There can be different views of the same data, as for example the normal view in Word versus the Print Preview view or the Page Layout view.

 

To write your own program, you will add code to a method that AppWizard already defined; and sometimes you will add new methods or classes and implement them.

 

To edit a method, just double-click it in Class View. For example,

double-click OnDraw in the view class:

/////////////////////////////////////////////////////////////////////////////

// CHello54View drawing

 

void CHello54View::OnDraw(CDC* pDC)

{

CHello54Doc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// TODO: add draw code for native data here

}

 

It is our present aim to understand this code snippet thoroughly, even though it does nothing visible.

 

• When is it called?

• Who or what calls it?

• What is a CDC?

• What is GetDocument and why is it called?

• What is ASSERT_VALID?

 

 

We will take the easy questions first.

 

• ASSERT_VALID is in all caps because it is #-defined. It is a macro. A macro works like a #-defined constant except it takes an argument. This particular macro makes some checks to see if pDoc looks like a valid pointer to a CDocument object. For example, NULL won't pass this test. In case the test does not succeed, ASSERT_VALID will cause the program to exit, outputting some kind of informative message about what went wrong.

• As the code is, it won't fail, because GetDocument has just returned a pointer to the (one and only) document. In an SDI application there is always one and only one document. If you close it, the application exits. If you open another file, the contents of the document are replaced with new contents, but the document is not destroyed and re-created.

• OnDraw is called in response to the WM_PAINT message. This message is sent to your program when the system finds that your window (or part of it) needs repainting. It is called by the WinMain somewhere in the guts of MFC (somewhere in mfc.dll), when the WM_PAINT message is received.

• Now for the hardest of the questions: What is a CDC?

 

DC stands for "device context". The first C is just the "signature" of MFC--all MFC classes begin with "C". In the Windows API (or SDK in the textbook's terminology) you would manipulate a DC by an identifying number called its "handle". That would be an HDC. In MFC, an HDC is a member variable of the CDC class, and device contexts are manipulated by "wrapping" them in an object of class CDC. There are many member functions of the CDC class which produce graphic output. For example, the method TextOut is used to write a string at a specific location.

 

The code that calls OnDraw (which you don't see, it's in mfc.dll somewhere) first calls BeginPaint to get an HDC. Then it wraps that HDC in an object of type CDC (that is, it creates an object of type CDC and stores the HDC in a member variable of that object). It passes a pointer to this CDC as the actual parameter of OnDraw. You get to work with this CDC in the code you add to OnDraw. After OnDraw exits, the code that called it can "delete" the HDC--as an MFC programmer, you are not responsible for that detail.

 

In Friday's lab, you will add some code to OnDraw and produce a customized Windows program. For example:

 

/////////////////////////////////////////////////////////////////////////////

// CHello54View drawing

 

void CHello54View::OnDraw(CDC* pDC)

{

CHello54Doc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// TODO: add draw code for native data here

pDC->TextOut(5,5,"Hello World");

 

}

 

 

The upper left corner of the text will be at pixel coordinates (5,5).

These are "client coordinates"-- (0,0) is at the upper left corner of the client area of the window. This text will be printed in a default size and default font.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download