Skip to content

Commit da38c58

Browse files
authored
Fixed bringToTop bug for Windows (#39)
* Fixed bringToFront bug for Windows * Reassign b to result from SetWindowForeground()
1 parent 8aabc0a commit da38c58

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/windows.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,20 @@ Napi::Boolean showWindow (const Napi::CallbackInfo& info) {
258258

259259
Napi::Boolean bringWindowToTop (const Napi::CallbackInfo& info) {
260260
Napi::Env env{ info.Env () };
261-
262261
auto handle{ getValueFromCallbackData<HWND> (info, 0) };
263262
BOOL b{ SetForegroundWindow (handle) };
264-
SetActiveWindow (handle);
265-
SetWindowPos (handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE);
263+
264+
HWND hCurWnd = ::GetForegroundWindow();
265+
DWORD dwMyID = ::GetCurrentThreadId();
266+
DWORD dwCurID = ::GetWindowThreadProcessId(hCurWnd, NULL);
267+
::AttachThreadInput(dwCurID, dwMyID, TRUE);
268+
::SetWindowPos(handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
269+
::SetWindowPos(handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
270+
::SetForegroundWindow(handle);
271+
::AttachThreadInput(dwCurID, dwMyID, FALSE);
272+
::SetFocus(handle);
273+
b = ::SetActiveWindow(handle);
274+
266275

267276
return Napi::Boolean::New (env, b);
268277
}

0 commit comments

Comments
 (0)