VC ++MFC Tutorial 4 & 5 (English)

zhaozj2021-02-08  226

Lesson 4: MFC Basics

Are you ready to start programming? No you are not. You do not want me to teach you a stupid 'hello world' application, do you? If you want to make the most of Visual C you have to use Microsoft Foundation Classes ( MFC). These classes are great because they wrap all of those handles we talked about in the first lesson with easy to use classes. The most important to you right now is the class CWnd. This wraps the functions which needed a window handle (HWND ). Remember That PostMessage Function I Mentioned?

Postmessage (Your_hWnd, WM_Paint, 0, 0);

Well Now We can take our window we..

MycWnd.PostMessage (wm_paint, 0, 0);

THIS DOES The Same Thing, But Now We don't have to keep track of the window. The Handle to the window a cwnd is associated with in the me. We can call the old post message....

:: PostMessage (mycwnd.m_hwnd, wm_paint, 0, 0);

Those colons (: :) Are used to tell mfc That We Are calling the old fashioned version of the function. You can get away, but I put the hesel See Them in Microsoft's Code.

The CWnd class is the base for several other classes. Like CButton and CDialog which I hope the names make self explanatory. From CButton you can access the windows handle also. (You'd be surprised how many things are windows .. Scroll bars, Edit Boxes, Tree Views, The Desktop ...) GOT ALL THAT! GREAT!

The next most important class, though you will not explicitly use it much, is CWinApp. This class is the backbone of all of your future MFC applications. This is the class that does the main dirty work under the hood. Your program will have a CWinApp class, which when created, starts the program execution. The main function called when the CWinApp is constructed is InitInstance (). in this function windows are created and the application is set up. Think of the InitInstance () function in CWinApp as the main () section in a C program.Let's move on to one last very useful MFC class that you will surely use: CString This is one of microsoft's support classes It is used to make string manipulation easier Since CString overrides many of... THE Common Operators, Like = and , You Can Do Things Like this:

Cstring strmystring;

Strmystring = "May the force be with you";

Strmystring = "Young jedi."

Printf ("% s", strmystring;

Lesson 5: Dialog-Based ApplicationsWe will not build a dialog application just yet, but I will tell you enough here so that you get the picture of what's going on in dialog applications Dialog apps are the simplest apps in my opinion In the.. IDE, go to File, New, Projects, MFC AppWizard (exe), and type in a project name. Hit next. Select Dialog Application as the type of application and then hit finish. Next go to the File View. You will see the source files created automagically. You should be able to compile and run the application as it is. What is going on in all these files? Everything boils down to the CWinApp derived class and the CDialog derived class (which is derived from CWnd). Look in the source file named after your project. you should see a the InitInstance () function there. Inside of that function you can see that a dialog class is constructed, it is set as the 'main window' of the application, and it is Displayed with the domodal () Function. Once You Exit your dialog app, the DoModal () function returns and your dialog is hidden. InitInstance () returns FALSE and your application ends. Now the question is, "What is DoModal ()?" There are 2 ways to create a dialog, Modal and Modeless. A Modal dialog suspends the program until you press OK, Cancel, or Close. On the other hand a modeless dialog can remain opened while allowing you to press buttons and whatnot on the rest of the application. An example of a Modal dialog is one of those annoying Error Boxes with an ok button on it. That is the only type of dialog we'll talk about here. To create a modal dialog, you simply need to call the dialog '

s DoModal () function. It returns either IDOK or IDCANCEL depending on how you exited the dialog. Internally the DoModal () will call OnInitDialog () which is a good place to initialize your dialog variables. If you create a dialog app, you will notice that a default dialog class and resource is created for you. The file name for the class will be your project name with a 'Dlg' tacked on at the end. Though we are not making an application just yet, I have to tell you how to put something useful on the dialog. First open up the resource view. Open up your dialog in the editor. Right click on the dialog and select 'properties'. Make sure the' Visible 'checkbox is checked. If it isn' t, you will not be able to see your dialog. (Remember this, it will come back to haunt you in the future). you can change the Dialog's caption here as well as other things. Now drag a button control onto the dialog Somewhere. Right Click ON IT and SELECT Properties. You can change the id of the button to something m ore descriptive like IDC_SHOWMESSAGE. You can also change the text on the button to something more descriptive like 'Show Message'. You have a button now. But it does nothing. You can easily fix that. Press Ctrl-W to bring up the class Wizard. make Sure You are on the first tab. You Should See Your Dialogs Name and the button's id in the list on the left. if you select the dialog '

s name you can see all of the functions and messages that the class wizard will let you add code for on the right. You can see the WM_PAINT and all the other messages we talked about. If you select the ID of the button you can see the messages that the button sends. Double click on the CLICK message and accept the default function name. you see that it appears in the list at the bottom. Now double-click on the function in the bottom list. Shazam, you are transported right To the cpp file where you need to fill in the code. let's do something easy. Just add the line: AfxMessageBox ("stupid text");

Compile, Build and Run Your Application (Just Press Ctrl-F5). If you put ... Functions That Are useful. I think the message box one is the most useful out of all of them. It is instant feedback). That was dead simple. you see how to add message handlers now (like for the button click), but you need at least one more vital bit of information to make a useful dialog box. How to use the automatic data handling stuff in MFC. This is best described by going back to the code again. Bring up the resource editor one more time and this time add an Edit Box to your dialog. Again right click on it and give it a nice and friendly ID. Hit Ctrl-W and bring up the class wizard. This time go to the second Tab. Here is where you add member variables that are associated with the controls on your Dialog. Double Click on The Edit Box's ID. You Now Have The Choice to Add A Variable To Your Project. Give IT A name like m_strMessage since it will be a string for our message box. Make sure the data type selected is CString there at the bottom. Press OK. And press it again to get out of the class wizard. When you add a member variable to a dialog like this, you can set the value to that in the control of the dialog by calling UpdateData (TRUE); Likewise you can change the data displayed in the dialog to represent your variable's value by calling UpdateData (FALSE);

Let's put this to use and finish up this less oninitdialog () Function and in it put the two line: m_StrMessage = "Initial text";

Updatedata (FALSE);

THEN Go THE FUNCTION WHICH IS CALLED WHEN THE button IS PRESSED and Replace The AfxMessagebox () Line with these Two Lines: Updatedata (TRUE); AFXMESSAGEBOX;

Ok, we are done. What we just did was set the initial text in the edit box to "Initial text" with the UpdateData (FALSE). Then when the button is pressed the text in the message box is displayed since we get the text from the dialog with UpdateData (TRUE). by playing around and by reading the help (or a good book) you will learn how to use the other controls. On of the trickiest to figure out with out help is the slider bar. If you Use One of these You Will Have To Handle The Dialog's Scroll Messages. That's Just a hint for you.

转载请注明原文地址:https://www.9cbs.com/read-2787.html

New Post(0)