![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Our First Example: HelloDlgOur first Visual C++ program will be dialog based, which means it will use a dialog box as its main window. This is the simplest general-purpose type of Visual C++ program, and because its so easy to work with, well look at it first. Our first program, HelloDlg, presents the user with a text box (also called an edit box or an edit control) and a button that displays the caption Click Me: ------------------------------------------------------ | | |------------------------------------------------------ | | | | | | | | ---------------------- ---------- | | | | | Click Me | | | ---------------------- ---------- | | | | | | | ------------------------------------------------------ When the user clicks the button, the program displays a simple message of greeting, Hello from Visual C++ in the text box: ------------------------------------------------------ | | |------------------------------------------------------ | | | | | | | | ---------------------- ---------- | | |Hello from Visual C++ | | Click Me | | | ---------------------- ---------- | | | | | | | ------------------------------------------------------ By writing this program, well create our first Visual C++ example, and well also review the Visual C++ Integrated Development Environment (IDE) and the ClassWizard tool. Creating HelloDlgWell begin the creation of HelloDlg with Visual C++ now. Open Visual C++, as shown in Figure 1.1; select the New item in the File menu to open the New dialog box, as shown in Figure 1.2. Visual C++ programs are handled as projects. To create our first project, HelloDlg, select the MFC AppWizard(exe) item in the Projects tab, as shown in
Figure 1.2, and enter HelloDlg as the projects name in the Project name box. Also enter the location where you want to store this project (in this book, well store our projects as folders in the c:\avc folder), and click the OK button to close the New dialog box. Clicking the OK button opens the MFC AppWizard, as shown in Figure 1.3. The AppWizard is another valuable Visual C++ tool. It writes the skeleton framework of our projects for us, usually saving a great deal of time. Click the option button marked Dialog based in the AppWizard, as shown in Figure 1.3, and click the Finish button to make the HelloDlg project dialog based. Finally, click OK in the New Project Information box that appears, and Visual C++ creates the HelloDlg project for us. At this point, Visual C++ opens the HelloDlg project, as shown in Figure 1.4. You can see the dialog window on which our project is based in the middle of the project. As you can see in Figure 1.4, Visual C++ has placed the message TODO: Place dialog controls here. in a label on the dialog box, and the first order of business is to remove that label. By clicking that label to select it, a dotted border appears around the label, and sizing handles (the small boxes in the border) appear to allow you to position or stretch the label as you like. Delete the label now by pressing the Del key. Now were ready to add the two new controls well need to this dialog window: a text box and a command button. We add controls using the control toolbox, which appears at right in Figure 1.4.
Click the text box tool (the second tool down on the right in Figure 1.4) in the toolbox now and draw a text box as shown in Figure 1.5. Then add a command button, using the button tool (the third tool down on the right), also shown in Figure 1.5. Now that weve added a text box to our program, we need to make it accessible to our code, which we do with the Visual C++ ClassWizard. Right-click the text box now and select the ClassWizard option, as shown in Figure 1.6. To make the text box accessible to the rest of the program, we use ClassWizard to connect the text in that text box to a variable in our program. Select the text box entry, IDC_EDIT1, in the ClassWizard now, as shown in Figure 1.6, and click the Add Variable button. This opens the Add Member Variable dialog box; type m_text in the Member variable name box, leaving the variable type as CString (the Visual C++ string class, which handles text strings), and click OK. This associates a new variable in our program, m_text, with the text in the text box. Click OK to close the ClassWizard. Next, we will connect the button to our code so we can display our greeting message when the user clicks that button. Right-click the button to open the Push Button Properties window and, selecting the General tab, set the buttons caption to Click Me. Then close the Push Button Properties window and double-click the button to open the Add Member Function box. Click OK in that box to accept the default function for button clicks, OnButton1. This opens the code for the program to the new function, OnButton1(), in a new window.
void CHelloDlgDlg::OnButton1() { // TODO: Add your control notification handler code here } Here, Visual C++ has given us a prompt in the form of a C++ one-line comment; we will replace that comment with the code for our program. Select the TODO line above in the open code window and replace that line with these two lines of code: void CHelloDlgDlg::OnButton1() { m_text = Hello from Visual C++; ⇐ UpdateData(false); ⇐ }
In this case, we set the m_text variable to the string Hello from Visual C++, but that does not, by itself, display that text in the text box. Instead, we must call the UpdateData() function with a value of false to update the text box with the text now in m_text, and we do that as just shown.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |