Add functions to the Main Menu at the top of a window - c++

I would like to add sub options to the Main Menu at the top of a main frame window in MFC.
For example; File>Open, or Edit>Undo.
Is this possible to do at all? My intention is to replace the function of some buttons in my program with options typically found in the drop down menus
Also after adding an item to the Main Menu how would you use it to call a function?

Here I have added an entry to the View menu called Test:
When I build and runt he program it shows disabled:
This is because I still have to create an event handler. In the resource editor you right-click the menu item and select Add Event handler:
This brings up the class wizard:
On the dialogue there are a couple of menu event handlers to choose from. Select the one you need (as in the screen shot) but don't forget to choose the right class on the right. Then click Add and Edit.
Now you can add your event handler functionality. Example:
void CMainFrame::OnViewTest()
{
AfxMessageBox(_T("Hello!"), MB_OK | MB_ICONINFORMATION);
}
When I compile and run this:
Hopefully this will help you get up and running.

Related

C++ MFC Button on a second Dialog does nothing

I created a C++ MFC Program with the Visual Studio Wizard. There I set the application type to "Dialog Based".
I have a button on the first dialog, which opens another dialog. I created this second dialog by right clicking on the project -> Add -> Resource -> Dialog -> New.
Then I added a MFC class to the new dialog by double clicking it in resource view.
On the second Dialog I also created a button. I double clicked the button and added some code which should be executed.
When I run the program and click the button on the second dialog, nothing happens and the code is not executed. I did everything like with the button on the first dialog. That one works fine. Why is the second button not working and what do I need to do?
First Dialog
void CMFCApplication1Dlg::OnBnClickedButton1()
{
CDialogEx secondDialog(IDD_DIALOG1);
secondDialog.DoModal();
}
Second Dialog
void SettingsDlg::OnBnClickedButton1()
{
MessageBox(L"Button clicked", L"Button clicked", MB_OK);
}
#andrew-truckle, your side node was the answer! I changed it to:
void CMFCApplication1Dlg::OnBnClickedButton1()
{
SettingsDlg settingsDialog;
settingsDialog.DoModal();
}
Now the button works just as expected. Thank you very much!
Further Info (from #andrew-truckle)
For the benefit of others the issue here was that the original code declared the dialog like this:
CDialogEx secondDialog(IDD_DIALOG1);
That was wrong because the dialog was actually associated with the class SettingsDlg. This is the class that had the message map and event handlers etc. CDialogEx was the base class.
I added this update to the answer to save the reader from locating my comment to the question.

Is there a way to stop a qmenu from being hidden when you click off of the menu

I have a QMenu. I would like to keep the menu up when you click outside menu. Is this possible. I have tried overriding the eventmethod but that does not work. I know how to keep the menu open when you click on an action (there are a few questions here about that) but I want to keep it up when you click outside the menu, clicking on an action should still close the menu. This should work when bringing up the menu via popup or exec. Is this possible using QMenu?
EDIT: showTearOffMenu is not what I want, I would like to keep these pop-up frameless
EDIT #2: Overriding closeEvent and ignoring it will keep the menu up but now it steals all clicks
void MenuSubclass::closeEvent(QCloseEvent* e){
e->ignore();
}

How to insert more dialogs in MFC Dialog application?

I am trying to build an MFC application Dialog based application. It runs ok. But I need to insert another Dialog. So how can I for example, pressing on a button from the first dialog to open the new added dialog?.
I am using Microsoft Visual Studio 2015.
I right clicked on the resources folder and insert a dialog.
It is inserted, but how to create it?.
Thank you.
The easiest way is: I consider you are creating a Dialog based application so you get a main Dialog box and an About Dialog box when Selecting menu->About.
To add Another Dialog to your application:
1- Right click on the solution explorer on the resources files and select Add->Resource->Dialog->New
You get a new Dialog right front of you. Right click on this Dialog and select Add Class. give it for example a name like "MyDlg2" and click ok.
You'll see two files added: MyDlg2.h and MyDlg2.cpp.
Now How to Popup this second dialog (MyDlg2)? Let's create a button on the main Dialog:
Drag a button onto Main Dialog.
Give it a caption "Gong to Dialog2..."
Double-click this button to add a handler for it.
In this handler enter:
MyDlg2 dlg;
dlg.DoModal();
Scroll to the top of this file and add:
#include "MyDlg2.h"
This is important so that main Dialog knows How to create dialog 2.
Build and run.
You need to derive a class from CDialog.
For more information check this MSDN example.

MFC menu item doesn't open dialog box

I am having a proglem with the MFC application and the DialogBox. I am quiet sure I've done everything well with this tutorial: https://msdn.microsoft.com/en-us/library/6wb9s9ah.aspx
but still it doesn't work...
1. I've created new project with simple menu commands.
2. I've created new menu item (+ID) and new resource DialogBox (+ID).
3. Then I've added a new class named CParameters with the Class Wizard. For the BaseClass I've typed in CDialog.
4. I've created new handler on the menu item and added the code
CParameters dlg;
dlg.DoModal();
I think this is it, and this should work... But it doesnt... What is missing??
Here is my project, you can access it freely:
https://www.dropbox.com/sh/e6ajoxqk76hkuvn/AACRMY8bgcuyXguFwP240QB9a?dl=0
Additionally I want to insert TextEditors and to change parameters in my program from the dialog box.
A scan of your source code reveals that you are trying to handle the menu item event within the class that is going to display the dialog.
void CParameters::OnParam()
{
// TODO: Add your command handler code here
CParameters dlg;
dlg.DoModal();
}
I don't see anywhere else that you actually instantiate the dialog class (I may have missed it). What you are trying is incorrect. You cannot handle the menu item event within the same class that displays the dialog because that class (CParameters) has not been instantiated, so, it cannot respond to the menu event. Typically, the menu event would be handled in the mainframe class.
If you are doing this by adding a new menu item from a simple SDI application, then trying adding that part of code in
CMainFrame::OnEdit
OnEdit method used here is obtained from the Event Handler for the new menu item and Message type being COMMAND.

Unable to Create Exit Button

I tried a tutorial about C and ATL about a basic dialog. It has a window, and 2 buttons inside. In the beginning of the tutorial, there are 2 buttons and they all exit the application. But, these 2 buttons are created by default. So, I tried creating another button that can exit the application, and I failed.
My aim is to use radio buttons with this project. I want to create some radio buttons and when I press a button, I want to execute some commands.
Here are the links - In my opinion, the tutorial link is unrelated, but I guess it won't hurt: Tutorial, Working Project, Problematic Project.
Thanks in advance.
You add a button onto dialog resource template
You associate an identifier with the button in properties pane right there in resource editor, e.g. IDC_MYBUTT
You will find #define for the chosen constant IDC_MYBUTT in resource.h file on the project
You add handlers to button events on your dialog class. Those are COMMAND_ID_HANDLER macros on Sample ATL Dialog Window code, which connect the event (underlying WM_COMMAND message sent to the window) with specific method (both IDOK and IDCANCEL buttons execute OnCommand in the sample code).
There on the handler you decide how to handle, and in particular to end the dialog or not.
COMMAND_HANDLER, COMMAND_ID_HANDLER and friends are described on MSDN: Message Map Macros (ATL).