#define APP_ID "C4A75624-61A8-4D2D-9112-E410742857E8" // GUID
void CTestDlg::OnlyRunOneInstance()
{
BOOL bInstanceExist=false;
HWND hWnd = ::GetWindow(::GetForegroundWindow(), GW_HWNDFIRST);
for (; hWnd; hWnd = ::GetWindow(hWnd, GW_HWNDNEXT))
{
if ((HANDLE)::GetProp(hWnd, APP_ID) == (HANDLE)1)
{
if (::IsIconic(hWnd))
{
::ShowWindow(hWnd, SW_RESTORE); //还原最小化的窗口
}
::SetForegroundWindow(hWnd); //激活窗口
bInstanceExist=true; //赋值0是为了防止ExitInstance中将找到的ATOM量删除
break;
}
}
if(bInstanceExist)
{
exit(0);
}
else
{
// 给添加一个具有唯一性的属性值
::SetProp(this->m_hWnd, APP_ID, (HANDLE)1);
}
}
然后在初始化对话框中调用函数OnlyRunOneInstance()即可