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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mixing Dialog-based and SDI apps 1

Status
Not open for further replies.

JillyT

Programmer
Jul 11, 2002
23
CA
Is it possible to call an SDI app from a dialog-based app?? Suggestions for how to do this...are there existing functions or does the code essentially have to be copied from the app::initinstance file of the SDI app to the button command of the calling dialog along with all SDI files required? Thanks oodles!
 
>> Is it possible to call an SDI app from a dialog-based
>> app??

No it is NOT possible to call any process from another, well not really... sort of.

That said, there are almost infinite number of ways to perform inter-process communications. This of course requires one of two things.

1) You own both applications so you can develop whatever mechanism you want.

2) The application you don't control provides a specification (API,SDK) for communications.

-pete
 
Oh no...I made both applications...I've been working on a dialog-based application but then realized that in order to have a feature that allows the user to view an invoice for a particular order, an SDI approach might work best. The problem is trying to link the two. I would ultimately like a button in the dialog-based app to open up a document-based app...so I have all the source code I just don't know how to call one from the other.
 
>> I would ultimately like a button in the dialog-based app
>> to open up a document-based app

Consult the Windows SDK for the following:


ShellExecute
CreateProcess
CreateProcessEx


-pete
 
I was hoping to not access the SDI app from the outside type of thing. I haven't actually designed the document interface yet so it can be rewritten...there is no way to incorporate the document interface into my current (dialog-based) application?
 
Not so good with the messaging!

JillyT, please try to be clear in your messages, it will reduce the unwanted noise, as well as provide you with a solution in a shorter timeframe.

This is my last guess.

>> there is no way to incorporate the document interface
>> into my current (dialog-based) application?

Yes there is. You can create a CFrameWnd class and show it.
This frame window will use the same document and view classes from your SDI project. This can all be done from within your dialog based application.

To get started to see how this is done look at your CWinApp derived class in your SDI project. Look at that classes InitInstance implementation. Look at it's use of CSingleDocTemplate.

Hope this helps
-pete
 
Thanks for the tip but I'm still really stumped...that actually was the approach I was taking before posting here but I didn't know if I was on the right track...still don't know if I'm doing it right though. My Main dialog box (which of course is called by "the" app class (MyProject.cpp) has a button. In the button's Click event I pretty much copied the code from the SDI app's InitInstance function with some tweaking since there was no reference to the MyProject app object required in the InitInstance code. So this is what my button function looks like...I don't know what its doing cause its not doing anything when I click on it...

void MainDlg::OnViewinvoices()
{
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CInvoices2Doc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CInvoices2View));
AddDocTemplate(pDocTemplate);

// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();

}

There are no compile errors...just from the info above, do you have any suggestions?
 
I've discovered that my problem lies in that m_pMainWnd is pointing to the Dialog object (CMyMainDlg) whereas a standalone working SDI application has m_pMainWnd pointing to a CMainFrame object. How can I get m_pMainWnd to point to a CMainFrame object...I can't declare it by: CMainFrame myMainFrame; because the constructor is protected.
 
Just as Pete described...you have to "create" a CFrameWnd object...thanks oodles...just needed to think a little bit.

_______________________________________________
CMainFrame *pMainFrame = new CMainFrame();

pMainFrame->Create(NULL,"The Frame");
m_thisApp->m_pMainWnd = pMainFrame;

m_thisApp->m_pMainWnd->ShowWindow(SW_SHOW);
m_thisApp->m_pMainWnd->UpdateWindow();
_________________________________________________
 

This is a good lesson learned. Here’s a tip:

Always create a MDI app when creating a new project even if it's only going to be a dialog app. This way you can't go wrong. If you only want a dialog app you create and show your dialog in CMainFrm and call ShowWindow(FALSE) in CMainFrm to hide the frame and view or views. Then if you change your mind and want an SDI app or MDI app (as you have done) you don't need to spend hours trying to cut and paste code from one to the other. You just add your view and don’t call ShowWindow(FALSE) in CMainFrm. Plus, you get CDoc's Serialized member function for your CDialog app for saving.

Of course if you only want a dialog app you need to look of IDCANCEL (or what ever you use to exit the app) in CMainFrm so you can destroy the app or your app will still be running when you close your dialog but you won’t see it because CMainFrm will be hidden.

Cheers,

Brother C
 
It "looked" like I got it but after closer scrutiny I discovered that when I push the button in my dialog to start up the SDI frame, only the frame object is getting created...the frame itself "looks" good, its complete with toolbar and menubar BUT the view and document objects do not get created. When you click New or Open, a new Frame, View and Document set of objects are successfully created and all is well but only after choosing New or Open. I obviously need things to work directly from the dialog. Any suggestions?? Let me know if you want to see parts of the code. Thanks so much!!
 
>> Just as Pete described
>> CMainFrame *pMainFrame = new CMainFrame();

>> pMainFrame->Create(NULL,"The Frame");
>> m_thisApp->m_pMainWnd = pMainFrame;

Ooops, no that's not what I was trying to say.

>> To get started to see how this is done look at your
>> CWinApp derived class in your SDI project. Look at that
>> classes InitInstance implementation. Look at it's use of
>> CSingleDocTemplate.

-pete
 
Here's some code:


BOOL CMyApp::InitInstance() {

if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
}

AfxEnableControlContainer();

#ifdef _AFXDLL
Enable3dControls();
#else
Enable3dControlsStatic();
#endif

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CInvoicesDoc),
RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CInvoicesView));
pDocTemplate->SetContainerInfo(IDR_CNTR_INPLACE);
AddDocTemplate(pDocTemplate);

MainDlg dlg;
int nResponse = dlg.DoModal();

return FALSE;
}



The button from my dialog box:
*****************************************
CMainFrame *pMainFrame = new CMainFrame();

pMainFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW, NULL, NULL );
m_documentWnd = pMainFrame;

m_documentWnd->ShowWindow(SW_SHOW);
m_documentWnd->UpdateWindow();

**********************************************
The rest is pretty much standard SDI code. m_documentWnd is a private CWnd pointer in the MainDlg class that had to be set up otherwise the Close button in the top right corner would close the whole app when I just wanted it to close the frame and go back to the dialog. This help at all?
 
If you need access to objects within the frame you are creating you have to do a little more than cut and paste.
You will need to keep track of your CSingleDocTemplate object and manage it's creation etc. But basically this is what you want to do.

CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CInvoicesDoc),
RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CInvoicesView));
pDocTemplate->SetContainerInfo(IDR_CNTR_INPLACE);

pDocTemplate->OpenDocumentFile("mydocument.txt");

Don't forget all those IDR_MAINFRAME resources that are needed. Luckily you can copy/paste them using the Resource view in VC++ IDE.


Hope this helps
-pete
 
Thanks for the tip...shortly before your posting I changed my button function to the following:

**************************************************
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CInvoicesDoc),
RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CInvoicesView));
pDocTemplate->SetContainerInfo(IDR_CNTR_INPLACE);
m_thisApp->AddDocTemplate(pDocTemplate);

pDocTemplate->OpenDocumentFile(NULL);
************************************************

Which works very happily, however, now I have the following problems:

The File/Exit button and X Exit button closes the entire application where I just want it to close the SDI frame...and the status bar is not updating when I hit Num Lock, Caps Lock, etc. Not really sure how to approach this problems but thanks for all the help you've given me this far!! You've been a life saver...cherry flavoured!
 
Remove this:

>> m_thisApp->AddDocTemplate(pDocTemplate);

-pete
 
You rock...thanks SOOOOOOOOOOOOOOOOOOOOOOOOOO much!
 
Any idea why that idea doesn't work if I'm just using a CView object rather than a CRichTextView?
 
what do you mean by CView?

CView is an abstract class.

-pete

 
Sorry...I was wondering if I really needed to use CRichTextView as my base class for my View class...besides...I was having trouble drawing to it. Plus, I realized I only want to draw to it programmatically, I don't want the user to be able to write text to it and add OLE objects, etc. So I went back to a former version of my app that used CView as the base class of my View class...make more sense?? The code in my App class and my Dialog button function are the same and I even took out m_thisApp->AddDocTemplate(pDocTemplate); but it is still closing the whole application when I try to exit the document frame.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top