1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Open Dialog when using BOOL OnInitDialog() function ?

Discussion in 'MFC' started by zippor, Nov 13, 2006.

  1. zippor

    zippor New Member

    Joined:
    Nov 13, 2006
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    BOOL OnInitDialog()
    {
    CListBox wndListBox;
    wndListBox.Attach (GetDlgItem (IDC_LIST1)->m_hWnd);
    wndListBox.AddString (_T ("One"));
    wndListBox.AddString (_T ("Two"));
    wndListBox.AddString (_T ("Three"));
    wndListBox.Detach ();
    return true;
    }

    When I use this code above in BOOL OnInitDialog() function of CDialog class, this code below can't execute. (IDC_LIST1 is a member of IDD_DLTEST1). Can you explain for me ? If I want to open IDD_DLTEST1 dialog, What do I have to do?

    void CallDialog()
    {
    Dlg d1(IDD_DLTEST1);
    d1.DoModal();
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    I could not get what you are asking.
     
  3. zippor

    zippor New Member

    Joined:
    Nov 13, 2006
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    When i used this code in CDialog class:
    Code:
    BOOL OnInitDialog()
    {
    CListBox wndListBox;
    wndListBox.Attach (GetDlgItem (IDC_LIST1)->m_hWnd);
    wndListBox.AddString (_T ("One"));
    wndListBox.AddString (_T ("Two"));
    wndListBox.AddString (_T ("Three"));
    wndListBox.Detach ();
    return true;
    }
    Why didn't the code below do? (class Dlg::public CDialog)
    Code:
    void CallDialog()
    {
    Dlg d1(IDD_DLTEST1);
    d1.DoModal();
    }
     
    Last edited by a moderator: Nov 15, 2006
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Because you are not calling that function.
     
  5. zippor

    zippor New Member

    Joined:
    Nov 13, 2006
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    This is my code. Help me
    Code:
    #include<afxwin.h>
    #include<afxext.h>
    #include"resource.h"
    //////////////////////////////////////////////////////////////////////////////////////////////
    class  CD : public CDocument
    {
    	DECLARE_DYNCREATE( CD )
    
    public:
    	CD():CDocument()
    	{ 
    	}
    };
    IMPLEMENT_DYNCREATE( CD, CDocument )
    //////////////////////////////////////////////////////////////////////////////////////////////
    class  CV : public CView
    {
    	
    	DECLARE_DYNCREATE( CV )
    
    public:
    	
    	void OnDraw(CDC* pDC)
    	{
    	}
    };
    IMPLEMENT_DYNCREATE( CV, CView )
    class Dlg : public CDialog
    {
    public:
    	Dlg(UINT  id):CDialog(id){}
    	BOOL OnInitDialog()
    	{
    		CListBox wndListBox;
    		wndListBox.Attach (GetDlgItem (IDC_LIST1)->m_hWnd);
    		wndListBox.AddString (_T ("One"));
    		wndListBox.AddString (_T ("Two"));
    		wndListBox.AddString (_T ("Three"));
    		wndListBox.Detach ();
    		return true;
    		
    	}
                    void CallDialog()
                   {
                                   Dlg d1(IDD_DLTEST1);
                                   d1.DoModal();
                   }
    	DECLARE_MESSAGE_MAP()
    };
    BEGIN_MESSAGE_MAP(Dlg,CDialog)
    	ON_COMMAND(IDOK, CallDialog)
    END_MESSAGE_MAP()
    class  CS : public CFrameWnd
    {
    	DECLARE_DYNCREATE( CS )
    	CToolBar   tb;
    	CStatusBar   sb;
    public:
    void OnCreate(LPCREATESTRUCT   cs)
    	{
    		CFrameWnd::OnCreate(cs);
    		sb.Create(this);
    		tb.Create(this);
    		tb.LoadToolBar(IDR_MNTB);
    		tb.EnableDocking( CBRS_ALIGN_ANY );
    		EnableDocking( CBRS_ALIGN_ANY );
    		DockControlBar( &tb );
    	}
                   BOOL CS::PreCreateWindow(CREATESTRUCT& cs)
                   {
                   }
    	DECLARE_MESSAGE_MAP()
    };
    IMPLEMENT_DYNCREATE( CS, CFrameWnd )
    BEGIN_MESSAGE_MAP( CS, CFrameWnd )
    	
    	ON_WM_CREATE()
    
    END_MESSAGE_MAP()
    //////////////////////////////////////////////////////////////////////////////////////////////
    class  CT : public CWinApp
    {
    public:
    	BOOL  InitInstance()
    	{
    		CSingleDocTemplate  *pd = new CSingleDocTemplate(
    			IDR_MNTB,
    			RUNTIME_CLASS( CD ),
    			RUNTIME_CLASS( CS ),
    			RUNTIME_CLASS( CV ));
    		AddDocTemplate(pd);
    		
    		CCommandLineInfo  cf;
    		ParseCommandLine(cf);
    		if (!ProcessShellCommand(cf)) return false;
    		return true;
    	}
    	
    	DECLARE_MESSAGE_MAP()
    };
    BEGIN_MESSAGE_MAP(CT,CWinApp)
    	ON_COMMAND( ID_EXIT, CWinApp::CloseAllDocuments)
    END_MESSAGE_MAP()
    //////////////////////////////////////////////////////////////////////////////////////////////
    CT a;
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    We normally dont do this
    ON_COMMAND(IDOK, CallDialog)
    but we override the OnOK but that should not be the cause of the problem. You are treating IDOK as a normal button and call dialog should be called.

    Is it that CallDialog is not getting called then follow this steps

    Make a dialog based MFC exe application

    Copy the following line in the message map
    ON_COMMAND(IDOK, CallDialog)

    Now add the CallDialog function in .h as follows
    afx_msg void CallDialog();

    I think this should do the job

    Thanks
    Shabbir
     
  7. zippor

    zippor New Member

    Joined:
    Nov 13, 2006
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I don't know much about .h, In the situation, Could you show content of .h file? thanks
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Opening the file on your system will show you the content.
     

Share This Page