BOOL CXTPShortcutBarPopup::OnNcCreate(LPCREATESTRUCT lpCreateStruct) { if (!CWnd::OnNcCreate(lpCreateStruct)) return FALSE; if (GetStyle() & MFS_SYNCACTIVE) { // syncronize activation state with top level parent CWnd* pParentWnd = GetTopLevelParent(); ASSERT(pParentWnd != NULL); if (!pParentWnd) return FALSE; CWnd* pActiveWnd = GetForegroundWindow(); BOOL bActive = (pParentWnd == pActiveWnd) || (pParentWnd->GetLastActivePopup() == pActiveWnd && pActiveWnd->SendMessage(WM_FLOATSTATUS, FS_SYNCACTIVE) != 0); // the WM_FLOATSTAzTUS does the actual work SendMessage(WM_FLOATSTATUS, bActive ? FS_ACTIVATE : FS_DEACTIVATE); } return TRUE; }
bool CCpDesktop::FirstInstance() { // Determine if another window with our class name or caption exists... CString strCaption; strCaption.LoadString(GetTitleId()); CWnd* pWndApp = CWnd::FindWindow(NULL, strCaption); if (!pWndApp) return true; // An instance of the app exists // Does it have any popups? CWnd* pPopupWnd = pWndApp->GetLastActivePopup(); // Bring the main window to the top pWndApp->BringWindowToTop(); pWndApp->SetForegroundWindow(); // If iconic, restore the main window if (pWndApp->IsIconic()) pWndApp->ShowWindow(SW_RESTORE); // If there was an active popup, bring it along too if (pPopupWnd && (pPopupWnd != pWndApp)) { pPopupWnd->BringWindowToTop(); pPopupWnd->SetForegroundWindow(); } return false; }
BOOL CALLBACK EnumWindowsProc(HWND hwnd/*当前找到的窗口句柄*/, LPARAM lParam/*自定义参数*/) //枚举当前窗口 { TCHAR tcClass[256]; LPWSTR pStr = (LPWSTR)lParam; HWND htmpWnd = NULL; ::GetClassName(hwnd, tcClass, 255); DWORD dwProcessID; TCHAR szProcessName[260] = { 0 }; GetWindowThreadProcessId(hwnd, &dwProcessID); HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessID); GetModuleFileNameEx(hProcess, NULL, szProcessName, MAX_PATH); if (strstr(szProcessName, _T("\\PaperEditor.exe")) != NULL) { CWnd * pWndPrev = CWnd::FromHandle(hwnd); pWndPrev = pWndPrev->GetParent(); if (pWndPrev != NULL) { CWnd * pWndChild = pWndPrev->GetLastActivePopup(); if (pWndPrev->IsIconic()) pWndPrev->ShowWindow(SW_RESTORE); pWndChild->SetForegroundWindow(); } } return TRUE; }
BOOL CSingleCtrl::Check() { CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD); ::CreateMutex(NULL, TRUE, m_sExeName.GetString()); if (GetLastError() != ERROR_ALREADY_EXISTS) return TRUE; while (pPrevWnd) { if (::GetProp(pPrevWnd->GetSafeHwnd(), m_sExeName.GetString())) { TRACE("Check: %s found\r\n", m_sExeName.GetString()); if (pPrevWnd->IsIconic()) { TRACE("Check: show it now\r\n"); pPrevWnd->ShowWindow(SW_RESTORE); } else pPrevWnd->ShowWindow(SW_NORMAL); pPrevWnd->SetForegroundWindow(); pPrevWnd->GetLastActivePopup()->SetForegroundWindow(); return FALSE; } pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT); } TRACE("Could not find previous instance main window!\r\n"); return FALSE; }
// Activate the Previous Instance of our Application. HWND CInstanceChecker::ActivatePreviousInstance() { //Try to open the previous instances MMF HANDLE hPrevInstance = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, GetMMFFilename()); if (hPrevInstance) { // Open up the MMF int nMMFSize = sizeof(CWindowInstance); CWindowInstance* pInstanceData = (CWindowInstance*) ::MapViewOfFile(hPrevInstance, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, nMMFSize); if (pInstanceData != NULL) //Opening the MMF should work { // Lock the data prior to reading from it CSingleLock dataLock(&m_instanceDataMutex, TRUE); //activate the old window ASSERT(pInstanceData->hMainWnd); //Something gone wrong with the MMF HWND hWindow = pInstanceData->hMainWnd; if (hWindow) { CWnd wndPrev; wndPrev.Attach(hWindow); CWnd* pWndChild = wndPrev.GetLastActivePopup(); // Restore the focus to the previous instance and bring it to the foreground if (wndPrev.IsIconic()) wndPrev.ShowWindow(SW_RESTORE); pWndChild->SetForegroundWindow(); //Detach the CWnd we were using wndPrev.Detach(); } //Unmap the MMF we were using ::UnmapViewOfFile(pInstanceData); //Close the file handle now that we ::CloseHandle(hPrevInstance); //return the Window handle of the previous instance return hWindow; } //Close the file handle now that we ::CloseHandle(hPrevInstance); //When we have activate the previous instance, we can release the lock ReleaseLock(); } return NULL; }
BOOL CMapMgrApp::FirstInstance() { CString strCaption; strCaption.LoadString(AFX_IDS_APP_TITLE); CWnd *pWndFirst = CWnd::FindWindow(NULL,strCaption); if( pWndFirst ) { CWnd *pWndPopup = pWndFirst->GetLastActivePopup(); pWndFirst->BringWindowToTop(); if(pWndFirst->IsIconic()) pWndFirst->ShowWindow(SW_SHOWNORMAL); if(pWndFirst != pWndPopup) pWndPopup->BringWindowToTop(); return FALSE; } return TRUE; }
bool CEtsEodManagerApp::_IsFirstInstance() { CWnd* pWndPrev = CWnd::FindWindow(g_cszMainFrameClassName, NULL); if(pWndPrev) { CWnd* pWndChild = pWndPrev->GetLastActivePopup(); if(pWndPrev->IsIconic()) pWndPrev->ShowWindow(SW_RESTORE); else if(!pWndPrev->IsWindowVisible()) pWndPrev->ShowWindow(SW_SHOW); pWndChild->SetForegroundWindow(); return false; } return true; }
BOOL Cwinlirc::InitInstance() { AfxInitRichEdit(); #ifdef _DEBUG RedirectIOToConsole(); #endif // set current directory for plugins from exe path { //===================== CString fullPath; int indexOfLastSep; //===================== GetModuleFileName(nullptr, fullPath.GetBufferSetLength(MAX_PATH+1), MAX_PATH); indexOfLastSep = fullPath.ReverseFind(_T('\\')); if(!SetCurrentDirectory(fullPath.Left(indexOfLastSep) + _T("\\plugins\\"))) { SetCurrentDirectory(fullPath.Left(indexOfLastSep) + _T("\\")); } } config.readINIFile(); // // command line stuff // if(_tcsstr(m_lpCmdLine,_T("/e")) || _tcsstr(m_lpCmdLine,_T("/E"))) { config.exitOnError = TRUE; } if(_tcsstr(m_lpCmdLine,_T("/t")) || _tcsstr(m_lpCmdLine,_T("/T"))) { config.showTrayIcon = FALSE; } CString mutexName; mutexName.Format(_T("WinLIRC Multiple Instance Lockout_%i"),config.serverPort); if(!CreateMutex(0,FALSE,mutexName) || GetLastError()==ERROR_ALREADY_EXISTS) { //======= HWND tmp; //======= tmp=FindWindow(nullptr,_T("WinLIRC")); if(!tmp) { MessageBox(nullptr,_T("WinLIRC is already running"),_T("WinLIRC"),MB_OK); } else { // bring it to the top //=========== CWnd winlirc; CWnd *last; //=========== winlirc.Attach(tmp); last = winlirc.GetLastActivePopup(); if(!winlirc.IsWindowVisible()) { winlirc.ShowWindow(SW_SHOW); } winlirc.SetForegroundWindow(); last->SetForegroundWindow(); winlirc.Detach(); } return FALSE; } // //Process initialization and sanity checks // if(SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS)==0 || SetThreadPriority(THREAD_PRIORITY_IDLE)==0) { MessageBox(nullptr,_T("Could not set thread priority."),_T("WinLIRC"),MB_OK|MB_ICONERROR); return FALSE; } if(server.startServer()==false) { MessageBox(nullptr,_T("Server could not be started. Try checking the port."),_T("WinLIRC"),MB_OK|MB_ICONERROR); } WL_DEBUG("Creating main dialog...\n"); dlg = new Cdrvdlg(); if(!dlg->Create(IDD_DIALOG,nullptr)) { delete dlg; dlg = nullptr; MessageBox(nullptr,_T("Program exiting."),_T("WinLIRC"),MB_OK|MB_ICONERROR); return FALSE; } dlg->ShowWindow(SW_HIDE); dlg->UpdateWindow(); m_pMainWnd = dlg; return TRUE; }
void CBCGPMessageBox::Initialize () { CWnd* pParent = CWnd::FromHandle (m_Params.hwndOwner); if (pParent == NULL) { pParent = CWnd::GetActiveWindow(); if (pParent != NULL) { CWnd* pPopup = pParent->GetLastActivePopup(); if (pPopup != NULL) { pParent = pPopup; } } } // Loading an icon LPTSTR iconID = NULL; switch (m_Params.dwStyle & MB_ICONMASK) { case MB_ICONASTERISK: iconID = IDI_ASTERISK; break; case MB_ICONEXCLAMATION: iconID = IDI_EXCLAMATION; break; case MB_ICONHAND: iconID = IDI_HAND; break; case MB_ICONQUESTION: iconID = IDI_QUESTION; break; } m_hMessageIcon = NULL; if (iconID) { m_hMessageIcon = ::LoadIcon (NULL, MAKEINTRESOURCE(iconID)); } if (m_Params.dwStyle & MB_USERICON) { m_hMessageIcon = ::LoadIcon (m_Params.hInstance, m_Params.lpszIcon); } m_rectIcon.SetRectEmpty (); if (m_hMessageIcon != NULL) { m_rectIcon.right = ::GetSystemMetrics(SM_CXICON); m_rectIcon.bottom = ::GetSystemMetrics(SM_CYICON); } m_strMessageCaption = GetString (m_Params.lpszCaption, _T("Error")); m_strMessageText = GetString (m_Params.lpszText); m_strCheckBox = GetString (m_Params.lpszCheckBoxText); switch (m_Params.dwStyle & MB_DEFMASK) { case MB_DEFBUTTON1: m_nDefaultButtonIndex = 0; break; case MB_DEFBUTTON2: m_nDefaultButtonIndex = 1; break; case MB_DEFBUTTON3: m_nDefaultButtonIndex = 2; break; case MB_DEFBUTTON4: m_nDefaultButtonIndex = 3; break; default: m_nDefaultButtonIndex = -1; break; } // Creating dialog template m_pTemplate = (DLGTEMPLATE*)::GlobalAlloc (GPTR, 1024); m_pTemplate->x = 0; m_pTemplate->y = 0; m_pTemplate->cx = 62; m_pTemplate->cy = 40; m_pTemplate->cdit = 0; m_pTemplate->style = WS_CAPTION | WS_VISIBLE | WS_SYSMENU | WS_POPUP | DS_MODALFRAME | DS_CENTER; m_pTemplate->dwExtendedStyle = 0; m_bRightAlignment = (m_Params.dwStyle & MB_RIGHT) != 0; if (m_Params.dwStyle & MB_RTLREADING) { //// Uncomment to enable RTL //m_pTemplate->dwExtendedStyle |= WS_EX_LAYOUTRTL; //m_bRightAlignment = !m_bRightAlignment; } if (m_Params.dwStyle & MB_SYSTEMMODAL) { m_pTemplate->style |= DS_SYSMODAL; } WORD* pEnd = (WORD*)(m_pTemplate + 1); *pEnd = 0; InitModalIndirect (m_pTemplate, pParent); // Calculating margins, sizes and spacings m_rectClientMargins.SetRect (7, 7, 5, 7); m_szButton.cx = 52; m_szButton.cy = 14; m_cxIconSpacing = 12; // spacing between an icon and message text m_cyCheckBoxHeight = 10; m_cxButtonSpacing = 4; // gap between buttons m_cyVerticalSpacing = 11; // vertical spacing between text, optional check box and buttons }
BOOL CAccountApp::InitInstance() { // InitCommonControlsEx() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); // Set this to include all the common control classes you want to use // in your application. InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance(); AfxEnableControlContainer(); // Create the shell manager, in case the dialog contains // any shell tree view or shell list view controls. CShellManager *pShellManager = new CShellManager; // 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 // 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")); HANDLE hEvent; UINT cTries = 0; // Create a named event hEvent = CreateEvent(NULL, TRUE, FALSE, _T("兰新高铁网管台帐")); if (hEvent != NULL) { // If the event already existed, that means there's another copy of our app // already running if (GetLastError() == ERROR_ALREADY_EXISTS) { do { // Just in case the other window needs to finish initialization Sleep(cTries ? 250 : 0); // Try to find the other application window CWnd* pwnd = CWnd::FindWindow(NULL,_T("兰新高铁网管台帐")); if(pwnd) { CWnd* pwndLast = pwnd->GetLastActivePopup(); if(!pwndLast) { exit(1); return FALSE; } pwndLast->SetForegroundWindow(); exit(1); return FALSE; } } while (++cTries < 2); // only try twice // If we didn't find the window, the other application was probably // shutting down, so we'll just continue } } getAppCurDir(m_exeFullPath); CAccountDlg dlg; m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Delete the shell manager created above. if (pShellManager != NULL) { delete pShellManager; } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
//Activate the Previous Instance of our Application. HWND CInstanceChecker::ActivatePreviousInstance(LPCTSTR lpCmdLine, ULONG_PTR dwCopyDataItemData, DWORD dwTimeout) { //What will be the return value from this function (assume the worst) HWND hWindow = NULL; //Try to open the previous instances MMF HANDLE hPrevInstance = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, GetMMFFilename()); if (hPrevInstance) { //Open up the MMF int nMMFSize = sizeof(CWindowInstance); CWindowInstance* pInstanceData = static_cast<CWindowInstance*>(MapViewOfFile(hPrevInstance, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, nMMFSize)); if (pInstanceData) //Opening the MMF should work { //Lock the data prior to reading from it CSingleLock dataLock(&m_instanceDataMutex, TRUE); //activate the old window ASSERT(pInstanceData->hMainWnd); //Something gone wrong with the MMF hWindow = pInstanceData->hMainWnd; if (::IsWindow(hWindow)) { CWnd wndPrev; wndPrev.Attach(hWindow); CWnd* pWndChild = wndPrev.GetLastActivePopup(); //Restore the focus to the previous instance and bring it to the foreground if (wndPrev.IsIconic()) wndPrev.ShowWindow(SW_RESTORE); if (pWndChild) pWndChild->SetForegroundWindow(); if (lpCmdLine) { //Send the current apps command line to the previous instance using WM_COPYDATA COPYDATASTRUCT cds; cds.dwData = dwCopyDataItemData; DWORD dwCmdLength = static_cast<DWORD>(_tcslen(lpCmdLine) + 1); cds.cbData = dwCmdLength * sizeof(TCHAR); TCHAR* pszLocalCmdLine = new TCHAR[dwCmdLength]; //We use a local buffer so that we can specify a constant parameter //to this function #if (_MSC_VER >= 1400) _tcscpy_s(pszLocalCmdLine, dwCmdLength, lpCmdLine); #else _tcscpy(pszLocalCmdLine, lpCmdLine); #endif cds.lpData = pszLocalCmdLine; CWnd* pMainWindow = AfxGetMainWnd(); HWND hSender = NULL; if (pMainWindow) hSender = pMainWindow->GetSafeHwnd(); //Send the message to the previous instance. Use SendMessageTimeout instead of SendMessage to ensure we //do not hang if the previous instance itself is hung DWORD_PTR dwResult = 0; if (SendMessageTimeout(hWindow, WM_COPYDATA, reinterpret_cast<WPARAM>(hSender), reinterpret_cast<LPARAM>(&cds), SMTO_ABORTIFHUNG, dwTimeout, &dwResult) == 0) { //Previous instance is not responding to messages hWindow = NULL; } //Tidy up the heap memory we have used delete [] pszLocalCmdLine; } //Detach the CWnd we were using wndPrev.Detach(); } //Unmap the MMF we were using UnmapViewOfFile(pInstanceData); } //Close the file handle now that we CloseHandle(hPrevInstance); //When we have activate the previous instance, we can release the lock ReleaseLock(); } return hWindow; }
BOOL Cwinlirc::InitInstance() { AfxInitRichEdit(); if((m_lpCmdLine[0]=='/' || m_lpCmdLine[0]=='-') && (m_lpCmdLine[1]=='d' || m_lpCmdLine[1]=='D')) debug=true; else debug=false; if(debug) { #ifndef _DEBUG /* see where they want to save it */ CFileDialog fd(FALSE,".txt","wldebug.txt", OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY, "Text Files (*.txt)|*.txt|All Files (*.*)|*.*||", NULL); fd.m_ofn.lpstrTitle="Save debugging information to..."; if(fd.DoModal()==IDCANCEL) { MessageBox(NULL,"Debugging disabled.\n","WinLIRC",MB_OK); debug=false; } else { if((debugfile=fopen(fd.GetPathName(),"w"))==NULL) { MessageBox(NULL,"Could not create debug file;\ndebugging disabled.", "WinLIRC",MB_OK); debug=false; } } #endif } DEBUG("Checking for other instances...\n"); if(!CreateMutex(0,FALSE,"WinLIRC Multiple Instance Lockout") || GetLastError()==ERROR_ALREADY_EXISTS) { HWND tmp; tmp=FindWindow(NULL,"WinLIRC"); if(tmp==NULL) { MessageBox(NULL,"WinLIRC is already running","WinLIRC",MB_OK); } else { // bring it to the top CWnd winlirc; winlirc.Attach(tmp); CWnd *last=winlirc.GetLastActivePopup(); if(!winlirc.IsWindowVisible()) winlirc.ShowWindow(SW_SHOW); winlirc.SetForegroundWindow(); last->SetForegroundWindow(); winlirc.Detach(); } return FALSE; } DEBUG("Setting process and thread priorities...\n"); /* Process initialization and sanity checks */ if(SetPriorityClass(GetCurrentProcess(),REALTIME_PRIORITY_CLASS)==0 || SetThreadPriority(THREAD_PRIORITY_IDLE)==0) { MessageBox(NULL,"Could not set thread priority.","WinLIRC",MB_OK|MB_ICONERROR); return FALSE; } DEBUG("Checking for high-resolution timer...\n"); __int64 x=0; if(QueryPerformanceFrequency((LARGE_INTEGER *)&x)!=TRUE || x==0) { MessageBox(NULL,"This system does not support a high-resolution timer." "Sorry.","WinLIRC",MB_OK|MB_ICONERROR); return FALSE; } DEBUG("Starting server...\n"); server=NULL; if((server=new Cserver)==NULL) { MessageBox(NULL,"Could not create server object.\n","WinLIRC",MB_OK|MB_ICONERROR); if(server!=NULL) { delete server; server=NULL; dlg=NULL; } return FALSE; } if(server->init()==false) { MessageBox(NULL,"Could not start server.","WinLIRC",MB_OK|MB_ICONERROR); if(server!=NULL) { delete server; server=NULL; dlg=NULL; } return FALSE; } DEBUG("Creating main dialog...\n"); if((dlg=new Cdrvdlg)==NULL || dlg->Create(IDD_DIALOG,NULL)==0) { MessageBox(NULL,"Could not create dialog boxes.\n","WinLIRC",MB_OK|MB_ICONERROR); if(dlg!=NULL) { delete dlg; dlg=NULL; } if(server!=NULL) { delete server; server=NULL; dlg=NULL; } return FALSE; } dlg->ShowWindow(SW_HIDE); dlg->UpdateWindow(); m_pMainWnd=dlg; return TRUE; }