The command target can be either some hidden window in your project or even the toolbar itself. Let’s assume you have some ID_YOUR_COMMAND
toolbar button or menu item. The command target window should have the following two methods and message map entries:
1. Command handler method, invoked when the ID_YOUR_COMMAND
toolbar button or menu item is clicked:
afx_msg void OnYourCommand();
ON_COMMAND( ID_YOUR_COMMAND, OnYourCommand )
void CYourCommandTargetWnd::OnYourCommand()
{
// PUT YOUR COMMAND HANDLER CODE HERE
}
2. Command updating method, which modifies the state of the
ID_YOUR_COMMAND
toolbar button or menu item:
afx_msg void OnUpdateYourCommand( CCmdUI * pCmdUI );
ON_UPDATE_COMMAND_UI( ID_YOUR_COMMAND, OnUpdateYourCommand )
void CYourCommandTargetWnd::OnUpdateYourCommand( CCmdUI * pCmdUI )
{
// TO ENABLE OR DISABLE THE COMMAND ID_YOUR_COMMAND
pCmdUI->Enable( bEnable );
// TO SET/REMOVE CHECK MARK
pCmdUI->SetCheck( bChecked );
// TO SET/REMOVE RADIO MARK
pCmdUI->SetRadio( bChecked );
}
The last method is optional. If the first method is defined, then the
ID_YOUR_COMMAND
toolbar button or menu item becomes enabled. A command which has no handler method automatically gets disabled.
Menu item commands invoke the command updating mechanism just before the menu is about to appear on the screen. Toolbar commands invoke the command updating mechanism automatically in MFC/EXE applications where the application’s message loop is controlled by the MFC host (which is typically a global variable called
theApp
of a
CWinThread
-derived class type). Your IE extension module does not control the message loop of IE instance. This means you need to invoke the command updating mechanism for the toolbar manually. You may use the following code to forcibly update all the toolbar buttons:
CWnd * pCommandTargetWnd = pToolBar->GetOwner();
pToolBar->OnUpdateCmdUI( pCommandTargetWnd, TRUE );
It is possible to emulate the command updating mechanism in your IE extension module and make all the MFC objects working exactly as they work in MFC/EXE projects. Just set a timer for some window (for instance, for your toolbar) using the 100 millisecond timer’s period and invoke the following code from the
WM_TIMER
handler method:
CWinThread * pWinThread = ::AfxGetThread();
ASSERT_VALID( pWinThread );
pWinThread->OnIdle( 0 );
In your IE extension module, it is not possible to use the MFC’s message pretranslation based on the
CWnd::PreTranslateMessage()
virtual method and route invocations of this method. But you can emulate this mechanism with hooks.