分享
 
 
 

VC++/MFC 教程4&5(英文)

王朝vc·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Lesson 4: MFC Basics

Are you ready to start programming? No you are not. You don't 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 windows class and call it's member function. MyCWnd.PostMessage(WM_PAINT, 0, 0);

This does the same thing, but now we don't have to keep track of the window handles. But don't be fooled, they are still there. We can still use them too. They are now just member variables of the class. The handle to the window a CWnd is associated with in the member variable m_hWnd. We can call the old post message this way: ::PostMessage(MyCWnd.m_hWnd, WM_PAINT, 0,0);

Those colons (::) are used to tell MFC that we are calling the old fashioned versions of the function. You can get away without using them most of the time, but I put them here so you won't be too confused when you 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 won't 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 won't 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 aren't 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 won't 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 more 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 press the button, you should see a message box pop up when you press the new button. (There are some Afx... 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 lesson. Go towards the end of your OnInitDialog() function and in it put the two lines: m_strMessage = "Initial text";

UpdateData(FALSE);

Then go to the function which is called when the button is pressed and replace the AfxMessageBox() line with these two lines: UpdateData(TRUE);

AfxMessageBox(m_strMessage);

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有