BOOL CSingleDocApp::InitInstance()
{
HANDLE hSep = ::CreateSemaphore(NULL,1,1,m_strAppName);
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hSep);
HWND hWnd = ::GetWindow(::GetDesktopWindow(),GW_CHILD);
while(hWnd)
{
if(GetProp(hWnd,m_strAppName))
{
if(::IsIconic(hWnd))
::ShowWindow(hWnd,SW_RESTORE);
::SetForegroundWindow(hWnd);
return FALSE;
}
hWnd = ::GetWindow(hWnd,GW_HWNDNEXT);
}
return FALSE;
}
AfxEnableControlContainer();
......
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
// .....
::SetProp(m_hWnd,theApp.m_strAppName,(HANDLE)1);
return 0;
}
void CMainFrame::OnDestroy()
{
CFrameWnd::OnDestroy();
// TODO: Add your message handler code here
RemoveProp(m_hWnd,theApp.m_strAppName);
}