open a new dialog in the existing frame.

an easy question but i cant find how to do it anywhere, imust be searching for the wrong thing.

basically i have an mfc app with a button on it when its click i want to open that window in the existing window rather than opening a new one using the following code.

CAddDlg addDlg = new CAddDlg;
long ret = addDlg->DoModal();

how can i do this please??
flynnyAsked:
Who is Participating?
 
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> C++ supports multiple inheritances.
C++ yes but MFC no.

MFC makes a cast on the CWnd pointer when sending messages to derived classes. That cast will fail in case of multiple inheritance and a derived class having a different (virtual) pointer than the CWnd baseclass. Multiple inheritance only works with MFC if all MFC classes are in the main branch of the inheritance tree, i. e. the MFC class is always the first class you are deriving from.

>>>> My dialogs already extend CSizingDialog
You might consider deriving CSizingDialog from CPropertyPage. CPropertyPage is a CDialog with a few members to support paging within a CPropertySheet.

If you need CSizingDialog somewhere outside of a CPropertySheet you may try to use it like that. I can't think of really essential reasons why you shouldn't be able to use it as single dialog. But if you experience some assertions or unresolved constructors you may consider a class definition like that:

class CSizingDialog
#ifndef WITH_PROPERTYSHEET
        : public CDialog
#else
        : public CPropertyPage
#endif
{
         ....
};

Regards, Alex
0
 
amitrsharmaCommented:
when its click i want to open that window in the existing window rather than opening a new one using the following code.
can u please explain more on this. what do u mean by that...

0
 
lucky_jamesCommented:
The problem is how to show one window instead of two after the click. Two ways:
1. Use addDlg->Create method. Set parent value to 'this'.
2. Before DoMOdal() call, set the current window invisible ( use this->ShowWindow(FALSE);)

Let me know if you have any doubt.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
flynnyAuthor Commented:
hi james, ok easiest way seems to be seting the parent how would i do this?
0
 
jkrCommented:
If that is the window you are hooking (and I assume it is), you need to create a CWnd instance from the handle using

CWnd* pWnd = CWnd::FromHandlePermanent(hWndMain);
CAddDlg addDlg = new CAddDlg(IDR_ADDDLG,pWnd); // set parent
0
 
lucky_jamesCommented:
In my opinion the simplest was the other one. :)
All you need to do is to put this code :

this->ShowWindow(SW_HIDE);
dlg->doModal();
this->ShowWindow(SW_SHOW);

if you want to implement the create method:

virtual BOOL Create(
   CWnd* pParentWnd,
   UINT nIDTemplate,
   UINT nStyle,
   UINT nID
);

here in place of parent, you need to pass 'this',
templateID is the resourceID. ( you can get it from the .h file of the dialog class. it says IDD=resourceid)
0
 
lucky_jamesCommented:
I would suggest you to implement the first one:
this->ShowWindow(SW_HIDE);
dlg->doModal();
this->ShowWindow(SW_SHOW);

least code changes and easy to implement.
0
 
lucky_jamesCommented:
let me know if you have any doubt.
0
 
itsmeandnobodyelseCommented:
>>>> i want to open that window in the existing window rather than opening a new one

Hmmm, do you mean you want to exchange all current controls (including the button where you clicked on) by the controls of the new dialog? If so, you should be aware that this is a unusual behavior for a  MFC app. Assume the user made a double-click instead of a single-click. Then the second click may be recognized by the second dialog (assume there is a button as well).

Normally multiple dialogs are managed in a CPropertySheet, each of the dialogs has CPropertySheet as baseclass (not CDialog). Then the CPropertySheet handles switching between dialog pages what is a much more usual approach.

Regards, Alex
0
 
lucky_jamesCommented:
If you have already prepared functionality and the issue is just this one scenario....between these two dialogs then that code change can be more cost-time effective.

