15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Finding open window

Jackie Braun -- jbraun@eagle.wbm.ca
Thursday, April 11, 1996

Hi.

I am using VC 1.51, Win 3.1.                         
                         
I am developing an application which has several CMultiDocTemplates. Each
document/view pair is to be created via a menu command. So, the first time
the menu command is activated, the appropriate document/view/frame is created.  
If the same menu command is subsequently activated, I would like to bring
the window containing the appropriate document/view to the front, and not
create a new window.  This sounds as though it should be simple enough to
implement, but I am having trouble determining how to find
appropriate window.  I have looked at FindWindow(), but it searches only
top-level windows.  

I would appreciate any suggestions.

Thanks,
Jackie Braun.


-----------------------------------------------------------------
 Jackie Braun, Ph.D. |  Randco Software Corporation
 Consultant          |  2530 Hanover Avenue
 jbraun@eagle.wbm.ca |  Saskatoon SK         Phone (306)343-3380
                     |  Canada S7J 1G1       Fax   (306)343-3341
-----------------------------------------------------------------




Darius Thabit -- darius@world.std.com
Sunday, April 14, 1996

[Mini-digest: 5 responses]

> I am developing an application which has several CMultiDocTemplates. Each
> document/view pair is to be created via a menu command. So, the first tim
> the menu command is activated, the appropriate document/view/frame is crea
> If the same menu command is subsequently activated, I would like to bring
> the window containing the appropriate document/view to the front, and not
> create a new window.  This sounds as though it should be simple enough to

   You can create a "new" view for a my doc template by calling 
CMultiDocTemplate::OpenDocumentFile(NULL).  You can "open" an existing file 
associated with a template by passing a path to this routine, but if you use 
the CWinApp version of the call it searches the template list to see whether 
the file is already in an open window (this logic starts in appdlg.cpp).

   It sounds like you are saying that you will only have one window of each 
template type.  If that's true, and if you never "save" these things, you 
could make up a dummy file name for each, and call CDocument::SetPathName() 
whenever you create a new window; then call CWinApp::OpenDocumentFile with 
the special name to activate that window later.

   Otherwise, you can steal the logic to search the template list (as I have 
done for some things).

-----From: erik.heuer@ks-t.no (Erik Heuer)

Look at EnumChildWindows.  It allows you to search all children of 
a particular top level window. 
Erik Heuer, Kongsberg Simulation & Training, 3600 Kongsberg, Norway
E-mail: erik.heuer@ks-t.no  Phone:(+47) 32735766 Fax:(+47) 32736965

-----From: "Martin J. Mahoney" 

Try this it should satisfy your needs.

In your CWinApp derived class add a CMapWordtoOb such as
class CMyWinApp : public CWinApp
{
...
  CMapWordToOb m_DocumentViewMap;
...
};

and in your CWinApp derived class a a function like the following.

long CMyWinApp::OnOpenDocumentView(WORD requestedTemplate)
{
  CMDIChildWnd *myMDIFrame;

  m_DocumentViewMap.Lookup(requestedTemplate, (CObject *&)myMDIFrame);

  if (myMDIFrame == NULL)
  {
    //Document View never created so create new view and add to map
    myMDIFrame = CreateRequestedTemplate(requestedTemplate);
    m_DocumentViewMap.SetAt(requestedTemplate, myMDIFrame);
  }
  else
  {
    //Document view already exists just bring view to top and set focus
    myMDIFrame->ActivateDrame();
  }

  return 0;
}

Martin J. Mahoney
-----From: Niels Ull Jacobsen 

Here's a piece of code from my program, which opens a new (or and old) view
on a document. It might be of use to you. I derived from CMultiDocTemplate
to provide this functionality.

CMDIChildWnd *CMyMultiDocTemplate::CreateView(
        CDocument* pDoc, 
        BOOL bOrActivateLastViewOfClass /* = FALSE */)
// Creates a new view on the document or activates an existing view of 
// a document
// Returns a pointer to the frame window of the view

{
	CMDIChildWnd *pFrame;

	ASSERT_VALID(this);
	ASSERT_VALID(pDoc);
	ASSERT(pDoc->IsKindOf(GetDocClass()));

	if (bOrActivateLastViewOfClass)
	{
		CView* pView;
		POSITION pos = pDoc->GetFirstViewPosition();
		while (pos)
		{
		    pView = pDoc->GetNextView(pos);
		    if (pView->IsKindOf(m_pViewClass))
		    {
		        pFrame = CAST(CMDIChildWnd, pView->GetParentFrame());
				ASSERT_VALID(pFrame);
		        pFrame->ActivateFrame();
				return pFrame;
		    }
		}
	} 

	pFrame = CAST(CMDIChildWnd, CreateNewFrame(pDoc, NULL));
	ASSERT_VALID(pFrame);
	if (pFrame != NULL) // Just for safe
	{
		ASSERT(pFrame->IsKindOf(m_pFrameClass));
		InitialUpdateFrame(pFrame,pDoc,TRUE);
	};
	return pFrame;	
};

Niels Ull Jacobsen, Krьger A/S (nuj@kruger.dk)
Everything stated herein is THE OFFICIAL POLICY of the entire Kruger group
and should be taken as legally binding in every respect. Pigs will grow
wings and fly.





-----From: JLR@ARTECH.COM.UY (Jose Lamas RЎos)

There's a public member of CWinApp named m_templateList that you can use for 
this.

Example:
CDocument* CYourApp::IsDocOpen(parameters that identify the doc you're 
looking for)
{
   POSITION posTpl = m_templateList.GetHeadPosition();

   while (posTpl)
   {
      CDocTemplate* pTemplate = (CDocTemplate*) 
m_templateList.GetNext(posTpl);
      ASSERT_VALID(pTemplate);
      ASSERT(pTemplate->IsKindOf(RUNTIME_CLASS(CDocTemplate)));

      POSITION posDoc = pTemplate->GetFirstDocPosition();
      while (posDoc)
      {
         CDocument* pDoc = pTemplate->GetNextDoc(posDoc);
         if (pDoc is what you are looking for)
              return pDoc;
      }
   }
   return NULL;
}

The above example allows you to iterate through all the documents currently 
opened for each of the templates you have added to the app (using 
AddDocTemplate). I don't know how you want to determine which of them is 
what you are looking for, but I guess you do. If you find the document and 
want to bring it to the front you can do it using the following:

{
   POSITION posView = pDoc->GetFirstViewPosition();
   while (posView)
   {
      CView* pView = pDocument->GetNextView(viewpos);
      if (pView is the one you want to activate)
      {
         pView->GetParentFrame()->ActivateFrame();
         return;
      }
   }
}

Hope this helps



David W. Gillett -- DGILLETT@expertedge.com
Tuesday, April 16, 1996

> I am using VC 1.51, Win 3.1.
>
> I am developing an application which has several
> CMultiDocTemplates. Each document/view pair is to be created via a
> menu command. So, the first time the menu command is activated, the
> appropriate document/view/frame is created.  If the same menu
> command is subsequently activated, I would like to bring the window
> containing the appropriate document/view to the front, and not
> create a new window.  This sounds as though it should be simple
> enough to implement, but I am having trouble determining how to
> find appropriate window.  I have looked at FindWindow(), but it
> searches only top-level windows.  
> 
> I would appreciate any suggestions.

  If you have a separate menu command for each doc/view pair, then 
you can't have a huge number of them, right?
  So what I would do is provide each with a CWnd* (or derivative...), 
initialized to NULL.  In the handler for each menu command, you check 
the corresponding pointer -- if it's NULL, you create the doc/view 
and save the view pointer; if it's not NULL, you activate that view.
[You could get slick and use something like an array or list that 
matches menu commands to doc/view templates and view pointers, and so 
let these menu entries share a single handler....]

Dave





| Вернуться в корень Архива |