使应用程序只能运行一次,第二次运行显示上次程序

来源:互联网 发布:淘宝如何不显示会员名 编辑:程序博客网 时间:2024/05/22 21:46


// 此程序只能运行一次,用互斥量来判断程序是否已运行HANDLE m_hMutex=CreateMutex(NULL,TRUE, m_pszAppName); if(GetLastError() == ERROR_ALREADY_EXISTS){ ReleaseMutex(m_hMutex);HWND hHWND = ::FindWindow(NULL, m_pszAppName);if(::IsWindowVisible(hHWND)){ ::ShowWindow(hHWND,SW_SHOW);}return FALSE;}


将窗口置顶并且居中显示


HWND  hWnd = FindWindow(NULL, _T("添加设备"));CRect rectWindow;if (hWnd != NULL && ::IsWindow(hWnd)){int   cx = GetSystemMetrics(SM_CXSCREEN);int   cy = GetSystemMetrics(SM_CYSCREEN);GetWindowRect(hWnd, &rectWindow);::SetForegroundWindow(hWnd);::SetWindowPos(hWnd, HWND_TOP, (cx - rectWindow.right) / 2, (cy - rectWindow.bottom) / 2, 0, 0, SWP_NOMOVE|SWP_DRAWFRAME|SWP_NOSIZE);//SWP_NOMOVE || SWP_NOSIZEbreak;}


1 0