but if you are in mid way of development and have several dialogs to be shown one after another, you can make use of CPropertySheet.
For More info on CPropertySheet check out:
http://www.dotnetheaven.com/Uploadfile/bulentozkir/pp_wizard04212005064904AM/pp_wizard.aspx

Above link shows cpropertysheet usage (dont go in name as its in dotnetheaven !!). Let me know if you have any issues.
0
 
lucky_jamesCommented:
let me know if you have any doubts
0
 
flynnyAuthor Commented:
thanks for the comments guys.

>> If that is the window you are hooking (and I assume it is)
it is related to the program yes but this is a simply mfc app i have created to test my hooks, (similar to spy++ but more specific), here i have a window with some info in, however rather than open a new window when the use clicks a button which diapys more details, i want to load a seperate dialog.

CPropertyPage looks to be perfect. a couple of questions though. My dialogs already extend CSizingDialog (an extended class i downloaded to handle resizing and repositioning windows). Just to check before i break everything, (using th provided link), i assue  will have to change my CSizingDialog to extend CPropertyPage rather than CDialog (coudl this have any effect on the program?) .
0
 
lucky_jamesCommented:
No it should not affect. C++ supports multiple inheritances. Inherit your dialog from both CSizingdlg and CPropertyPage.

In order to bind these pages with one another (so that one will come after other, you can term it as sequencing), you also need to create a cpropertysheet which would be a sort of wizard with each dialog as its page.

OR
If you have only 2 dialogs to play with then a quick solution would be to switch dialogs' visibility accordingly. I have already stated it as well in my previous comments.
0
 
flynnyAuthor Commented:
ok great i've worked through changing the CDialog to CPropertyPage no i get the error

error C2664: '__thiscall CPropertyPage::CPropertyPage(unsigned int,unsigned int)' : cannot convert parameter 2 from 'class CWnd *' to 'uns
igned int'

where i  have

CSizingDialog::CSizingDialog(
      UINT nIDTemplate, CWnd* pParentWnd /*= NULL*/,
      LPCTSTR lpszSection /*= _T("Placement")*/,
      LPCTSTR lpszEntry /*= _T("MainDialog")*/)
      : CPropertyPage(nIDTemplate, pParentWnd)
      , m_lpszSection(lpszSection)
      , m_lpszEntry(lpszEntry)
      , m_bEnableRWP(TRUE)
      , m_bErase(TRUE)
{
}

any ideas?

thanks matt.
0
 
lucky_jamesCommented:
you should pass 'this' (any pointer of type CWnd*) for pParentWnd parameter.
Plz tell what value you have passed for this parameter.
0
 
flynnyAuthor Commented:
//////////////////////////////////////////////////////////////////////////
// SizingDialog.h

#pragma once

#include "SizingSP.h"


//////////////////////////////////////////////////////////////////////////
// CSizingDialog dialog

class CSizingDialog : public CPropertyPage
{
      DECLARE_SIZING_SUPPORT()

// Constructors
public:
      CSizingDialog(LPCTSTR lpszSection = NULL, LPCTSTR lpszEntry = NULL);
      explicit CSizingDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL,
            LPCTSTR lpszSection = _T("Placement"), LPCTSTR lpszEntry = _T("MainDialog"));

// Implementation
protected:
      afx_msg void OnPaint();
      afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
      afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
      afx_msg void OnSize(UINT nType, int cx, int cy);
      afx_msg BOOL OnEraseBkgnd(CDC* pDC);
      afx_msg void OnTimer(UINT nIDEvent);
      afx_msg void OnDestroy();
      afx_msg UINT OnNcHitTest(CPoint point);
      DECLARE_MESSAGE_MAP()

// Data
private:
      LPCTSTR                  m_lpszSection;
      LPCTSTR                  m_lpszEntry;
      BOOL                  m_bEnableRWP;            // Enable to restore the window placement
      BOOL                  m_bErase;                  // Enable erase background
};

class CSizingFormView : public CFormView
{
};
this is the header file if it helps.
0
 
