Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a CDialog from other CDialog 1

Status
Not open for further replies.

mvillara

Programmer
May 18, 2002
24
0
0
ES
Can I call within a CDialog to another Cdialog and so on without exiting?
 
Of course. But only one dialog will be enabled, if You do not override DoModal() to enable another Dialogs. You should allways think, which Window must be parent for Your Dialog (deafult CDialog() constructor uses window from which You start the dialog).
 
Thanks tchouch.
Well, I have got an Application based on Dialogs, and my first Window is precisely a CDialog. The code is the next:

CMyDialog1 dlg1;
m_pMainWnd = &dlg1;
dlg1.DoModal();

At this point, this first dialog appear being my main window. From this dialog I have some buttons that once pushed, link with others Dialogs windows where I need to get or show some information. So, I do not know how I can open a new Dialog Window within my main Dialog window.

I have tried the next code:

CSecondDialog dlg2;
dlg2.DoModal();

and it doesnt work. Whats your suggest?
 
Derive Your dialog from new class, for example CMyDialog, which has only one function DoModal() overrided. The best way, copy this function from CDialog source code. In this code You will find next lines:

// disable parent (before creating dialog)
HWND hWndParent = PreModal();
AfxUnhookWindowCreate();
BOOL bEnableParent = FALSE;
if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
{
::EnableWindow(hWndParent, FALSE);
bEnableParent = TRUE;
}

Here You can enable (or do not disable) all windows/dialogs You need. For example so:

int CMyDialog::DoModal()
{
//old code
...
// enable parent (before creating dialog)
HWND hWndParent = PreModal();
AfxUnhookWindowCreate();
BOOL bEnableParent = FALSE;
if (hWndParent != NULL && !::IsWindowEnabled(hWndParent))
{
::EnableWindow(hWndParent, TRUE);
}
//You can enable all windows You need here
...
//old code
...
return m_nModalResult;
}
 
How can I get CDialog::DoModal() source code to override the mine?

A simple comment is that the bEnableParent variable is declared, inicializated but not used. Whay should I do with it?

Another thing that I havent mentioned is that I am programming for the Windows CE 3.0 and the function AfxUnhookWindowCreate() doesnt appear. Do you know if there is any other equivalent?

Please if you had a complete source code example about this topic, could send it to me?
Thanks.
 
The source is in File dlgcore.cpp (You must have MFC - libraries installed to have this file).
Original function DoModal() from dlgcore.cpp:
int CDialog::DoModal()
{
// can be constructed with a resource template or InitModalIndirect
ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
m_lpDialogTemplate != NULL);

// load resource as necessary
LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
HGLOBAL hDialogTemplate = m_hDialogTemplate;
HINSTANCE hInst = AfxGetResourceHandle();
if (m_lpszTemplateName != NULL)
{
hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
hDialogTemplate = LoadResource(hInst, hResource);
}
if (hDialogTemplate != NULL)
lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

// return -1 in case of failure to load the dialog template resource
if (lpDialogTemplate == NULL)
return -1;

// disable parent (before creating dialog)
HWND hWndParent = PreModal();
AfxUnhookWindowCreate();
BOOL bEnableParent = FALSE;
if (hWndParent != NULL && ::IsWindowEnabled(hWndParent))
{
::EnableWindow(hWndParent, FALSE);
bEnableParent = TRUE;
}

TRY
{
// create modeless dialog
AfxHookWindowCreate(this);
if (CreateDlgIndirect(lpDialogTemplate,
CWnd::FromHandle(hWndParent), hInst))
{
if (m_nFlags & WF_CONTINUEMODAL)
{
// enter modal loop
DWORD dwFlags = MLF_SHOWONIDLE;
if (GetStyle() & DS_NOIDLEMSG)
dwFlags |= MLF_NOIDLEMSG;
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
}

// hide the window before enabling the parent, etc.
if (m_hWnd != NULL)
SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}
}
CATCH_ALL(e)
{
DELETE_EXCEPTION(e);
m_nModalResult = -1;
}
END_CATCH_ALL

if (bEnableParent)
::EnableWindow(hWndParent, TRUE);
if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
::SetActiveWindow(hWndParent);

// destroy modal window
DestroyWindow();
PostModal();

// unlock/free resources as necessary
if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
UnlockResource(hDialogTemplate);
if (m_lpszTemplateName != NULL)
FreeResource(hDialogTemplate);

return m_nModalResult;
}
bEnableParent was in original source code. You can use it for disabling/enabling Your windows or comment it out.

AfxUnhookWindowCreate() You can try to comment out, if You can't use it on Your plattform.

It is not too easy at all. I have used it for an application, but I'm afraid, if I send You code I used, You will have more Questions. Please, You can see it:

int CShowDlg::DoModal()
{
// can be constructed with a resource template or InitModalIndirect
ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL ||
m_lpDialogTemplate != NULL);

