English 中文(简体)
MFC - Windows Resources
  • 时间:2024-11-03

MFC - Windows Resources


Previous Page Next Page  

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 apppcation 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 apppcation 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 pst 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.

Identifiers

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.

CMFCDialogDemo

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.

Identifiers2

Icons

An icon is a small picture used on a window which represents an apppcation. 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.

Icons

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

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

Select Add Resources

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

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-cpck 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 pke 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

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 apppcation. To create a menu, follow the steps given below −

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

Add Resources Menu

Step 2 − Select Menu and cpck 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 pke 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-cpck 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 cpck 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 apppcation and you will see the following dialog box which also contains menu options.

Menu Option

Toolbars

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

    A toolbar provides a convenient group of buttons that simppfies 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-cpck on your project and select Add → Resources. You will see the Add Resources dialog box.

Toolbar

Step 2 − Select Toolbar and cpck 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 : pubpc CDialogEx {
   // Construction
   pubpc:
      CMFCModalDemoDlg(CWnd* pParent = NULL); // standard constructor
   // Dialog Data
   #ifdef AFX_DESIGN_TIME
      enum { IDD = IDD_MFCMODALDEMO_DIALOG };
   #endif

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

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

BOOL CMFCModalDemoDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();
   
   // Set the icon for this dialog. The framework does this automatically
   // when the apppcation 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 |
      // WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS |
      // CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
      // !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1)) {
         TRACE0("Failed to Create Dialog Toolbar
");
         EndDialog(IDCANCEL);
      }
      butD = TRUE;
      CRect rcCpentOld; // Old Cpent Rect
      CRect rcCpentNew; // New Cpent Rect with Tollbar Added
		
      // Retrive the Old Cpent WindowSize
      // Called to reposition and resize control bars in the cpent area of a window
      // The reposQuery FLAG does not really traw the Toolbar. It only does the calculations.
      // And puts the new CpentRect values in rcCpentNew so we can do the rest of the Math.
      
      GetCpentRect(rcCpentOld);
      RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0, reposQuery, rcCpentNew);
      // 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(rcCpentNew.left - rcCpentOld.left, rcCpentNew.top - rcCpentOld.top); 
		 
      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
         ScreenToCpent(rcChild);
          
         // Changes the Child Rect by the values of the claculated offset
         rcChild.OffsetRect(ptOffset);
         pwndChild -> MoveWindow(rcChild, FALSE); // Move the Child Control
         pwndChild = pwndChild -> GetNextWindow();
      }
       
      CRect rcWindow;
      // Get the RECT of the Dialog
      GetWindowRect(rcWindow);
       
      // Increase width to new Cpent Width
      rcWindow.right += rcCpentOld.Width() - rcCpentNew.Width();
       
      // Increase height to new Cpent Height
       rcWindow.bottom += rcCpentOld.Height() - rcCpentNew.Height();
      // Redraw Window
      MoveWindow(rcWindow, FALSE);
       
      // Now we REALLY Redraw the Toolbar
      RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
       
   // TODO: Add extra initiapzation here

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

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

Toolbar4

Accelerators

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 apppcation and press Alt. You will see that the first letter of all menu options are underpned.

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, rightcpck on the menu item and select properties.

In the Caption field type 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 pst 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 pke 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-cpck on *.rc file in the solution explorer.

Accelerator Table

Step 2 − Select Accelerator and cpck New.

Select Accelerator

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

Accelerator Table

Step 4 − Select Ctrl from the Modifier dropdown.

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

We will also add New menu item event handler to testing. Right-cpck 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 cpck 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 : pubpc CWinApp {
   pubpc:
      CMFCModalDemoApp();
   
   // Overrides
   pubpc:
      virtual BOOL InitInstance();
      HACCEL m_hAccelTable;
      
      // Implementation

      DECLARE_MESSAGE_MAP()
      virtual BOOL ProcessMessageFilter(int code, LPMSG lpMsg);
};

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

m_hAccelTable = LoadAccelerators(AfxGetInstanceHandle(),
   MAKEINTRESOURCE(IDR_ACCELERATOR1));

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 Advertisements