lucky_jamesCommented:
I feel the pointer is not getting casted properly to CWnd type
The problem should be where you are creating an object of CSizingDialog.
The value you are passing in to the constructor are not going fine.
Plz let me know of those statement(s) where you ar creating an object of CSizingDialog class.
0
 
itsmeandnobodyelseCommented:
The constructor of CPropertyPage expects two id numbers, first is the dialog id (what is correct), second is a caption id for the text to display at the tab (button). The parent window is not an argument (as it is with CDialog) cause a CPropertyPage expects to be a child of a CPropertySheet being added by a call to CPropertySheet::AddPage. That member would set the parent window of the CDialog (it takes the 'this' of the CPropertySheet). If you need the CSizingDlg a single dialog you may change the constructor like that:

CSizingDialog::CSizingDialog(
      UINT nIDTemplate, CWnd* pParentWnd /*= NULL*/,
      LPCTSTR lpszSection /*= _T("Placement")*/,
      LPCTSTR lpszEntry /*= _T("MainDialog")*/)
      : CPropertyPage(nIDTemplate, 0)
      , m_lpszSection(lpszSection)
      , m_lpszEntry(lpszEntry)
      , m_bEnableRWP(TRUE)
      , m_bErase(TRUE)
{
     if (pParentWnd != NULL)
         SetParent(pParentWnd);
}

In case of a property sheet you would construct it without supplying a parent window. In case of a single dialog you may pass the parent window (or NULL) if it is the top level dialog. However, I've seen some sequences in MFC dlgprop.cpp where the window retrieved with GetParent() was 'down_casted' to a CPropertySheet, so I wouldn't bet that the single page mode will work.
0
 
itsmeandnobodyelseCommented:
>>>> I wouldn't bet that the single page mode will work.
It looks not so bad: It seems there are only 2 down-casts of the parent pointer. One, when the page was made active. If the down-cast failed, it would call OnSetActive what makes a UpdateData(FALSE), i. e. it fills the screen from members, what seems ok. Second, is in the CPropertyPage::EndDialog where failing of the down-cast would prevent from calling pParent->EndDialog(nID); what means that the dialog wouldn't end. But EndDialog never was called from members of CPropertyPage, so it is only an issue if you call EndDialog from OnOK or OnCancel. If so (or do it for any case), you should provide a CSizingDialog::EndDialog what would call CDialog::EndDialog in single page mode and CPropertyPage::EndDialog else. To make that easy you might add a bool member which tells what mode you in:

   void CSizingDialog::EndDialog(int id)
   {
          if (m_bSingleMode)
                CDialog::EndDialog(id);
         else
                CPropertyPage::EndDialog(id);
   }
0
 
