VeraCrypt
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMounir IDRASSI <mounir.idrassi@idrix.fr>2018-03-25 18:39:40 (GMT)
committerMounir IDRASSI <mounir.idrassi@idrix.fr>2018-03-25 20:00:27 (GMT)
commitb2069885313881a177798375160256879c7b796c (patch)
treea8ebe2eec1307406d99eb9304d4b797f843ae71f
parente412c0458b9df6bb1f2482823d397acc2a656464 (diff)
downloadVeraCrypt-b2069885313881a177798375160256879c7b796c.zip
VeraCrypt-b2069885313881a177798375160256879c7b796c.tar.gz
Windows: use cleaner approach to implement bringing our windows to foreground. The previous implementation was causing issues with other application, like random freezing.
-rw-r--r--src/Common/Dlgcode.c37
1 files changed, 23 insertions, 14 deletions
diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c
index 112328b..4f0c4af 100644
--- a/src/Common/Dlgcode.c
+++ b/src/Common/Dlgcode.c
@@ -7577,33 +7577,42 @@ BOOL CALLBACK WaitDlgProc (HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
}
-void BringToForeground(HWND hWnd)
+// Based on source: https://www.codeproject.com/Tips/76427/How-to-bring-window-to-top-with-SetForegroundWindo?msg=5285754#xx5285754xx
+void BringToForeground (HWND hWnd)
{
if(!::IsWindow(hWnd)) return;
-
- DWORD lockTimeOut = 0;
HWND hCurrWnd = ::GetForegroundWindow();
DWORD dwThisTID = ::GetCurrentThreadId(),
dwCurrTID = ::GetWindowThreadProcessId(hCurrWnd,0);
-
+ // This structure will be used to create the keyboard
+ // input event.
+ INPUT ip;
+
if (hCurrWnd != hWnd)
{
if(dwThisTID != dwCurrTID)
{
- ::AttachThreadInput(dwThisTID, dwCurrTID, TRUE);
-
- ::SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT,0,&lockTimeOut,0);
- ::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT,0,0,SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
-
- ::AllowSetForegroundWindow(ASFW_ANY);
+ // Set up a generic keyboard event.
+ ip.type = INPUT_KEYBOARD;
+ ip.ki.wScan = 0; // hardware scan code for key
+ ip.ki.time = 0;
+ ip.ki.dwExtraInfo = 0;
+
+ // Press the "A" key
+ ip.ki.wVk = VK_MENU; // virtual-key code for the "a" key
+ ip.ki.dwFlags = 0; // 0 for key press
+ SendInput(1, &ip, sizeof(INPUT));
+
+ ::Sleep(250); //Sometimes SetForegroundWindow will fail and the window will flash instead of it being show. Sleeping for a bit seems to help.
}
-
+
::SetForegroundWindow(hWnd);
-
+
if(dwThisTID != dwCurrTID)
{
- ::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT,0,(PVOID)lockTimeOut,SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
- ::AttachThreadInput(dwThisTID, dwCurrTID, FALSE);
+ // Release the "A" key
+ ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
+ SendInput(1, &ip, sizeof(INPUT));
}
}