?? singleinstance.cpp
字號:
#include "stdafx.h"
#include "SingleInstance.h"
CSingleInstance::CSingleInstance()
{
// Set our default values
m_hMutex = NULL;
}
CSingleInstance::~CSingleInstance()
{
if ( m_hMutex != NULL ) {
ReleaseMutex( m_hMutex );
}
}
BOOL CSingleInstance::Create(LPCSTR pszClassName)
{
// Add the word 'Class' to the end
m_strClassName = pszClassName;
// Create the mutex
m_hMutex = CreateMutex( NULL, FALSE, m_strClassName );
//m_hMutex = CreateSemaphore(NULL, 1, 1, m_strClassName);
// Check for errors
if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
m_hMutex = NULL;
CString s;
// 尋找先前實例的主窗口
HWND hWndPrevious = ::GetWindow(::GetDesktopWindow(),GW_CHILD);
while (::IsWindow(hWndPrevious))
{
// 檢查窗口是否有預設的標記?
// 有,則是我們尋找的主窗
::GetWindowText(hWndPrevious,(char*)(LPCTSTR)s ,255);
if (!s.Find(m_strClassName))
{
ShowWindow(hWndPrevious,SW_SHOW);
ShowWindow( hWndPrevious, SW_RESTORE );
BringWindowToTop( hWndPrevious );
SetForegroundWindow( hWndPrevious );
return FALSE;
}
// 繼續尋找下一個窗口
hWndPrevious = ::GetWindow(hWndPrevious,GW_HWNDNEXT);
}
// 前一實例已存在,但找不到其主窗
// 可能出錯了
// 退出本實例
return FALSE;
}
// Return success
return TRUE;
}
CString CSingleInstance::GetClassName( void ) const
{
return m_strClassName;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -