• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大龍的博客

            常用鏈接

            統(tǒng)計

            最新評論

            如何做到像《金山詞霸》一樣只運行一個實例 --------- zt

            如何做到像《金山詞霸》一樣只運行一個實例

            作者:孫鑫           日期:2002-7-30

            我們在使用《金山詞霸》時發(fā)現(xiàn),在《金山詞霸》已經(jīng)運行了的情況下,再次點擊《金山詞霸》的圖標,那么它不會再運行另外一個《金山詞霸》,而是將已有的《金山詞霸》給激活,始終只能運行一個《金山詞霸》的實例。

            在我們的程序當(dāng)中如果要實現(xiàn)類似《金山詞霸》的功能,就要解決兩個問題,首先是要判斷該程序已有一個實例在運行,其次是要將已運行的應(yīng)用程序?qū)嵗せ?,同時退出第二個應(yīng)用程序?qū)嵗?/span>

            對于第一個問題,我們可以通過設(shè)置命名互斥對象或命名信標對象,在程序啟動的時候檢測互斥對象或信標對象,如互斥對象或信標對象已存在,則可以判斷此程序已有一個實例正在運行。

            第二個問題是如何找到已經(jīng)運行的應(yīng)用程序?qū)嵗?,如果我們能夠找到已運行實例主窗口的指針,即可調(diào)用SetForegroundWindow來激活該實例。我們可以通過兩種形式找到已運行實例的主窗口,一種形式是通過調(diào)用FindWindowEx去查找正在運行的窗口的句柄,這種方式用得比較多一些,而本文通過另一種形式去查找正在運行的窗口的句柄。通過調(diào)用SetProp給應(yīng)用程序主窗口設(shè)置一個標記,用GetDesktopWindow 可以獲取Windows環(huán)境下的桌面窗口的句柄,所有應(yīng)用程序的主窗口都可以看成該窗口的子窗口,接著我們就可以用GetWindow函數(shù)來獲得這些窗口的句柄。然后再用Win32 SDK函數(shù)GetProp查找每一個應(yīng)用程序的主窗口是否包含有我們設(shè)置的標記,這樣就可以找到我們要找的第一個實例主窗口。

            下面演示代碼是以一個單文檔應(yīng)用程序為例,工程名字是Mutex。

            1、在應(yīng)用程序類InitInstance()函數(shù)中判斷是否已有一個應(yīng)用程序?qū)嵗谶\行。

            BOOL CMutexApp::InitInstance()

            {

                   //創(chuàng)建命名信標對象。

                   HANDLE hSem=CreateSemaphore(NULL,1,1,"維新");

                   if(hSem) //信標對象創(chuàng)建成功。

                   {

                          //信標對象已經(jīng)存在,則程序已有一個實例在運行。

                          if(ERROR_ALREADY_EXISTS==GetLastError())

                          {                  

                                 CloseHandle(hSem);      //關(guān)閉信號量句柄。

             

            //獲取桌面窗口的一個子窗口。

                                 HWND hWndPrev=::GetWindow(::GetDesktopWindow(),GW_CHILD);   

             

                                 while(::IsWindow(hWndPrev))

                                 {

                                 //判斷窗口是否有我們預(yù)先設(shè)置的標記,如有,則是我們尋找的窗口,并將它激活。

                                        if(::GetProp(hWndPrev,"維新"))   

                                        {

                                        //如果主窗口已最小化,則恢復(fù)其大小。

                                               if (::IsIconic(hWndPrev))     

                                                      ::ShowWindow(hWndPrev,SW_RESTORE);

             

                                               //將應(yīng)用程序的主窗口激活。

                                               ::SetForegroundWindow(hWndPrev);

                                               return FALSE;                      //退出實例。

                                        }

                                        //繼續(xù)尋找下一個窗口。

                                        hWndPrev = ::GetWindow(hWndPrev,GW_HWNDNEXT);

                                 }

                                

                                 AfxMessageBox("已有一個實例在運行,但找不到它的主窗口!");

                          }

                   }

                   else

                   {

                          AfxMessageBox("創(chuàng)建信標對象失敗,程序退出!");

                          return FALSE;

                   }

             

                   AfxEnableControlContainer();

             

                   // Standard initialization

                   // If you are not using these features and wish to reduce the size

                   // of your final executable, you should remove from the following

                   // the specific initialization routines you do not need.

                  

            #ifdef _AFXDLL

                   Enable3dControls();                     // Call this when using MFC in a shared DLL

            #else

                   Enable3dControlsStatic();      // Call this when linking to MFC statically

            #endif

             

                   // Change the registry key under which our settings are stored.

                   // TODO: You should modify this string to be something appropriate

                   // such as the name of your company or organization.

                   SetRegistryKey(_T("Local AppWizard-Generated Applications"));

             

                   LoadStdProfileSettings(); // Load standard INI file options (including MRU)

             

                   // Register the application's document templates. Document templates

                   // serve as the connection between documents, frame windows and views.

             

                   CSingleDocTemplate* pDocTemplate;

                   pDocTemplate = new CSingleDocTemplate(

                          IDR_MAINFRAME,

                          RUNTIME_CLASS(CMutexDoc),

                          RUNTIME_CLASS(CMainFrame),       // main SDI frame window

                          RUNTIME_CLASS(CMutexView));

                   AddDocTemplate(pDocTemplate);

             

                   // Parse command line for standard shell commands, DDE, file open

                   CCommandLineInfo cmdInfo;

                   ParseCommandLine(cmdInfo);

             

                   // Dispatch commands specified on the command line

                   if (!ProcessShellCommand(cmdInfo))

                          return FALSE;

             

                   // The one and only window has been initialized, so show and update it.

                   m_pMainWnd->ShowWindow(SW_SHOW);

                   m_pMainWnd->UpdateWindow();

             

                   return TRUE;

            }

            2、在框架類的OnCreate()函數(shù)中設(shè)置查找標記。

            int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

            {

                   if (CFrameWnd::OnCreate(lpCreateStruct) == -1)

                          return -1;

                  

                   if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP

                          | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||

                          !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))

                   {

                          TRACE0("Failed to create toolbar\n");

                          return -1;      // fail to create

                   }

             

                   if (!m_wndStatusBar.Create(this) ||

                          !m_wndStatusBar.SetIndicators(indicators,

                           sizeof(indicators)/sizeof(UINT)))

                   {

                          TRACE0("Failed to create status bar\n");

                          return -1;      // fail to create

                   }

             

                   // TODO: Delete these three lines if you don't want the toolbar to

                   // be dockable

             

                   m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

                   EnableDocking(CBRS_ALIGN_ANY);

                   DockControlBar(&m_wndToolBar);

             

                  

                   //設(shè)置查找標記。

                   ::SetProp(m_hWnd,"維新",(HANDLE)1);

             

                   return 0;

            }

            3、在程序退出是刪除設(shè)置的標記,在框架類中響應(yīng)WM_DESTROY消息,進行處理。

            void CMainFrame::OnDestroy()

            {

                   CFrameWnd::OnDestroy();

                  

                   // TODO: Add your message handler code here

                   //刪除所設(shè)置的標記。

                   ::RemoveProp(m_hWnd,"維新");

            }

            至此,使應(yīng)用程序只運行一個實例的功能就完成了。

            posted on 2007-08-29 11:32 大龍 閱讀(180) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久久久久久尹人综合网亚洲| 久久久久无码专区亚洲av| 久久国产热精品波多野结衣AV| 久久综合给合久久狠狠狠97色69 | 久久综合日本熟妇| 久久免费视频1| 久久久久人妻精品一区 | 99精品国产在热久久无毒不卡 | 久久久久亚洲精品日久生情| 久久婷婷国产剧情内射白浆| 亚洲国产另类久久久精品小说| 久久亚洲美女精品国产精品| 中文字幕亚洲综合久久2| 亚洲?V乱码久久精品蜜桃| 久久午夜羞羞影院免费观看| 情人伊人久久综合亚洲| 久久久久av无码免费网| 久久被窝电影亚洲爽爽爽| 亚洲精品WWW久久久久久 | 久久综合久久鬼色| 国产V亚洲V天堂无码久久久| 香蕉99久久国产综合精品宅男自 | 欧美久久久久久| 久久综合丝袜日本网| 久久99热这里只有精品国产| 国产午夜福利精品久久| 日本强好片久久久久久AAA| 久久黄视频| av国内精品久久久久影院| 亚洲欧洲久久av| 国产精品成人99久久久久91gav| 中文字幕无码精品亚洲资源网久久| 91久久精品视频| 国产精品久久99| 久久天堂AV综合合色蜜桃网| 久久久久久久91精品免费观看| 伊人久久免费视频| 成人国内精品久久久久一区 | 99久久精品免费观看国产| 无码精品久久久久久人妻中字| 久久精品国产亚洲5555|