The Resource View of the MFC

Programming applications for Windows can be a walk in the park if you’re able to use the powerful options encapsulated in the Microsoft Foundation Class library properly. You’ve already learned a lot about these tools and now there is one more step ahead of us that you shouldn’t ignore. I invite you into this final part and together we’ll skim through the resource-related questions/options in MFC.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
March 26, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

If you are new to the MFC and haven't read the previous four segments, I strongly recommend delaying this article and searching for the previous parts of the series as they contain very important information about MFC. Having a strong grasp of the basics is required to understand the following pages of this final segment. Don't search too far as you should find them here at ASP Free.

I'm sure that by now you've figured out the basics of MFC and the way it works, but you may be wondering about the purpose of the resource view near the class and file view. Well the answer to this question is in this article, so please just read ahead. We'll start with Menu creation and editing followed by the Dialog boxes, but we are also going to cover assigning some Hot-keys to your menu, changing your icons for the application, and customizing your toolbar.

Of course, as you would already expect, my words are going to be accompanied at every step by code snippets and at the end of this article a hyperlink to the created application shall be present for you to play with it.

So find the voice within you that drives you towards programming and read this final part of the introduction to the world of MFC. As our schedule is hectic and we have many things to do, let's start already! Don't be shy or afraid, just "turn" to the next page.

Menu Editing

You can already see that Microsoft has included quite a few little tools into MFC that can enhance your programming speed dramatically. Resource manager has been included for even greater efficiency. With this handy tool we can customize our resources at a blazing fast speed. Resources can be categorized as Accelerator keys, the Dialog boxes, Icons, String Tables, Menus, Toolbars... and even a nice version of the info table.

The menu is at the base/top of each application window. You already see them everywhere, that is if you've spent at least 10 minutes in Windows. If a user wants to customize something, most of the time, the first place s/he expects this kind of option is the menu.

Considering this, your own application shouldn't be an exception. This way, any user can find his/her way in a familiar situation. Scaring away users by implementing something extremely unfamiliar or new isn't great. You may ask, how will we customize them? Well, the answer is quite simple; it's presented below.

All you need to do is open up the corresponding part of the resource tree and select IDR_MFC_exampleTYPE. That part is responsible for the mainframes menu. The initial customization is quite intuitive and can be done without any additional effort; typing the new menu string where you want a new menu is very straightforward. Additionally you may also insert separators by right clicking and selecting Add separator. For our application, let's make something like this.



At this time, the menus are useless, so we need to assign them some sort of functionality. Right click on the desired menu and select Add Event Handler...



Select the class that you want to assign and we are ready to go. In OnDataoptionsEnterdata, we can enter all the things we should be able to do and expect from pressing on the menu(s).

At this part, we'll learn how to add check boxes near a menu. It is quite a useful option and fits perfectly in our Use Blue Bk -> True section. To accomplish this, you need to repeat the above process once again, but this time choose the UPDATE_COMMAND_UI message type instead.

Add a _useBlue bool type variable that will indicate whether we draw with a blue background or not and set a start value of false. Add the COMMAND message so that we can change the value of the _useBlue; therefore, when you push the True menu we can signal to the application that a change has been made.

Each time the True menu is drawn, the OnUpdateUsebluebkTrue32775 function is called. So in it we indicate whether or not the check box has to be drawn. Add the following code in there. The result must be something like the screen shot below - a check box that disappears and appears upon selection.


void CMFC_exampleView::OnUsebluebkTrue()

{

_useBlue= (!_useBlue);

Invalidate();

}

 

void CMFC_exampleView::OnUpdateUsebluebkTrue32775(CCmdUI *pCmdUI)

{

pCmdUI->SetCheck(_useBlue);

}



A utility for Enter Data... will be integrated in the following page and there you are also going to find out how to create a Dialog Box. In the same way, you can implement radio boxes too.

Dialog Boxes

