Skip to content

Commit

Permalink
Modern: Bring windows to foreground, fix opening from context menu ca…
Browse files Browse the repository at this point in the history
…use window in background (#548)
  • Loading branch information
R-YaTian authored Jan 18, 2025
1 parent e3e8871 commit 5e042a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NanaZip.UI.Modern/MainWindowToolBarPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ namespace winrt::NanaZip::Modern::implementation
return;
}

AllowSetForegroundWindow(GetProcessId(ProcessInformation.hProcess));

::CloseHandle(ProcessInformation.hThread);
::WaitForSingleObjectEx(ProcessInformation.hProcess, INFINITE, FALSE);
::CloseHandle(ProcessInformation.hProcess);
Expand Down
32 changes: 32 additions & 0 deletions NanaZip.UI.Modern/SevenZip/CPP/Windows/ProcessUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ static UString GetQuotedString(const UString &s)
}
#endif

// **************** NanaZip Modification Start ****************
static BOOL CALLBACK BringToForeground(HWND hwnd, LPARAM lParam)
{
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);

if (processId == (DWORD) lParam) {
HWND hCurWnd = ::GetForegroundWindow();
DWORD dwMyID = ::GetCurrentThreadId();
DWORD dwCurID = ::GetWindowThreadProcessId(hCurWnd, NULL);
::AttachThreadInput(dwCurID, dwMyID, TRUE);
::SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
::SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
::SetForegroundWindow(hwnd);
::SetFocus(hwnd);
::SetActiveWindow(hwnd);
::AttachThreadInput(dwCurID, dwMyID, FALSE);

return FALSE;
}

return TRUE;
}
// **************** NanaZip Modification End ****************

WRes CProcess::Create(LPCWSTR imageName, const UString &params, LPCWSTR curDir)
{
/*
Expand Down Expand Up @@ -88,6 +113,13 @@ WRes CProcess::Create(LPCWSTR imageName, const UString &params, LPCWSTR curDir)
}
if (result == 0)
return ::GetLastError();

// **************** NanaZip Modification Start ****************
AllowSetForegroundWindow(GetProcessId(pi.hProcess));
::WaitForInputIdle(pi.hProcess, INFINITE);
EnumWindows(BringToForeground, (LPARAM) GetProcessId(pi.hProcess));
// **************** NanaZip Modification End ****************

::CloseHandle(pi.hThread);
_handle = pi.hProcess;
return 0;
Expand Down

0 comments on commit 5e042a7

Please sign in to comment.