// load resource as necessary
LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
HGLOBAL hDialogTemplate = m_hDialogTemplate;
HINSTANCE hInstSave = afxCurrentInstanceHandle;
// HINSTANCE hInst = AfxGetResourceHandle();
AfxSetResourceHandle(m_Hinst);
HINSTANCE hInst = AfxGetResourceHandle();
if (m_lpszTemplateName != NULL)
{
hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG);
HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG);
hDialogTemplate = LoadResource(hInst, hResource);
}
if (hDialogTemplate != NULL)
lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate);

// return -1 in case of failure to load the dialog template resource
if (lpDialogTemplate == NULL) {
afxCurrentInstanceHandle=hInstSave;
AfxSetResourceHandle(hInstSave);
return -1;
}

HWND hWndParent = PreModal();
AfxUnhookWindowCreate();
CWnd* pParentWnd = CWnd::FromHandle(hWndParent);
// BOOL bEnableParent = FALSE;
//Disable only main Window
if(AfxGetMainWnd()) {
if:):IsWindowEnabled( AfxGetMainWnd()->m_hWnd)) {
//Disable Main Window
::EnableWindow( AfxGetMainWnd()->m_hWnd, FALSE);
}

}
//Enable Parent
if (hWndParent != NULL /*&& ::IsWindowEnabled(hWndParent)*/)
{

::EnableWindow(hWndParent, TRUE);
// bEnableParent = TRUE;
}
TRY
{
// create modeless dialog
AfxHookWindowCreate(this);
m_pDialog = this;
if(AfxGetMainWnd())
((CMainFrame*)AfxGetMainWnd())->m_pShowDlg = m_pDialog;
if ( CreateDlgIndirect(lpDialogTemplate, CWnd::FromHandle(hWndParent), m_Hinst)

|| CreateDlgIndirect(lpDialogTemplate, CWnd::FromHandle(hWndParent), hInst)

|| CreateDlgIndirect(lpDialogTemplate, CWnd::FromHandle(hWndParent), hInstSave))
{
if (m_nFlags & WF_CONTINUEMODAL)
{
// enter modal loop
DWORD dwFlags = MLF_SHOWONIDLE;
if (GetStyle() & DS_NOIDLEMSG)
dwFlags |= MLF_NOIDLEMSG;
// ModifyStyle( WS_CHILDWINDOW | WS_DISABLED |WS_DLGFRAME ,0);
ModifyStyle( WS_CHILD | WS_CHILDWINDOW | WS_DISABLED |WS_DLGFRAME ,
WS_POPUPWINDOW|WS_CAPTION | WS_OVERLAPPED | DS_NOFAILCREATE|DS_NOIDLEMSG, SWP_NOSIZE );
ModifyStyleEx(WS_EX_TOOLWINDOW | WS_EX_MDICHILD |WS_EX_DLGMODALFRAME, WS_EX_NOPARENTNOTIFY , 0);
ShowWindow( SW_SHOWNORMAL);
ShowWindow( SW_HIDE);
if(AfxGetMainWnd()) {
RECT Rect;
AfxGetMainWnd()->GetUpdateRect( &Rect, FALSE );
AfxGetMainWnd()->RedrawWindow(&Rect);
}
ShowWindow( SW_SHOWNORMAL);
ShowOwnedPopups(TRUE );
EnableWindow( TRUE );
SetActiveWindow( );
//Restore Instance Handle
afxCurrentInstanceHandle=hInstSave;
AfxSetResourceHandle(hInstSave);
VERIFY(RunModalLoop(dwFlags) == m_nModalResult);
}
// hide the window before enabling the parent, etc.
if (m_hWnd != NULL) {
SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW|
SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER);
}
}
else {
m_nModalResult = -1;
}
}
CATCH_ALL(e)
{
delete e;
// DELETE_EXCEPTION(e);
m_nModalResult = -1;
}
END_CATCH_ALL

//Restore Instance Handle
afxCurrentInstanceHandle=hInstSave;
AfxSetResourceHandle(hInstSave);
// if (bEnableParent)
::EnableWindow(hWndParent, TRUE);
if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd)
::SetActiveWindow(hWndParent);
// destroy window
DestroyWindow();
PostModal();

// unlock/free resources as necessary
if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
UnlockResource(hDialogTemplate);
if (m_lpszTemplateName != NULL)
FreeResource(hDialogTemplate);
if(m_pDialog == this) {
m_pDialog = NULL;
if(AfxGetMainWnd())
((CMainFrame*)AfxGetMainWnd())->m_pShowDlg = NULL;
}
return m_nModalResult;
}
 
Ok, now it works perfectly.

Could you tell me who can I change the font, the background color of Label, CEdit,.... ?
How can differ in propoerties of this kind two differents Labels? I can not find each particular properties to change it. Maybe I am working on Windows CE.

In Visual Basic is easier the way of changing this properties.
 
I have seen some of such answers in this forum earlier, try to search them with keywords.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top