Windows NT Systems Programming

[ Home | Syllabus |Course Notes]


Menus

PROBLEM: Add GUI controls for accessing message folders.

SOLUTION: menus, list boxes, dialog boxes


Menus

There are three ways to create menus:

  1. Programmatically
  2. Through data structures
    these first two allow dynamic creation of menus
  3. With Resource File
    this allows a visual editing tool to help create this

 


Resource Files

VC++ IDE has visual tools for creating resources (like menus). which automatically create a ".rc" and a resource.h file.

or you can edit directly.

Resource file for Project "simple1"

rainbow.gif (2243 bytes)

 

#include "resource.h"	// for ID constants
#include <afxres.h>	// for VK_F4

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDR_MENU1 MENU DISCARDABLE // names a menu IDR_MENU1
BEGIN
    POPUP "&Folder" // this will appear in the title bar of the window (see below)
    BEGIN
        MENUITEM "&Open\tCtrl-O",       IDM_FOLDER_OPEN // \t is tab Ctrl-O bound below
        MENUITEM "Close",              	   IDM_FOLDER_CLOSE // ID for Message Handler
        MENUITEM SEPARATOR
        MENUITEM "E&xit\tAlt-F4",          IDM_FOLDER_EXIT
    END
END

IDR_MENUACC ACCELERATORS PRELOAD MOVEABLE
BEGIN
   "O",	IDM_FOLDER_OPEN,	VIRTKEY,CONTROL
   VK_F4, IDM_FOLDER_EXIT, VIRTKEY,ALT
END



// Once loaded, these two resources give menus with mouse and keyboard control


Assigning IDs

Use a value greater than 100


Loading Menu and Accelerator Resources

CMainWindow::CMainWindow()
{
// Create the window
Create(NULL, "Message Folder Demo",
WS_OVERLAPPEDWINDOW,
CRect(0,0,700,500),
NULL,
MAKEINTRESOURCE(IDR_MENU1)); // Loads Menu from resource file and associates with window
LoadAccelTable(MAKEINTRESOURCE(IDR_MENUACC)); // load keyboard accelerators
}


What's Left?

rainbow.gif (2243 bytes)

void CMainWindow::OnCloseFolder()
{
	MessageBox("You clicked Close", "MsgFolder Close");
}
void CMainWindow::OnExit()
{
	if(MessageBox("Are you sure you want to Exit?", "MsgFolder Exit",
		MB_YESNO) == IDYES)
		SendMessage(WM_CLOSE, 0, 0); 
}

Copyright chris wild 1997.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: September 22, 1997.