MFC / Event Driven-Programming

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.

See also  5 Tips for Working With an Onsite Interpreter

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.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist