我已经开启了一个exe程序,双击打开又会开启一个同样exe,如何判断已经有这个exe在运行,如果此时不在前台,将其置顶?
谢谢!

解决方案 »

  1.   

    使用FindWindow查看返回句柄是否有效
    如果有效就BringWindowToFront(hWnd);
    SetForegroundWindow(hWnd);
    SetWindowPos(....)把其置顶,然后退出如果无效就正常启动
      

  2.   

    在BOOL CTestCaptureApp::InitInstance()添加如下代码HANDLE hMutex=CreateMutexW(NULL,TRUE,L"随便写点就行");   
        if(hMutex)   
        {   
            if(ERROR_ALREADY_EXISTS==GetLastError())   
            {   
                AfxMessageBox(L"程序已在运行中!");   
                return FALSE;   
            }   
        } 
      

  3.   

    互斥变量也是一种方法链接讲了互斥变量和FindWindow两种方式,因为你要将其置顶,因此我觉得FindWindow更合适一些http://wtunuiyf.blog.163.com/blog/static/18003200682573922496/
      

  4.   

    防止打开多个exe
    http://topic.csdn.net/u/20101119/15/8fcae5f3-df45-4116-9f03-a543ef1ee910.html
      

  5.   


    // App类的InitInstance函数中加入HANDLE hMutext = ::CreateMutex(NULL, FALSE, _T("A"));
    if(hMutext && ERROR_ALREADY_EXISTS == GetLastError())
    {
    ::CloseHandle(hMutext);
    HWND hWnd = ::FindWindow(NULL, _T("A"));
    if(hWnd && ::IsIconic(hWnd))
    {
    ::ShowWindow(hWnd, SW_RESTORE);
    }
    BringWindowToTop(hWnd);
    SetForegroundWindow(hWnd);
    return FALSE;
    }