BOOL CMyApp::InitInstance()
{
//检测程序是否运行一个实例
if (! IsFirstRun())
return FALSE;...
// 建立对象
::SetProp(AfxGetApp()->m_pMainWnd->m_hWnd, m_pszExeName, (HANDLE)1); }BOOL CMyApp::IsFirstRun()
{
HANDLE m_hMutex = CreateMutex( NULL, FALSE, m_pszExeName);
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
CloseHandle(m_hMutex);
HWND hWndPrevious = ::GetWindow(::GetDesktopWindow(), GW_CHILD);
while (::IsWindow(hWndPrevious))
{
if (::GetProp(hWndPrevious, m_pszExeName))
{
if (::IsIconic(hWndPrevious))
::ShowWindow(hWndPrevious,SW_RESTORE);
::SetForegroundWindow(hWndPrevious);
::SetForegroundWindow(::GetLastActivePopup(hWndPrevious));
return FALSE;
}
hWndPrevious = ::GetWindow(hWndPrevious, GW_HWNDNEXT);
}
return FALSE;
}
return TRUE;
}
{
//检测程序是否运行一个实例
if (! IsFirstRun())
return FALSE;...
// 建立对象
::SetProp(AfxGetApp()->m_pMainWnd->m_hWnd, m_pszExeName, (HANDLE)1); }BOOL CMyApp::IsFirstRun()
{
HANDLE m_hMutex = CreateMutex( NULL, FALSE, m_pszExeName);
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
CloseHandle(m_hMutex);
HWND hWndPrevious = ::GetWindow(::GetDesktopWindow(), GW_CHILD);
while (::IsWindow(hWndPrevious))
{
if (::GetProp(hWndPrevious, m_pszExeName))
{
if (::IsIconic(hWndPrevious))
::ShowWindow(hWndPrevious,SW_RESTORE);
::SetForegroundWindow(hWndPrevious);
::SetForegroundWindow(::GetLastActivePopup(hWndPrevious));
return FALSE;
}
hWndPrevious = ::GetWindow(hWndPrevious, GW_HWNDNEXT);
}
return FALSE;
}
return TRUE;
}
你的方法只能确定运行一个实例,我的问题是如何双击文档只启动一个实例,在该实例的子窗口显示对应文档。
我用AppWizard生成的多文档工程,只要文档类型是新的类型,则双击文档只启动一个实例,在该实例的子窗口显示对应文档。
我现在恰好用的是系统已注册的html文档类型。