How to handle dynamic menus in MFC? What happens when client calls CoCreateInstance? How to create a Modeless Dialog?
How to handle dynamic menus in MFC?MFC got built in function to handle dynamic menus. It comes after the message maps. By overriding OnCmdMsg() you can catch the menu events so you can handle them properly.
In your OnCmdMsg() handler, there are two cases to handle:
When the user selects an item.
When the data has to be updated (enabled/disabled, checked/unchecked).
Then menu’s can be handled by certain classes and header files such as :-
Cmenu, Createmenu, Insrtmenu and Appendmenu functions.
What happens when client calls CoCreateInstance?
When client calls the CoCreateInstance() function it actually used to create a single instance of the appropriate object.
The function looks like:
STDAPI CoCreateInstance(
REFCLSID rclsid, /*Class identifier (CLSID) of the object */
LPUNKNOWN pUnkOuter,
/*Pointer to whether object is or isn’t part of an aggregate */
DWORD dwClsContext,
/*Context for creating the object*/
REFIID riid,
/*Reference to the identifier of the interface (a pointer in C, a reference variable in C++*/
LPVOID * ppv
/*Indirect pointer to requested interface if successful, NULL otherwise*/)
This function accepts 4 input parameters and returns an output parameter which is the interface pointer of the COM component.
How to create a Modeless Dialog?For creation of Modeless Dialog doesn’t require OK and Cancel buttons, because they are associated with modal operations. User closes it by pressing the close box (a small x) in the upper-right corner of the dialog box. If you do decide to use OK and Cancel buttons, you must override the default OnOK() and OnCancel() message handlers and create your own that call DestroyWindow(). You must do this because the default functions call EndDialog(), which only closes a dialog box in modal operation.
Create() function is used to create the window.
void CDialogTemplDoc::OnShowdialogDisplaymodeless()
{
CModeless* pDlgModeless = new CModeless;
pDlgModeless->Create(CModeless::IDD);
}
Delete() function is used to delete the instance of the created window.
void CModeless::PostNcDestroy()
{
CDialog::PostNcDestroy();
// Delete Ourselves
delete this;
}
ShowWindow() function is used to show the window on the screen to the user dynamically.