Dialog Boxes in MFC C++ - c++

I have 4 Dialog Boxes in 1 Project. Let's call them
IDD_DIALOG1
IDD_DIALOG2
IDD_DIALOG3
IDD_DIALOG4
When I compile my program the first window/dialog box I can see is IDD_DIALOG1, but I want to have IDD_DIALOG2 first.
My project do not have something like WinMain. It's clear MFC Application.

When you create your project you will have an application class too. In this class there will already be a default InitInstance method. For example:
BOOL CMFCApplication1App::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;
// Activate "Windows Native" visual manager for enabling themes in MFC controls
CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
CMFCApplication1Dlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
else if (nResponse == -1)
{
TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
}
// Delete the shell manager created above.
if (pShellManager != NULL)
{
delete pShellManager;
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
Part way down that method is this code:
CMFCApplication1Dlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
That is the main code you are interested in. So, if you want to start with a different dialogue, then #include the right header and change the code to the different dialogue class.
So in the above example, CMFCApplication1Dlg would be changed to something else, eg: CMyDialog2 (I do not know what the names of your dialogue classes are).

Related

Launch MFC window inside wrapped DLL

I'm trying to hook application, and launch it in my new MFC window. DLL (Wrapper) works very good, but I have problem with MFC... Full MFC code is inside "AST" - wrapper. Main application object has been created, but MFC window for this has not opened. I have control points (Message boxes) and thanks by this I know where code stop, this moment is inside InitInstance, in CCommandLineInfo -> ParseCommandLine and ProcessShellCommand. I don't know why, but at this moment AST have freeze, and i can't do anything.
BOOL CwelibApp::InitInstance(){
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinAppEx::InitInstance();
// Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}
AfxEnableControlContainer();
EnableTaskbarInteraction(FALSE);
// Place all significant initialization in InitInstance
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(4); // Load standard INI file options (including MRU)
InitContextMenuManager();
InitKeyboardManager();
InitTooltipManager();
CMFCToolTipInfo ttParams;
ttParams.m_bVislManagerTheme = TRUE;
weApplication.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CMFCApplication2Doc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CMFCApplication2View));
if (!pDocTemplate)
return FALSE;
Say::Box("Init MFC #1");
AddDocTemplate(pDocTemplate);
if(m_hInstance)
Say::Box("m_hInstance");
Say::Box("Init MFC #2");
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo); //Freeze...
Say::Box("Init MFC #3");
if (!ProcessShellCommand(cmdInfo))
return FALSE;
Say::Box("Init MFC #4");
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
Say::Box("Init MFC #5");
return TRUE;};
Where is problem? How I can open MFC application inside DLL, with using CFrameWndEx, CWinAppEx?
I have "Use FMC in a Static Library" in Project defaults.

How to bring my secondary dialog window to the top when the CDialog-based app starts?

I've coded an MFC CDialog based application. In normal circumstances it starts up by displaying a CDialog window from the InitInstance handler as such:
CMyDialog dlg;
INT_PTR nResponse = dlg.DoModal();
But for the first time this app runs I need to display another dialog from within CMyDialog::OnInitDialog before the main dialog is on the screen. So I do a similar thing:
CIntroDialog idlg(this);
idlg.DoModal();
But the issue with this approach is that my second CIntroDialog is not displayed in the foreground. So I attempted to fix this by calling the following from within CIntroDialog::OnInitDialog:
this->SetForegroundWindow();
this->BringWindowToTop();
but it didn't do anything.
I then tried calling ::AllowSetForegroundWindow(ASFW_ANY); from InitInstance for the app, and that didn't do anything either.
Any idea how to bring that second dialog to the foreground when the app starts?
PS. Due to the structure of this app, I need to call CIntroDialog::DoModal from within CMyDialog::OnInitDialog to prevent an extensive rewrite.
Have you consider making use of InitInstance for this in the app class?
BOOL CMyApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
CMyDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
I have cut some of the default implementation out, but you see this bit:
CMyDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
There is nothing stopping you doing something like:
CMyDlg2 dlg2;
if(dlg2.DoModal() == IDOK)
{
CMyDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
}
else
{
// Handle IDCANCEL
}
I admit I have not tested the above code, but I can't see why you can't execute the first dialogue and then the second dialogue.

Display Modal dialogs in MFC

I can't find any good tutorial on MFC, I am trying to display a dialog window.
I created a new resource, added my controls to it, it has inherited from CDialogEx, but I don't know where I should put the code to create and show the dialog window, I want it to be loaded when the application starts, can you give me hints?
The code should be in your application InitInstance() like so:
BOOL MyApp::InitInstance()
{
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
ExampleDlg dlg; // instance of dialog
m_pMainWnd = &dlg; // make dialog main window
INT_PTR nResponse = dlg.DoModal(); // get the response from your modal dialog
// this case, OK button, Cancel button or error in dialog
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
else if (nResponse == -1)
{
TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
}
This will give you a dialog when you startup your application and make it the main window. This can easily be done by using your MFC Application Wizard and selecting dialog base. It will automatically give you the layout of your application.
If you don't want to make it your main window just use:
ExampleDlg dlg;
dlg.DoModal();
And let your dialog code do the work.

DoModal Returns -1 Getlasterror says Invalid handle

I am using a Win32 application ,In this DoModal function returns -1 and GetlastError() returns 6(Invalid handle).I tried Deleting GDI handles to repair the GDI Exhausts,Result Fails.
Additional Information:
I'm using Visual Studio 2012 and this application is for Compact 2013.
if(!bDeviceOpened)
{
bDeviceOpened=OpenDriver();
if(bDeviceOpened == 0)
{
AfxMessageBox(_T("Please make sure the driver is up and runnning"));
return FALSE;
}
}
//Reading the Driver version
DWORD nBytesReturned = 0;
if(!GetOID(OID_RPS_DRIVER_STATS, &stats, sizeof(stats), &nBytesReturned) )
{
AfxMessageBox(_T("Failed to query the stats"));
}
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
Why I'm not getting the dialog box?
DoModal() returns -1 when your resource is not mapped correctly with dialog. If you step into DoModal() you will find statement
// return -1 in case of failure to load the dialog template resource
I would suggest you to call AfxSetResourceHandle(); function before DoModal().

C++ MFC : Debug Assertion error when trying to call method using instance of a class

I have this function in test2.cpp
void Ctest2App::message(){
MessageBox(0, L"And text here", L"MessageBox caption", MB_OK);
}
I call it from test2Dlg in the following way
void Ctest2Dlg::OnBnClickedButton1()
{
Ctest2App t;
t.message();
}
When I press the button I get a Debug Assertion error. why?
the test2.cpp file-
// test2.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "test2.h"
#include "test2Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Ctest2App
BEGIN_MESSAGE_MAP(Ctest2App, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
// Ctest2App construction
Ctest2App::Ctest2App()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
// The one and only Ctest2App object
Ctest2App theApp;
// Ctest2App initialization
BOOL Ctest2App::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
Ctest2Dlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Delete the shell manager created above.
if (pShellManager != NULL)
{
delete pShellManager;
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
void Ctest2App::message(){
MessageBox(0, L"And text here", L"MessageBox caption", MB_OK);
}
You have attempted to create a second Ctest2App object, but there can only be one in an app. MFC provides a global function to access the already-created app object:
AfxGetApp()->message();