Advanced MFC Programming

Advanced MFC Programming Advanced MFC Programming

math.hcmuns.edu.vn
from math.hcmuns.edu.vn More from this publisher
11.04.2014 Views

Chapter 2. Menu void CMenuDoc::OnFileClose() { CMenu menu; } menu.LoadMenu(IDR_MAINFRAME); AfxGetMainWnd()->SetMenu(&menu); AfxGetMainWnd()->DrawMenuBar(); menu.Detach(); In the above three member functions, we use a local variable menu to load the menu resource. It will be destroyed after the function exits. So before the variable goes out of scope, we must call function CMenu::Detach() to release the loaded menu resource so that it can continue to be used by the application. Otherwise, the menu resource will be destroyed automatically. Summary 1) In order to execute commands, we need to handle message WM_COMMAND. In order to update user interfaces of menu, we need to handle message UPDATE_COMMAND_UI. 1) To implement right-click menu, we need to first prepare a menu resource, then trap WM_RBUTTONDOWN message. Within the message handler, we can use CMenu type variable to load the menu resource, and call CMenu::TrackPopupMenu(…) to activate the menu and track mouse activities. Before the menu is shown, we can call functions CMenu::EnableMenuItem(…) and CMenu::CheckMenuItem(…) to set the states of the menu items. 1) To add or remove a menu item dynamically, we need to call functions CMenu::InsertMenu(…) and CMenu::RemoveMenu(…). 1) With function CMenu::SetMenuItemBitmaps(…), we can use bitmap images to implement checked and unchecked states for a menu item. 1) A window’s standard menu can be obtained by calling function CWnd::GetMenu(), and the application’s system menu can be obtained by calling function CWnd::GetSysMenu(…). 1) We can change a normal menu item to a bitmap menu item, a separator, or a sub-menu by calling function CMenu::ModifyMenu(…). 1) Owner-draw menu can be implemented by setting MF_OWNERDRAW style then overriding functions CMenu::MeasureItem(…) and CMenu::DrawItem(…). 1) We can change the whole menu attached to a window by calling function CWnd::SetMenu(…). 56

Chapter 3. Splitter Window Chapter 3 Splitter Window Asplitter window resides within the frame window. It is divided into several panes, each pane can have a different size. Splitter window provides the user with several different views for monitoring data contained in the document at the same time. Normally, the size of each pane can be adjusted freely, this gives the user a better view of data. There are two types of splitter windows: Dynamic Splitter Window and Static Splitter Window. For a dynamic splitter window, all views within the splitter window are of the same type. The user can create new panes or remove old panes on the fly. For a static splitter window, the views could be of different types and the number of panes has to be fixed at the beginning. In this case, the user can not add or delete views after the program has started. Both SDI and MDI applications can have splitter windows. In an SDI application, the splitter window is embedded in the mainframe window. In an MDI application, it is embedded in the child frame window. 3.1 Implementing Static Splitter Windows In MFC, class CSplitterWnd is used to implement window splitting. To split a window into several panes, we must first declare a CSplitterWnd type variable in the frame window class. One CSplitterWnd can divide window into M×N sub-panes. The splitter window can be nested, which means we can further split a single pane into several sub panes by using another CSplitterWnd type variable. So if we want to create an unevenly divided splitter window, we need to declare more than one CSplitterWnd type variables. For example, if we want to create a splitter window that has two rows, the first row has two columns and the second row has two columns, we need to first split the client area into a 1×2 splitter window, then split the first row into a 2×1 splitter window (Figure 3-1). Divide the window into row 0 and row 1 Divide row 0 into column 0 and column 1 Figure 3-1. Create irregularly divided splitter window To create static splitter window, first we need to declare CSplitterWnd type variable(s) in the frame window class, then in frame window’s OnCreateClient(…) member function, call functions CSplitterWnd::CreateStatic(…) and CSplitterWnd::CreateView(…). Here, function CSplitterWnd:: CreateStatic(…) is used to split the window into several panes and CSplitterWnd::CreateView(…) is used to attach a view to each pane. 57

Chapter 3. Splitter Window<br />

Chapter 3<br />

Splitter Window<br />

Asplitter window resides within the frame window. It is divided into several panes, each pane can<br />

have a different size. Splitter window provides the user with several different views for monitoring<br />

data contained in the document at the same time. Normally, the size of each pane can be adjusted<br />

freely, this gives the user a better view of data. There are two types of splitter windows: Dynamic<br />

Splitter Window and Static Splitter Window. For a dynamic splitter window, all views within the splitter<br />

window are of the same type. The user can create new panes or remove old panes on the fly. For a static<br />

splitter window, the views could be of different types and the number of panes has to be fixed at the<br />

beginning. In this case, the user can not add or delete views after the program has started.<br />

Both SDI and MDI applications can have splitter windows. In an SDI application, the splitter window<br />

is embedded in the mainframe window. In an MDI application, it is embedded in the child frame window.<br />

3.1 Implementing Static Splitter Windows<br />

In <strong>MFC</strong>, class CSplitterWnd is used to implement window splitting. To split a window into several<br />

panes, we must first declare a CSplitterWnd type variable in the frame window class. One CSplitterWnd<br />

can divide window into M×N sub-panes. The splitter window can be nested, which means we can further<br />

split a single pane into several sub panes by using another CSplitterWnd type variable. So if we want to<br />

create an unevenly divided splitter window, we need to declare more than one CSplitterWnd type<br />

variables.<br />

For example, if we want to create a splitter window that has two rows, the first row has two columns<br />

and the second row has two columns, we need to first split the client area into a 1×2 splitter window, then<br />

split the first row into a 2×1 splitter window (Figure 3-1).<br />

Divide the window<br />

into row 0 and row 1<br />

Divide row 0 into column<br />

0 and column 1<br />

Figure 3-1. Create irregularly divided splitter window<br />

To create static splitter window, first we need to declare CSplitterWnd type variable(s) in the frame<br />

window class, then in frame window’s OnCreateClient(…) member function, call functions<br />

CSplitterWnd::CreateStatic(…) and CSplitterWnd::CreateView(…). Here, function CSplitterWnd::<br />

CreateStatic(…) is used to split the window into several panes and CSplitterWnd::CreateView(…) is<br />

used to attach a view to each pane.<br />

57

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!