HWND CConEmuInside::InsideFindConEmu(HWND hFrom) { wchar_t szClass[128]; HWND hChild = NULL, hNext = NULL; //HWND hXpView = NULL, hXpPlace = NULL; while ((hChild = FindWindowEx(hFrom, hChild, NULL, NULL)) != NULL) { GetClassName(hChild, szClass, countof(szClass)); if (lstrcmp(szClass, gsClassNameParent) == 0) { return hChild; } hNext = InsideFindConEmu(hChild); if (hNext) { return hNext; } } return NULL; }
// Вызывается для инициализации из Settings::LoadSettings() HWND CConEmuInside::InsideFindParent() { bool bFirstStep = true; DWORD nParentPID = 0; if (!m_InsideIntegration) { return NULL; } if (mh_InsideParentWND) { if (IsWindow(mh_InsideParentWND)) { if (m_InsideIntegration == ii_Simple) { if (mh_InsideParentRoot == NULL) { // Если еще не искали "корневое" окно HWND hParent = mh_InsideParentWND; while (hParent) { mh_InsideParentRoot = hParent; hParent = GetParent(hParent); } } // В этом режиме занимаем всю клиентскую область _ASSERTE(mh_InsideParentRel==NULL); mh_InsideParentRel = NULL; } _ASSERTE(mh_InsideParentWND!=NULL); goto wrap; } else { if (m_InsideIntegration == ii_Simple) { DisplayLastError(L"Specified window not found"); mh_InsideParentWND = NULL; goto wrap; } _ASSERTE(IsWindow(mh_InsideParentWND)); mh_InsideParentRoot = mh_InsideParentWND = mh_InsideParentRel = NULL; } } _ASSERTE(m_InsideIntegration!=ii_Simple); if (mn_InsideParentPID) { PROCESSENTRY32 pi = {sizeof(pi)}; if ((mn_InsideParentPID == GetCurrentProcessId()) || !GetProcessInfo(mn_InsideParentPID, &pi)) { DisplayLastError(L"Invalid parent process specified"); m_InsideIntegration = ii_None; mh_InsideParentWND = NULL; goto wrap; } nParentPID = mn_InsideParentPID; } else { PROCESSENTRY32 pi = {sizeof(pi)}; if (!GetProcessInfo(GetCurrentProcessId(), &pi) || !pi.th32ParentProcessID) { DisplayLastError(L"GetProcessInfo(GetCurrentProcessId()) failed"); m_InsideIntegration = ii_None; mh_InsideParentWND = NULL; goto wrap; } nParentPID = pi.th32ParentProcessID; } EnumWindows(EnumInsideFindParent, nParentPID); if (!mh_InsideParentRoot) { int nBtn = MsgBox(L"Can't find appropriate parent window!\n\nContinue in normal mode?", MB_ICONSTOP|MB_YESNO|MB_DEFBUTTON2); if (nBtn != IDYES) { mh_InsideParentWND = INSIDE_PARENT_NOT_FOUND; return mh_InsideParentWND; // Закрыться! } // Продолжить в обычном режиме m_InsideIntegration = ii_None; mh_InsideParentWND = NULL; goto wrap; } HWND hExistConEmu; if ((hExistConEmu = InsideFindConEmu(mh_InsideParentRoot)) != NULL) { _ASSERTE(FALSE && "Continue to create tab in existing instance"); // Если в проводнике уже есть ConEmu - открыть в нем новую вкладку gpSetCls->SingleInstanceShowHide = sih_None; LPCWSTR pszCmdLine = GetCommandLine(); LPCWSTR pszCmd = StrStrI(pszCmdLine, L" /cmd "); gpConEmu->RunSingleInstance(hExistConEmu, pszCmd ? (pszCmd + 6) : NULL); mh_InsideParentWND = INSIDE_PARENT_NOT_FOUND; return mh_InsideParentWND; // Закрыться! } // Теперь нужно найти дочерние окна // 1. в которое будем внедряться // 2. по которому будем позиционироваться // 3. для синхронизации текущего пути InsideFindShellView(mh_InsideParentRoot); RepeatCheck: if (!mh_InsideParentWND || (!mh_InsideParentRel && (m_InsideIntegration == ii_Explorer))) { wchar_t szAddMsg[128] = L"", szMsg[1024]; if (bFirstStep) { bFirstStep = false; if (TurnExplorerTipPane(szAddMsg)) { goto RepeatCheck; } } //MessageBox(L"Can't find appropriate shell window!", MB_ICONSTOP); _wsprintf(szMsg, SKIPLEN(countof(szMsg)) L"%sCan't find appropriate shell window!\nUnrecognized layout of the Explorer.\n\nContinue in normal mode?", szAddMsg); int nBtn = MsgBox(szMsg, MB_ICONSTOP|MB_YESNO|MB_DEFBUTTON2); if (nBtn != IDYES) { mh_InsideParentWND = INSIDE_PARENT_NOT_FOUND; return mh_InsideParentWND; // Закрыться! } m_InsideIntegration = ii_None; mh_InsideParentRoot = NULL; mh_InsideParentWND = NULL; goto wrap; } wrap: if (!mh_InsideParentWND) { m_InsideIntegration = ii_None; mh_InsideParentRoot = NULL; } else { GetWindowThreadProcessId(mh_InsideParentWND, &mn_InsideParentPID); // Для мониторинга папки GetCurrentDirectory(countof(ms_InsideParentPath), ms_InsideParentPath); int nLen = lstrlen(ms_InsideParentPath); if ((nLen > 3) && (ms_InsideParentPath[nLen-1] == L'\\')) { ms_InsideParentPath[nLen-1] = 0; } } return mh_InsideParentWND; }
// Вызывается для инициализации из Settings::LoadSettings() HWND CConEmuInside::InsideFindParent() { bool bFirstStep = true; DWORD nParentPID = 0; PROCESSENTRY32 pi = {sizeof(pi)}; EnumFindParentArg find = {}; if (!m_InsideIntegration) { return NULL; } if (mh_InsideParentWND) { if (IsWindow(mh_InsideParentWND)) { if (m_InsideIntegration == ii_Simple) { // We cover all client area of mh_InsideParentWND in this mode _ASSERTE(mh_InsideParentRel==NULL); mh_InsideParentRel = NULL; } _ASSERTE(mh_InsideParentWND!=NULL); goto wrap; } else { if (m_InsideIntegration == ii_Simple) { DisplayLastError(L"Specified window not found"); SetInsideParentWND(NULL); goto wrap; } _ASSERTE(IsWindow(mh_InsideParentWND)); SetInsideParentWND(mh_InsideParentRel = NULL); } } _ASSERTE(m_InsideIntegration!=ii_Simple); if (mn_InsideParentPID) { if ((mn_InsideParentPID == GetCurrentProcessId()) || !GetProcessInfo(mn_InsideParentPID, &pi)) { DisplayLastError(L"Invalid parent process specified"); m_InsideIntegration = ii_None; SetInsideParentWND(NULL); goto wrap; } nParentPID = mn_InsideParentPID; } else { PROCESSENTRY32 pi = {sizeof(pi)}; if (!GetProcessInfo(GetCurrentProcessId(), &pi) || !pi.th32ParentProcessID) { DisplayLastError(L"GetProcessInfo(GetCurrentProcessId()) failed"); m_InsideIntegration = ii_None; SetInsideParentWND(NULL); goto wrap; } nParentPID = pi.th32ParentProcessID; } // Do window enumeration find.nPID = nParentPID; ::EnumWindows(EnumInsideFindParent, (LPARAM)&find); if (!find.hParentRoot) { int nBtn = MsgBox(L"Can't find appropriate parent window!\n\nContinue in normal mode?", MB_ICONSTOP|MB_YESNO|MB_DEFBUTTON2); if (nBtn != IDYES) { SetInsideParentWND(INSIDE_PARENT_NOT_FOUND); return mh_InsideParentWND; // Закрыться! } // Продолжить в обычном режиме m_InsideIntegration = ii_None; SetInsideParentWND(NULL); goto wrap; } mh_InitialRoot = find.hParentRoot; mn_InsideParentPID = nParentPID; HWND hExistConEmu; if ((hExistConEmu = InsideFindConEmu(find.hParentRoot)) != NULL) { _ASSERTE(FALSE && "Continue to create tab in existing instance"); // Если в проводнике уже есть ConEmu - открыть в нем новую вкладку gpSetCls->SingleInstanceShowHide = sih_None; LPCWSTR pszCmdLine = GetCommandLine(); CEStr lsArg; LPCWSTR pszCmd = pszCmdLine; while (0 == NextArg(&pszCmd, lsArg)) { if (lsArg.OneOfSwitches(L"-runlist",L"-cmdlist")) { pszCmd = NULL; break; } else if (lsArg.OneOfSwitches(L"-run",L"-cmd")) { break; } } gpConEmu->RunSingleInstance(hExistConEmu, (pszCmd && *pszCmd) ? (pszCmd) : NULL); SetInsideParentWND(INSIDE_PARENT_NOT_FOUND); return mh_InsideParentWND; // Закрыться! } // Теперь нужно найти дочерние окна // 1. в которое будем внедряться // 2. по которому будем позиционироваться // 3. для синхронизации текущего пути InsideFindShellView(find.hParentRoot); RepeatCheck: if (!isInsideWndSet() || (!mh_InsideParentRel && (m_InsideIntegration == ii_Explorer))) { wchar_t szAddMsg[128] = L"", szMsg[1024]; if (bFirstStep) { bFirstStep = false; if (TurnExplorerTipPane(szAddMsg)) { goto RepeatCheck; } } _wsprintf(szMsg, SKIPLEN(countof(szMsg)) L"%sCan't find appropriate shell window!\nUnrecognized layout of the Explorer.\n\nContinue in normal mode?", szAddMsg); int nBtn = MsgBox(szMsg, MB_ICONSTOP|MB_YESNO|MB_DEFBUTTON2); if (nBtn != IDYES) { SetInsideParentWND(INSIDE_PARENT_NOT_FOUND); return mh_InsideParentWND; // Закрыться! } m_InsideIntegration = ii_None; SetInsideParentWND(NULL); goto wrap; } wrap: if (!isInsideWndSet()) { m_InsideIntegration = ii_None; } else { GetWindowThreadProcessId(mh_InsideParentWND, &mn_InsideParentPID); // Для мониторинга папки GetCurrentDirectory(countof(ms_InsideParentPath), ms_InsideParentPath); int nLen = lstrlen(ms_InsideParentPath); if ((nLen > 3) && (ms_InsideParentPath[nLen-1] == L'\\')) { ms_InsideParentPath[nLen-1] = 0; } } return mh_InsideParentWND; }