MFC - Windows Resources

A resource is a text file that allows the compiler to manage objects such as pictures, sounds, mouse cursors, dialog boxes, etc. Microsoft Visual Studio makes creating a resource file particularly easy by providing the necessary tools in the same environment used to program. This means, you usually do not have to use an external application to create or configure a resource file. Following are some important features related to resources.

  • Resources are interface elements that provide information to the user.

  • Bitmaps, icons, toolbars, and cursors are all resources.

  • Some resources can be manipulated to perform an action such as selecting from a menu or entering data in dialog box.

  • An application can use various resources that behave independently of each other, these resources are grouped into a text file that has the *.rc extension.

  • Most resources are created by selecting the desired one from the Add Resource dialog box.

Add Resource
  • The Add Resource dialog box provides an extensive list of resources which can be used as per requirements, but if you need something which is not available then you can add it manually to the *.rc file before executing the program.


An identifier is a symbol which is a constant integer whose name usually starts with ID. It consists of two parts − a text string (symbol name) mapped to an integer value (symbol value).

  • Symbols provide a descriptive way of referring to resources and user-interface objects, both in your source code and while you're working with them in the resource editors.

  • When you create a new resource or resource object, the resource editors provide a default name for the resource, for example, IDC_DIALOG1, and assign a value to it.

  • The name-plus-value definition is stored in the Resource.h file.

Step 1 − Let us look into our CMFCDialogDemo example from the last chapter in which we have created a dialog box and its ID is IDD_EXAMPLE_DLG.


Step 2 − Go to the Solution Explorer, you will see the resource.h file under Header Files. Continue by opening this file in editor and you will see the dialog box identifier and its integer value as well.



An icon is a small picture used on a window which represents an application. It is used in two main scenarios.

  • On a Window's frame, it is displayed on the left side of the Window name on the title bar.

  • In Windows Explorer, on the Desktop, in My Computer, or in the Control Panel window.

If you look at our MFCModalDemo example, you will see that Visual studio was using a default icon for the title bar as shown in the following snapshot.


You can create your own icon by following the steps given below −

Step 1 − Right-click on your project and select Add → Resources, you will see the Add Resources dialog box.

Select Add Resources

Step 2 − Select Icon and click New button and you will see the following icon.


Step 3 − In Solution Explorer, go to Resource View and expand MFCModalDemo > Icon. You will see two icons. The IDR_MAINFRAME is the default one and IDI_ICON1 is the newly created icon.

Step 4 − Right-click on the newly Created icon and select Properties.

Step 5 − IDI_ICON1 is the ID of this icon, now Let us change this ID to IDR_MYICON.

Step 6 − You can now change this icon in the designer as per your requirements. We will use the same icon.

Step 7 − Save this icon.

Step 8 − Go to the CMFCModalDemoDlg constructor in CMFCModalDemoDlg.cpp file which will look like the following code.

CMFCModalDemoDlg::CMFCModalDemoDlg(CWnd* pParent /* = NULL*/)
   : CDialogEx(IDD_MFCMODALDEMO_DIALOG, pParent) {
   m_hIcon = AfxGetApp() -> LoadIcon(IDR_MAINFRAME);

Step 9 − You can now see that the default icon is loaded in the constructor. Let us change it to IDR_ MYICON as shown in the following code.

CMFCModalDemoDlg::CMFCModalDemoDlg(CWnd* pParent /* = NULL*/)
   : CDialogEx(IDD_MFCMODALDEMO_DIALOG, pParent) {
   m_hIcon = AfxGetApp() -> LoadIcon(IDR_ MYICON);

Step 10 − When the above code is compiled and executed, you will see the new icon is displayed on the dialog box.

Modal Demo


Menus allow you to arrange commands in a logical and easy-to-find fashion. With the Menu editor, you can create and edit menus by working directly with a menu bar that closely resembles the one in your finished application. To create a menu, follow the steps given below −

Step 1 − Right-click on your project and select Add → Resources. You will see the Add Resources dialog box.

Add Resources Menu

Step 2 − Select Menu and click New. You will see the rectangle that contains "Type Here" on the menu bar.

Type Here on Menu Bar

Step 3 − Write some menu options like File, Edit, etc. as shown in the following snapshot.

Menu Options

Step 4 − If you expand the Menu folder in Resource View, you will see the Menu identifier IDR_MENU1. Right-click on this identifier and change it to IDM_MAINMENU.

Menu Identifier

Step 5 − Save all the changes.

Step 6 − We need to attach this menu to our dialog box. Expand your Dialog folder in Solution Explorer and double click on the dialog box identifier.

Dialog Folder

Step 7 − You will see the menu field in the Properties. Select the Menu identifier from the dropdown as shown above.

Step 8 − Run this application and you will see the following dialog box which also contains menu options.

Menu Option


A toolbar is a Windows control that allows the user to perform some actions on a form by clicking a button instead of using a menu.

  • A toolbar provides a convenient group of buttons that simplifies the user's job by bringing the most accessible actions as buttons.

  • A toolbar can bring such common actions closer to the user.

  • Toolbars usually display under the main menu.

  • They can be equipped with buttons but sometimes their buttons or some of their buttons have a caption.

  • Toolbars can also be equipped with other types of controls.

To create a toolbar, following are the steps.

Step 1 − Right-click on your project and select Add → Resources. You will see the Add Resources dialog box.


Step 2 − Select Toolbar and click New. You will see the following screen.

Select Toolbar

Step 3 − Design your toolbar in the designer as shown in the following screenshot and specify the IDs as well.

Design Toolbar

Step 4 − Add these two variables in CMFCModalDemoDlg class.

   CToolBar m_wndToolBar;
   BOOL butD;

Step 5 − Following is the complete implementation of CMFCModalDemoDlg in CMFCModalDemoDlg.h file −

class CMFCModalDemoDlg : public CDialogEx {
   // Construction
      CMFCModalDemoDlg(CWnd* pParent = NULL); // standard constructor
   // Dialog Data

      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
   // Implementation
      HICON m_hIcon;
      CToolBar m_wndToolBar;
      BOOL butD;
      // Generated message map functions
      virtual BOOL OnInitDialog();
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
      afx_msg void OnBnClickedOk();

Step 6 − Update CMFCModalDemoDlg::OnInitDialog() as shown in the following code.

BOOL CMFCModalDemoDlg::OnInitDialog() {
   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);       // Set big icon
   SetIcon(m_hIcon, FALSE);      // Set small icon
   if (!m_wndToolBar.Create(this)
      || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
      //if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD |
      // !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1)) {
         TRACE0("Failed to Create Dialog Toolbar\n");
      butD = TRUE;
      CRect rcClientOld; // Old Client Rect
      CRect rcClientNew; // New Client Rect with Tollbar Added
      // Retrive the Old Client WindowSize
      // Called to reposition and resize control bars in the client area of a window
      // The reposQuery FLAG does not really traw the Toolbar. It only does the calculations.
      // And puts the new ClientRect values in rcClientNew so we can do the rest of the Math.
      RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0, reposQuery, rcClientNew);
      // All of the Child Windows (Controls) now need to be moved so the Tollbar does not cover them up.
      // Offest to move all child controls after adding Tollbar 
      CPoint ptOffset(rcClientNew.left - rcClientOld.left, -; 
      CRect rcChild;
      CWnd* pwndChild = GetWindow(GW_CHILD); //Handle to the Dialog Controls
      while (pwndChild) // Cycle through all child controls {
         pwndChild -> GetWindowRect(rcChild); // Get the child control RECT
         // Changes the Child Rect by the values of the claculated offset
         pwndChild -> MoveWindow(rcChild, FALSE); // Move the Child Control
         pwndChild = pwndChild -> GetNextWindow();
      CRect rcWindow;
      // Get the RECT of the Dialog
      // Increase width to new Client Width
      rcWindow.right += rcClientOld.Width() - rcClientNew.Width();
      // Increase height to new Client Height
       rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();
      // Redraw Window
      MoveWindow(rcWindow, FALSE);
      // Now we REALLY Redraw the Toolbar
   // TODO: Add extra initialization here

   return TRUE; // return TRUE unless you set the focus to a control

Step 7 − Run this application. You will see the following dialog box which also contains the toolbar.



An access key is a letter that allows the user to perform a menu action faster by using the keyboard instead of the mouse. This is usually faster because the user would not need to position the mouse anywhere, which reduces the time it takes to perform the action.

Step 1 − To create an access key, type an ampersand "&" on the left of the menu item.

Create an Access Key

Step 2 − Repeat this step for all menu options. Run this application and press Alt. You will see that the first letter of all menu options are underlined.

Menu Option

Shortcut Key

A shortcut key is a key or a combination of keys used by advanced users to perform an action that would otherwise be done on a menu item. Most shortcuts are a combination of the Ctrl key simultaneously pressed with a letter key. For example, Ctrl + N, Ctrl + O, or Ctrl + D.

To create a shortcut, on the right side of the string that makes up a menu caption, rightclick on the menu item and select properties.

In the Caption field type \t followed by the desired combination as shown below for the New menu option. Repeat the step for all menu options.

Shortcut Key

Accelerator Table

An Accelerator Table is a list of items where each item of the table combines an identifier, a shortcut key, and a constant number that specifies the kind of accelerator key. Just like the other resources, an accelerator table can be created manually in a .rc file. Following are the steps to create an accelerator table.

Step 1 − To create an accelerator table, right-click on *.rc file in the solution explorer.

Accelerator Table

Step 2 − Select Accelerator and click New.

Select Accelerator

Step 3 − Click the arrow of the ID combo box and select menu Items.

Accelerator Table

Step 4 − Select Ctrl from the Modifier dropdown.

Step 5 − Click the Key box and type the respective Keys for both menu options.

We will also add New menu item event handler to testing. Right-click on the New menu option.

Event Handler

Step 6 − You can specify a class, message type and handler name. For now, let us leave it as it is and click Add and Edit button.

Message Type  Handler Name

Step 7 − Select Add Event Handler.

Step 8 − You will now see the event added at the end of the CMFCModalDemoDlg.cpp file.

void CMFCModalDemoDlg::OnFileNew() {
   // TODO: Add your command handler code here
   MessageBox(L"File > New menu option");

Step 9Now Let us add a message box that will display the simple menu option message.

To start accelerator table in working add the HACCEL variable and ProcessMessageFilter as shown in the following CMFCModalDemoApp.

class CMFCModalDemoApp : public CWinApp {
   // Overrides
      virtual BOOL InitInstance();
      HACCEL m_hAccelTable;
      // Implementation

      virtual BOOL ProcessMessageFilter(int code, LPMSG lpMsg);

Step 10 − Load Accelerator and the following call in the CMFCModalDemoApp::InitInstance().

m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(),

Step 11 − Here is the implementation of ProcessMessageFilter.

BOOL CMFCModalDemoApp::ProcessMessageFilter(int code, LPMSG lpMsg) {
   if (code >= 0 && m_pMainWnd && m_hAccelTable) {
      if (::TranslateAccelerator(m_pMainWnd -> m_hWnd, m_hAccelTable, lpMsg))
      return TRUE;
   return CWinApp::ProcessMessageFilter(code, lpMsg);

Step 12 − When the above code is compiled and executed, you will see the following output.

Accelerator Result

Step 13 − Press Alt button followed by F key and then N key or Ctrl + N. You will see the following message.

Accelerator Table