Dialog boxes are mostly used to interact with the user; they are called Dialog Boxes for this very reason. Initially, their purpose was to assure communication with the client/user. Of course, they can also stand by themselves as a true application, but that is a little more complicated and exceeds the goal of this series. Also, a Dialog box is modular by default; in plain English, it means that while you have a dialog box opened, you cannot switch to any other window in the application.

Sometimes you will need to switch between a dialog box and another client window; this can be realized by implementing a non-modular dialog box. I won't cover this part because this article is intended as an introduction, but don't hesitate to explore this more deeply should you find it intriguing. Google is your friend, don't forget that!

Our goal is to create a dialog box that will get a text from the user and draw that text in the client area. Then, as an extra, we say that a blue background can be used. Also, we shall encapsulate this in the Enter Data... menu. You have already made up a Dialog box if you open the Dialog part of the resource tree. This represents the Dialog box that appears when you push down the About key in the menu. But don't rush ahead. We'll start with the creation process. Follow the path: Resource View -> Right Click on the Tree view -> Add Resource... -> Dialog -> New. If you've done it correctly, you should have something like this:



Now we can start customizing some part of it. In the Property page, you'll find a multitude of ways you can change the style of the Dialog box. We won't mess with that part yet, so you can comprehend the essence of the remaining part.

First of all, just make sure that the Toolbox is present in your Visual Studio. If the answer is affirmative, then we'll add an Edit Control, a Check Box, and a Static text. You shouldn't have a problem with this part because it is a simple and straightforward process. Just select the proper option from the Toolbox and drag it into the box. Follow this process via some resizing until you get something like what's in the attached screen shot below.




Change the captions for the Static text and the check box as I did in the above image. Finally, we can include the dialog box into our project by right clicking on the Dialog box and choosing "Add a box." Enter a class name for the Dialog box and press Finish. The code is generated and we can start with the needed modifications. The name I have chosen is CTutorial. Also notice that we have this Dialog ID assigned: IDD_DIALOG1.



We assign a variable to the data entered in the CEdit control. Simply right click on the control in the resource view and select Add variable. Make sure you select the options as shown in the picture below. The image is pretty self-explanatory, so I won't discuss it in detail. Repeat the procedure for the check box - you add an m_checkbox bool variable.



As this is done, we can integrate our creation into the application. Go to the place where you added the OnDataoptionsEnterdata on the previous page and add the following line to the corresponding CPP:

#include "Tutorial.h"

This simple step creates our customized Dialog Box when we press the Enter Data... menu and proceed to add the following code at the end:


void CMFC_exampleView::OnDataoptionsEnterdata()

{

CTutorial dialog;

 

dialog.m_data = CString("Enter data");

dialog.m_checkbox = _useBlue;


INT_PTR result = dialog.DoModal();

if( result == IDOK)

{

this->_print = dialog.m_data;

this->_useBlue = dialog.m_checkbox;

this->Invalidate();

}


}


We declared a CString type _print variable in the CMFC_exampleView that stores the value we got from the Dialog box. For some on-screen result, add the following code in the OnDraw:


if(_useBlue)

{

pDC->SetBkColor(RGB(0,0,255));

}

pDC->TextOutW(0,20,_print);


Yes, we are done for now. You can start the application and see the results for yourself. I hope you enjoy it. The following page will finish up the resources.

Toolbar, Hot-Keys and Icons

It is quite annoying to search the Data Options and Enter Data menu, so maybe we can speed up the process a little. The solution to our problem is the Toolbar. The Toolbar already contains a few icons, like Cut, Copy, Paste, and so forth. The only thing left to do is add our own. Select the IDR_MAINFRAME toolbar in the resource view and you can add another after the help icon (question mark). Just push down and after that, play a little with the colors and draw your fancy pictogram.

Assigning the icon with the Data Enter... menu is done by linking the icon to the menus ID. Search in Properties for what ID the Enter Data... menu has and add the same ID to the icon. If you've followed this tutorial, the corresponding ID is ID_DATAOPTION_ENTERDATA. You can see this in the attached screen shot below.




That's all we need to do. From now on, starting the program and pressing the icon is the same as searching for the option in the menu. Future improvements can be made by assigning a Hot-Key to the application. To do this, visit the Accelerator part of the Resource and add the desired Hot-Key for the corresponding ID. For the Dialog Bar, we'll add the Ctrl+F5 accelerator. Do the following; the interface is intuitive so you can figure out any other combination.



Finally, we can also advocate the addition of an accelerator key for aesthetics and other obvious reasons by modifying the caption of our menu in the Properties page to this: "&Enter Data..tCtrl + F5". The important part is adding the t in the text.

Modifying the icon of your application is simply drawing an image for the document type icon or the applications icon. Just search the Icon section in the menu and modify IDR_MAINFRAME for the application, while the applications document in our case is the IDR_MFC_exampleTYPE.

The String table can be used to display some tooltips or store some constant text that can be accessed from the table anytime. For example, if we want to add a little tooltip that will appear in the status bar, then as soon as we hover the mouse pointer over the Enter Data... menu, we assign the desired text to the menu's ID. For example, check out the entered data below in the attached screenshot.



You can also use the resource to add bitmaps, and by referring to the bitmap's ID, you can load it in your application and work with it just as with any other. The advantage of this is that you don't have to search for the path of the application because it will be included in the resource and can be accessed with just a couple of clicks.

One last concept we need to cover is the version tab, which contains information about the current version of the program, the authoring company, copyright details, contact information, and so forth. Basically, if you want to mark the application as yours and enter information about who made it, then this is the place to do it.

You may have already figured out that every resource has a unique ID assigned to it. We can refer to the resource anywhere in our application by including "Resource.h" in the implementation file (this is where the IDs are defined) and using their IDs as identifiers. Click the button below to download the program that we made; feel free to experiment with it. You should find it especially useful if you were struggling.


Epilogue

The time has come for us to review the concepts we just finished exploring. This part was dedicated to demystifying the Resource View. By now you should know the basics of the Menu, Dialog Box, Accelerator keys, Icon, String Table, Toolbar, and Version History.

You should know a lot more if you followed this series passionately. We learned everything about the basics of MFC. Keep in mind that just because this is the end of this article doesn't mean that you can sit back and relax. Crossing the finish line of this series means that the timing couldn't be better for you to take things into your own hands.

Search for good books or good online guides on MFC. With the knowledge you gained here  you should be able to breeze through them with ease. Most importantly, remember that programming confidence and skill can be achieved only through practice. Search, see, implement, and learn "by doing it" until programming becomes second nature.

I wish you luck in this upcoming adventure and if you have any questions or you fail to see the whole picture, don't hesitate to visit and ask our friendly community over at DevHardware Forums and ASP Free Forums. There are no wrong questions, just wrong answers. Someone who doesn't ask questions won't get any answers. Thus, should you want to advance and are stuck somewhere, then the aforementioned communities are perfect places to begin seeking the answers.

blog comments powered by Disqus
BRAINDUMP ARTICLES

- Microsoft Windows 8 Committed to Cloud Compu...
- Independent Developers Favor Windows Phone 7
- Dell Introduces VMware-based Cloud
- Microsoft and Skype Agree to Acquisition Deal
- Transfer Contacts in Microsoft Outlook
- Zune`s Next Steps
- Safari Books Online Review
- Does Microsoft Get Touch Screens Now?
- Microsoft`s Record Quarterly Earnings Not En...
- Basic Operations and Registers in Assembly
- Assembly Coding within Visual C/C++ IDE
- New Microsoft Office Coming with a Twist
- Microsoft`s FUSE Labs Unveils Spindex Social...
- HP Slate with Windows 7: Dead or Alive?
- Windows Phone 7 Mobile OS to Rival Android a...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials