⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wpw_mfc_task_95.html

📁 VC programing
💻 HTML
字号:
<HTML>

<HR><A NAME=MFC_TASK_SINGLE_INSTANCE>
Return to <a href="wpw_mfc_index.html#TOC">Table of Contents for this chapter</a><br> 
<H4>Subject: Single Instance of WinAPP</H4><PRE>
David Webber <dave@musical.demon.co.uk> writes:

>I want to detect previous instances of my program.  For that
>I used to use FindWindow with the main window class name.

>But now I've migrated to MFC my main window class name is chosen
>by MFC deep in its guts.

>Is there an easy way for me to create my main window with a name
>registered by me - the way I used to do it?

Look at the Microsoft sample ONETIME.ZIP, (MSDN CD or ftp.microsoft.com).
In brief:

const char* MyMainWndClassName = "MyMainWndXQW"

BOOL CMyApp::InitApplication()
{
  //Call base class.  Default version does nothing.
  CWinApp::InitApplication();  

  WNDCLASS wndcls;
  memset(&wndcls, 0, sizeof(WNDCLASS));   // start with NULL defaults

  // Get class information for default window class.
  ::GetClassInfo(AfxGetInstanceHandle(),"AfxFrameOrView",&wndcls);

  // Substitute unique class name for new class
  wndcls.lpszClassName = MyMainWndClassName;
  
  //Register new class and return the result code

  return ::RegisterClass(&wndcls);
}

BOOL CMyApp::FirstInstance()
{
  CWnd *PrevCWnd, *ChildCWnd;
  
  // Determine if another window with our class name exists...
  PrevCWnd = CWnd::FindWindow(MyMainWndClassName, NULL);
  if (PrevCWnd != NULL)
  {
    // if so, does it have any popups?
    ChildCWnd=PrevCWnd->GetLastActivePopup(); 
    // Bring the main window to the top
    PrevCWnd->BringWindowToTop();
    
    // If iconic, restore the main window
    if (PrevCWnd->IsIconic()) 
       PrevCWnd->ShowWindow(SW_RESTORE);
    // If there are popups, bring them along too!
    if (PrevCWnd != ChildCWnd) 
       ChildCWnd->BringWindowToTop();
    // Return FALSE.  This isn't the first instance
    // and we are done activating the previous one.
    return FALSE;
  }
  else
    // First instance. Proceed as normal.
    return TRUE;
}

CMyApp::InitInstance()
{
    if (!FirstInstance()) return FALSE;
    ...
}

>Dave
-- 
   Niels Ull Jacobsen, Dep. of CS, U of Copenhagen (null@diku.dk)
   Roenne Alle 3 st.th, 2860 Soeborg, Denmark, tel. +45 39 66 39 86
   
 <HR>
 Try OnCreateClient() which is virtual. Use SetTitle().

>I want to detect previous instances of my program.  For that
>I used to use FindWindow with the main window class name.

>But now I've migrated to MFC my main window class name is chosen
>by MFC deep in its guts.

>Is there an easy way for me to create my main window with a name
>registered by me - the way I used to do it?

>I tried to override CFrameWnd::Create() (which is called by LoadFrame)
>but it isn't virtual (unlike CWnd::Create()).  Pity.

>I know there *must* be an easy answer to this.

>Any help appreciated as always.

>Dave
>-- 
>+---------------------------+-----------------------------------+
>| Dave Webber                        dave@musical.demon.co.uk   |
>| Author of MOZART, the Windows 3.1 shareware Music Processor   |
>| and member of the Association of Shareware Professionals      |
>+---------------------------+-----------------------------------+

<HR>

In article <3s3dgh$fa5@odin.diku.dk> null@diku.dk "Niels Ull Jacobsen" writes:

> David Webber <dave@musical.demon.co.uk> writes:
> 
> >I want to detect previous instances of my [MFC] program.  For that
> >I used to use FindWindow with the main window class name.
> > 
>
> Look at the Microsoft sample ONETIME.ZIP, (MSDN CD or ftp.microsoft.com).
> In brief.....

Neils,

Thanks for pointing me in this direction.  I've checked it.  I didn't
understand how your answer could possibly work, but you didn't get to
the crucial step: override PreCreateWindow() and substitute my own
class name before the window gets created!  All is now clear and you
gave me enough information to get on with it, confident that it will
work.

I might even start subscribing to MSDN! :-)

Thanks again,

Dave.
-- 
+---------------------------+-----------------------------------+
| Dave Webber                        dave@musical.demon.co.uk   |
| Author of MOZART, the Windows 3.1 shareware Music Processor   |
| and member of the Association of Shareware Professionals      |
+---------------------------+-----------------------------------+
</PRE>
 

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -