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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.