mfc - How do I handle toolbar messages in a dialog box


Question: 

I am writing a MFC MDI application, which I started with the wizard, with visual studio 2017. In this application I have dialog boxes with toolbars. But when the dialog boxes are shown, the toolbar buttons are not enabled, even though the dialog classes contain handlers for the button IDs.

For instance, I Created 1 test class for a dialog named CDlg with toolbar with a button with ID ID_DIALOG_1. I have the corresponding message map entry and the corresponding handler function inside CDlg:

BEGIN_MESSAGE_MAP(CDlg, CDialogEx)
    ON_COMMAND(ID_DIALOG_1, &CDlg::OnDialog1)
END_MESSAGE_MAP()

void CDlg::ODialog1()
{
    // TODO: Add your command handler code here
}

But the toolbar button is disabled.

I experimented putting the handler both inside the document class and CDlg, and in this case the button is enabled and the CDlg::OnDialog1 handler gets called. I also experimented to put a menu on the CDlg class with the ID ID_DIALOG_1 and in this case CDlg::OnDialog1 gets called when the menu item is clicked but the toolbar button remains disabled.

So, Is it possible to have the command handler for the button ID ID_DIALOG_1 inside class CDlg for the toolbar?




1 Answer: 

There was a bug in my code. I was calling CDialogEx::OnInitDialog() after the creation and loading of toolbar, but actually CDialogEx::OnInitDialog() should be called first according to link.

 

More Articles


javascript - get a smooth animation for a canvas game

How to get a better animation, dinamically, even when browser is busy or idle, for different devices which have different hardware capacity.I have tried many ways and still cannot find the right way to make the game to display a better animation.This is what i tried:var now;var then = Date.now();var

android - How to create bottomsheet with toolbar when expanded,

I want to create bottom sheet layout, which when expanded to full screen should display toolbar. I have used following code but it is displaying toolbar even if its not expanded to full screen.<?xml version="1.0" encoding="utf-8"?><android.support.design.widget.CoordinatorLayout xmlns:an

python - Mayavi: Customize toolbar

Is there a way to customize the default toolbar of a mayavi scene? I would like to delete some buttons as I don't need them (e.g. the save button). Here you can see which toolbar I am talking about:The code is just an example code:import osos.environ['ETS_TOOLKIT'] = 'qt4'from pyface.qt import QtGui


Keras error when finetuning InceptionV3

I am trying to follow the "Fine-tune InceptionV3 on a new set of classes" sample code to freeze the first 172 layers and re-train the last layers on cats/dogs dataset. I keep getting an error which I have noted at the bottom. Please help. I am using Ubuntu 16.04, keras 1.2.1, theano 0.9.0beta1.dev,

android - CollapsingToolbarLayout - Hide ImageView when Expanded, show when Collapsed

I would like to hide an ImageView (logo) when CollapsingToolbarLayout is expanded and only show the ImageView when the CollapsingToolbarLayout is collapsed. (I know how to do the reverse of this question). As you can see from the screenshot below. The big mast head is actually one big logo, image on

java - Android Adding Buttons to Toolbar Programmatically

I am trying to create a toolbar programmatically and Add Left and Right Bar Button to it without using XML.But the button is not getting aligned to right.So kindly help me.Toolbar TopTulBarVar = new Toolbar(this);TopTulBarVar.setId(View.generateViewId());TopTulBarVar.setBackgroundColor(Color.parseCo


xamarin.android - How to align UI controls on a Custom Toolbar in Android xml

I'm creating a layout in Xamarin.Android and I'm working on a custom Toolbar with an image button a TextView and another label. What I need to do is Make sure the imagebutton BackButton is left aligned all the way to the left of the parent Contain, the TextView is Center Aligned and the MenuButton

c++ - Windows Toolbar - Controlling button size and padding

I'm trying to understand the behaviour of a Windows toolbar - in particular how the following values interact:the size of the bitmap image usedthe effective size of a toolbar buttonthe padding between the image and the button edgethe height of the toolbarText displayed by a button is not relevant in

compact framework - Toolbar button sizes in windows mobile 6.5.3

We have a VB.net CF2.0 Apllication that up until now has run on windows mobile platforms from 2003 to 6.0 without any major issues. Our current team use HP iPAQ devices running winodws 6.0 and everything runs fine. There is a good, hardware related reson why we use these devices. We are looking to u

wpf - Giving a stackPanel the same LinearGradientBrush as the toolbar has by default

I have two ToolBars side by side (I had to do that so I could align buttons right and left)But now, I have to add some text that can be aligned right left or center between both toolbars. I added the text in a TextBlock inside a StackPanel disposed on a Grid at the second position (between the toolb