flynnyAuthor Commented:
ok thanks for all your help itsme its compiling now. (i ran it to see what happened and it works however the close window button in the top right of the window doesn't work, i assume this is to do with your second point itsme).

ok so by deriving this class from CPropertyPage i then when initialising my dialog need to create a CProperttSheet, create an instance of the two dialogs i want to add and add them. is this correct?

I do this by

CPropertySheet dlg("Test Title", this, 0);

however it says it cannot convert parameter 1 from char[11] to unsigned int. from looking on the msdn library this seems correct though.

0
 
lucky_jamesCommented:
>>however it says it cannot convert parameter 1 from char[11] to unsigned int. from >>looking on the msdn library this seems correct though.

thats becoz it expects you to say a pointer instead of the char array.

try _T("Test Title") instead of "Test Title".

Hope this helps.
0
 
flynnyAuthor Commented:
sorry james, i tried this too and i just get the same error.
0
 
lucky_jamesCommented:
okies....
it may not able to convert it into a pointer.

try using LPTSTR.

for more info on LPTSTR:
http://www.experts-exchange.com/Programming/Languages/C/Q_21153785.html

and on LPCTSTR:
http://www.codeguru.com/forum/archive/index.php/t-78509.html
0
 
lucky_jamesCommented:
do revert back in case if it still doesnt work.
0
 
itsmeandnobodyelseCommented:
>>>> CPropertySheet dlg("Test Title", this, 0);

parameter 1 is "Test Title". That is a 'const char*' or LPCSTR.

CPropertySheet has two constructors, only differing in parameter 1.

The first takes a UINT (unsigned int) for an id of a text caption. That is why you get the error message "cannot convert parameter 1 from char[11] to unsigned int" cause if the first argument in the call cannot be recognized, the error message refres to the first constructor only.

The second constructor takes a  LPCTSTR what is a 'const char*' or LPCSTR if _UNICODE is not defined or a 'const wchar_t*' or LPCWSTR in case _UNICODE is defined for your project.

I would assume - same as lucky_james - that you have _UNICODE defined cause otherwise the second constructor should have compiled as "Test Title" would have been a valid ANSI string.

If we were right, the following should compile:

   CPropertySheet dlg(_T("Test Title"), this, 0);

cause the _T macro makes it a wide string in case of UNICODE.

Regards, Alex
0
 
itsmeandnobodyelseCommented:
You also can add a string resource in your resource view and call the constructor like

CPropertySheet dlg(IDS_PROPERTYSHEET_TITLE, this, 1);

That should resolve the problem.

>>>> however the close window button in the top right of the window doesn't work
Add the

      ON_WM_CLOSE()

macro to the message map entries of your dialog (between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP) and add the member function

         afx_msg void OnClose( );

to your dialog class and implement it like

 afx_msg void CSizingDialog::OnClose( )
 {
       EndDialog(IDOK);
 }

Note, you also have to add the CSizingDialog::EndDialog like I posted above.


 

0
 
flynnyAuthor Commented:
hi ok the resource has worke thanks itsme.

now i get the following for the second parameter

error C2664: '__thiscall CPropertySheet::CPropertySheet(unsigned int,class CWnd *,unsigned int)' : cannot convert parameter 2 from
'class CConfigApp *const ' to 'class CWnd *'
0
 
flynnyAuthor Commented:
ok i'm getting there now (sorry the CConfigApp extence CWinApp) so by using the following i have it compiled and loading

      CPropertySheet dlg(IDS_PROPERTYSHEET_TITLE,m_pActiveWnd,0);

      CConfigDlg page1;
      CButtonDlg page2;

      dlg.AddPage(&page1);
      dlg.AddPage(&page2);

      dlg.DoModal();

ok however, i'm doing something slightly wrong as the program i loading ok however i cannot resize the main wndow as i could before. however the property page insid the tab window i can (to a certain extent as i cannot resize the main window. how would i change this?

do i need to CSizingDialog extend propertysheet rather than page?
0
 
itsmeandnobodyelseCommented:
>>>> CPropertySheet dlg(IDS_PROPERTYSHEET_TITLE,m_pActiveWnd,0);

try

    CPropertySheet dlg(IDS_PROPERTYSHEET_TITLE, AfxGetMainWnd(), 0);

and tell whether it solves your problem. If not, you may explain what kind of windows and frames you currently have.
0
 
flynnyAuthor Commented:
no sorry itsme that didnt work it doing the same thing.

ok, let me run through this for you, i run the program which is running in a CWinApp

i create the Property Sheet as above and add each dialog, each of which extends the CSizingDialog, which in turn extend the PropertyPage class.

So i assume i will have to create an extension of the propertysheet class that will include the resizing function?
0
 
flynnyAuthor Commented:
the other thing i have noticed is that now i am using a CPropertySheet my file menu has disappeared? am i able to add this to a CPropertySheet?
0
 
itsmeandnobodyelseCommented:
>>>> the other thing i have noticed is that now i am using a
>>>> CPropertySheet my file menu has disappeared? am i
>>>> able to add this to a CPropertySheet?

All functionality like menus, handling sizing messages now have to been made by CPropertySheet rather than by one of the current dialog pages. You may derive from CPropertySheet to add the menu and the appropriate handlers.

Regards, Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.