Unable to Create Exit Button - c++

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).

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.

MFC radio button group message handler

I've got two little questions at Visual Studio MFC-GUI programming technique regarding the handling of a group of radio buttons in VS2015 CE.
I have a little dialog based application. This application draws some lines on my dialog with a specified pen.
Now I made a group of radio buttons to be able do use different colors for the pen. So I created a group box, placed the radio buttons into this group box, enabled the group property of the first radio button and checked the tab sequence so that all of the radio buttons are properly in sequence.
That's ok - testing the GUI I'm able to select just one of the radio button as planned, because I wouldn't be able to draw a line in to different colors at the same time. Now I added an handler for the BN_CLICKED Message of the first radio button object in the ClassWizard to add the selection of different colored pens.
Now comes the interesting part. This handler is executed only when i click the first radio button. The one with group property enabled. I thought it should be executed whenever I click any of the radio buttons in this group. The next thing I tried was to add an BN_CLICKED-Messagehandler to all of those radio buttons, but the ClassWizard does not shows the BN_CLICKED Message for the other radio buttons - only for the one with group proerty enabled.
After googling around I read a post that one would be able to add an BN_CLICKED-Messagehandler for an button by simply double clicking it in the Dialog. So I double clicked all of my radio buttons and added an invocation of the BN_CLICKED-Messagehandler of the first radio button. That's working as a workaround so that now my line is drawn in the different colors whenever I click on any of those radio buttons (black, blue, red , and so on).
I have two questions - is this behaviour intended? I suppose the reason why I'm only able to add a BN_CLICKED-Messagehandler in the ClassWizard for the first radio button is, that it would be invoked by any of the radio buttons in my group, wouldn't it?
Another question is, I would have preferred to write the handler once and be able to tell the class wizard to use this particular handler instead of creating a unique handler for each of the buttons. That's independent of this radio buttons. I tested this with a simple application which consists of an assembly of several buttons - which can have their own BN_CLICKED-Messagehandlers attached by the class wizard. When I didn't accept the automatically generated functionname from the wizard and enter the name of my own handlerfunction it simply states that it wouldn't be able to overwrite it. How can I select an existing handler for an object message in the class wizard or is that not possible and I have to do this manually in code for myself?
Hope there's an VS2015 MFC Guy out there be able to tell me ..
This is indeed by design. You could have wanted different behaviour for each button click - if you don;t simply associate all of the BN_CLICKED to the same message handler and delete the ones that were automatically generated.
You can also use ON_CONTROL_RANGE(BN_CLICKED, id1, id2, memberFxn ) - make sure your buttons are in the same range in your resource.h
Hope that helps!

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.

Embedding dialogs in main dialog and switching them with button click in MFC

I have a design like below:
So basically, I want to embed three dialogs in the application main dialog and switch between them, for each button click i.e., button 1 will show dialog one , button 2 will hide dialog 1 and show dialog 2 .. and so on.
Each dialog will be having a different design and functions.
I tried using CPropertySheet class to Add pages but its GUI is different. It has either option for navigating the dialogs using next / back button , or from a tab control.
None of which is as per my requirement.
So I want to know is it possible to have a design like this in MFC ? If yes how? Which Class/ control should I use.
Any help will be appreciated.
What you can do is use a normal CDialog class, add your buttons to it and also create a frame/rect as a placeholder for where your embedded dialogs are to appear. The following piece of code will create and position your embedded dialog.
CRect rect;
CWnd *pHost = GetDlgItem(ID_OF_YOUR_FRAME_RECT);
pHost->GetWindowRect(&rect);
ScreenToClient(&rect);
pDialog->Create(ID_OF_YOUR_DIALOG, this);
pDialog->MoveWindow(&rect);
pDialog->ShowWindow(SW_SHOW);
On button clicks, you hide the previously shown dialog (SW_HIDE) and show your selected dialog(SW_SHOW) with ShowWindow(...).
If you create your embedded dialogs with IDD_FORMVIEW style in the add resource editor it'll have the proper styles for embedding.
Another option is probably to use an embedded PropertySheet and hide the tab row and programatically change the tabs on the button clicks. I just find it to be too much fuzz with borders, positioning, validation and such for my liking.
If you have the MFC Feature Pack, that first came with VS2008 SP1 and is in all later versions, you might like to consider CMFCPropertySheet. There are a number of examples on the linked page, that are very similar to your design.
For example, this:
What worked for me just using dialog based application is SetParent() method. Dont know why nobody mentioned it. It seems to work fine.
I am doing like below:
VERIFY(pDlg1.Create(PanelDlg::IDD, this));
VERIFY(pDlg2.Create(PanelDlg2::IDD, this));
VERIFY(pDlg3.Create(PanelDlg2::IDD, this));
::SetParent(pDlg1.GetSafeHwnd(), this->m_hWnd);
::SetParent(pDlg2.GetSafeHwnd(), this->m_hWnd);
::SetParent(pDlg3.GetSafeHwnd(), this->m_hWnd);
Now I can show or hide a child dialog at will (button clicks) as below:
pDlg1.ShowWindow(SW_SHOW);
pDlg2.ShowWindow(SW_HIDE);
pDlg3.ShowWindow(SW_HIDE);

Access visual components text, buttons in a Internet Explorer_TridentDlgFrame dialog

I have a C++ app that makes use of a Webbrowser component. When a certain URL is open I get an standard Script Error dialog. I inspected the dialog and found out its type is *Internet Explorer_TridentDlgFrame*.
What I need to do is to close that dialog window. I already added a Window Hook and on the
WM_SHOWWINDOW I compare the title of the window and close it by sending a WM_CLOSE message.
The problem with that approach is that I cannot distinguish this window from other windows with the same title. So, What I'd need to do is to be able to access the text and the buttons in the
*Internet Explorer_TridentDlgFrame* dialog, in order to filter using its text and then click its Yes button.
I know how to get the child components from standard dialogs but I do not how to do it with this *Internet Explorer_TridentDlgFrame* dialog type. When I inspect it with WinSpy I can see that all the visual elements inside this dialog are encapsulated in *Internet Explorer_Server* element.
Does someone know how to access the visual components on the *Internet Explorer_TridentDlgFrame* dialog?
Thanks in advance for your help!