How to insert more dialogs in MFC Dialog application? - c++

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.

Related

Add functions to the Main Menu at the top of a window

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.

How to call a dialog box from another (Win32 application)

I implemented a dialog-based Win32 Visual C++ application (Visual Studio Ultimate 2012) by following this article.
What is the way to call another dialog box (by clicking on a button) from the one I already created?
Add a button to the dialog in the dialog resource view. Just drag a button from the toolbar onto the dialog template. When the button is clicked you will get a WM_COMMAND message containing the button ID and the BN_CLICKED notification code.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb761825(v=vs.85).aspx
Add a case in your DialogProc to detect the click. When you get it, create a new dialog by calling the DialogBox API.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645452(v=vs.85).aspx
This second dialog will need you to write a new DialogProc2, just like the first DialogProc, to handle messages from the second dialog.

MFC dialog box continue after pressing ok

I have a custom dialog message box that pops-up when a edit control in my main dialog has wrong data.
CDlgError dlgError = new CDlgError(this);
dlgError.Create(CDlgError::IDD, this);
dlgError.m_staticMessage.SetWindowTextA("Error message!");
dlgError.ShowWindow(SW_SHOW);
//more code
I want the rest of the code to be executed only after i press an OK button in my CDlgError pop-up dialog. How can i do that?
Use DoModal instead of Create and ShowWindow to show your error dialog. e.g.
CDlgError dlgError = new CDlgError(this);
dlgError.m_strMessage = "Error message!";
dlgError.DoModal();
As you can see from the code you'll need to pass in the text and THEN set your message label inside CDlgError::OnInitDialog because the control won't be initialized before going modal.
You are creating a dialog using Create which shows a modalless dialog(you can click on other parts of application even dialog is open).
You requirement is for modal dialog where you can not click on any part of application until this dialog is closed.
To do this use DoModal function instead of create.

C++ MFC: How to open immediately a secondary dialog after the first modal dialog was created

how can I open a secondary modal dialog in C++ MFC from a dialog without pressing any button?
(If I create a dialog in OnInitDialog(), the first dialog won't appear.)
Just call ShowWindow(SW_SHOW); in your OnInitDialog just before display the secondary dialog.
Had the same problem. The reason was because of a problem in clear case which made the sln folder unreachable. Try close/stop ClearCase and open it again. Browse to the folder from clearcase and open the .sln.
Good Luck

regarding mfc dialog bar

I am new to MFC and VC++ programming. I have two questions:
How do I make a resizable dialog bar?
How do I give background color for a dockable dialog bar?
Thanks!
So i assume we are working in visual studios 2008 or similar and i assume you have an MFC SDI or MDI application that you are working on.
Open the resource viewer (View->resource view).
Expand the project that you would like to place the dialog in.
Expand to the dialog folder.
Right click this folder and click add resource.
Expand and add a new dialog bar.
Give it any properties you like using the properties window.
To (display/ give context) your dialog bar, instantiate and get the
handle of the dialog. Like
GetDlgItem(ID)-> ShowWindow(SW_SHOW);//show
Where ID is the id of the dialog. You can obtain this by going into the resource viewer, right click on the dialog, properties, and the ID is given in there.