How to minimize the page in the property sheet? - c++

I have used property sheet concept and display 3 dialog box on the property sheet.
Now to access the each dialog i have to clicked on the tab of each dialog.
But my need is that i want to minimize the first dialog and show the second dialog without using tab. for that i have added one button using that button i minimized the first dialog but when i tried to call the second dialog i get assertion failed error. i have done like this...
In the 1st dialog (.cpp) file :
void ClstdemoDlg::OnBnClickedMinimize()
{
ClstdemoDlg_1 dlg; // created 2nd dialog variable.
this->ShowWindow(SW_MINIMIZE); // minimize the 1st dialog
dlg.ShowWindow(SW_SHOW); //Tried to show 2nd dialog but get Assertion failed Error
}
Please let me know how to do this?

Related

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.

C++ MFC create new dialog and add Combobox item

I'm using MFC to build an application with two dialogs.
When I press a button in the parent dialog a new window including a Combobox should apprear.
I created a first dialog with a button "New". This button will open the second dialog.
Therefor I created a second dialog with a Combobox. The Combobox has a linked variable variableCombobox. The second class is called CSecond.
Before I do anything in the new dialog I want to add an item to the Combobox.
In the first dialog class I create the new window like this:
void CFirstDlg::OnBnClickedNew()
{
CSecond dlg2 = new CSecond();
dlg2.variableCombobox.AddString(L"test");
dlg2.DoModal();
}
The program crashes in the line I want to add the test string to the Combobox showing an assertion error.
I noticed that the dlg2 object is null but I don't know why.
Can anyone tell me how to create a second window immediately adding an new item in the Combobox of the second window?
The problem is the second dialog is a modal dialog. The windows does not exist before the call to DoModal() and no longer exist after that function returns. Therefore calling AddString on the combobox is not all right, since the combo box does not exist at that time.
The solution is to initialize the dialog with your desired values (like in the constructor for instance, or other methods) and then in OnInitDialog() use those values to setup the controls (including this call to AddString for the combobox).

System Tray Menu error MFC

I am Making an Application which will make a dialog as system tray icon after some button press. it is working fine but also i need to open the menu same as dialog contains on Right Click and have written following Code:
CMenu pMenu;
pMenu.LoadMenu(IDR_MENU1);
POINT pointCursor;
::GetCursorPos( &pointCursor );
pMenu.TrackPopupMenu(TPM_BOTTOMALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON, pointCursor.x, pointCursor.y, this);
It is Creating the menu but the width of the menu is very thin as shown following:(Yellow highlighted area is menu)
if I add only first submenu of main menu then it works well as following code:
CMenu *pMenu = GetMenu();
POINT pointCursor;
CMenu *pMenu = GetMenu();
::GetCursorPos( &pointCursor );
pMenu->GetSubMenu(0)->TrackPopupMenu(TPM_BOTTOMALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pointCursor.x,pointCursor.y,this);
On applying this code i get following output
Actually I need the Following menu:
Kindly Suggest Where i am doing it wrong
TrackPopupMenu cannot display the menu bar as part of it's work. The menu bar itself is not displayed because TrackPopupMenu requires a handle to a menu, submenu, or shortcut menu. So, given that, if you really want the same menu structure including the menu bar you've shown, you'll need to create that structure dynamically using CMenu methods. Or, simply create a new menu resource with a different name that includes the menu bar items as submenus.

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.

CMFCPropertyGridCtrl last item not drawn?

I have a CMFCPropertyGridCtrl that I'm using in an options dialog box. I have a method in my options dialog class called InitPropertyGrid(). This method clears any properties and populates the CMFCPropertyGrid objects (using a custom Settings object for the property values) and appends them to the grid.
When I open my dialog box the first time all the properties show correctly. However, if I then close my dialog box and reopen it, the very last property is not drawn on the screen. All other properties are drawn normally:
First time:
All subsequent times:
As you can see, the plus/minus icon is showing minus in both cases to indicated the section is expanded. When the last item is not showing, clicking on the +/- icon once to contract and once to expand causes the last item to be correctly shown.
Note when I close the dialog box, I do not destroy it but just reshow it. However immediately before calling ShowWindow on the dialog I call the InitPropertyGrid() (called by UpdateToCurrentSettings) method.
if(optionsDialog_ == NULL)
{
optionsDialog_ = new OptionsDialog(settings_, this);
optionsDialog_->Create(OptionsDialog::IDD, this);
}
optionsDialog_->UpdateToCurrentSettings();
optionsDialog_->ShowWindow(SW_SHOW);
I found I can eliminate this problem simply by calling myPropertyGrid.ExpandAll(TRUE) at the end of the code where I initialize the property grid (InitPropertyGrid() for me). This seems to force all the properties to expand.