如何使vc++窗口应用程序保持一个实例

把下面红色文字的代码加到Winmain函数的开始或者InitInstance函数的开始处,MUTEXTOWINAPP为一字符串。

// check one instance

HANDLE MaxOneInstance = CreateMutex(NULL, FALSE, MUTEXTOWINAPP);
if (MaxOneInstance)
{
if (ERROR_ALREADY_EXISTS == GetLastError())
{
HWND DestinationWindow = FindWindowEx(NULL, NULL, szWindowClass, szTitle);
if (DestinationWindow)
{
SetForegroundWindow(DestinationWindow);
if (IsWindowVisible(DestinationWindow))
{
}
else
{
if (IsIconic(DestinationWindow))
{
ShowWindow(DestinationWindow, SW_RESTORE);
}
else
{
ShowWindow(DestinationWindow, SW_SHOW);
}
}
}


CloseHandle(MaxOneInstance);
return FALSE;
}
}
else
{
if (ERROR_INVALID_HANDLE == GetLastError())
{
MessageBox(NULL, _T("mutex object name has been used."), szTitle, MB_OK);


return FALSE;

}

}

你可能感兴趣的:(object,null,vc++)