Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
May 2, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

MFC / Event Driven-Programming

I've written a program using MFC that uses toolbars. I have an event that I want to change the state of some of the toolbar buttons (disable them, to be exact), and I also want this event to cause an Object in my CDocment class to be drawn. The problem is, the toolbar is in CMainFrame. Is it possible to call a function in the CDocument class from CMainFrame, or vice versa, or is there something else I can do to cause one event to trigger two different actions?

You could write code to make this possible, but MFC wasn't designed for it and I recommend you stick with the MFC approach. The change you are asking for may not work if two views are open on the same document and may not port to a non-MDI application, if that ever became desirable.

Because the action occurs after the command is selected, it should be a simple matter to handle this within the view. Just pass along to the document whatever information it needs to perform document changes.

The best way to implement the state change to your toolbar may seem a little backwards, but is really fairly easy. Just put code in your view's OnUpdate event handler:

void CMyView::OnUpdateMyCommand(CCmdUI* pCmdUI) 
   pCmdUI->Enable(TRUE or FALSE);
MFC will call this handler after any user events, and this works fairly well. You can set a flag in your view to indicate what state this command should be, but it sounds like it may make more sense to query the document to determine this.

Then just call pCmdUI->Enable() with a TRUE or FALSE argument to change the enabled state of that toolbar. Note that this affects the particular command so it disables or enables the menu command as well, if you implemented one.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date