MFC / Event Driven-Programming

MFC / Event Driven-Programming

Question:
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?

Answer:
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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several