@@ -9,19 +9,20 @@ use windows::Win32::{
9
9
Gdi :: { GetMonitorInfoW , MonitorFromPoint , MONITORINFO , MONITOR_DEFAULTTONEAREST } ,
10
10
} ,
11
11
System :: {
12
- Console :: { AllocConsole , FreeConsole , GetConsoleWindow } ,
13
12
LibraryLoader :: GetModuleFileNameW ,
14
13
Threading :: {
15
14
OpenProcess , QueryFullProcessImageNameW , PROCESS_NAME_WIN32 , PROCESS_QUERY_INFORMATION ,
16
15
PROCESS_VM_READ ,
17
16
} ,
18
17
} ,
19
- UI :: WindowsAndMessaging :: {
20
- EnumWindows , GetCursorPos , GetForegroundWindow , GetWindow , GetWindowLongPtrW ,
21
- GetWindowPlacement , GetWindowTextW , GetWindowThreadProcessId , IsIconic ,
22
- SetForegroundWindow , SetWindowPos , ShowWindow , GWL_EXSTYLE , GWL_STYLE , GWL_USERDATA ,
23
- GW_OWNER , SWP_NOZORDER , SW_RESTORE , WINDOWPLACEMENT , WS_EX_TOOLWINDOW , WS_ICONIC ,
24
- WS_VISIBLE ,
18
+ UI :: {
19
+ Input :: KeyboardAndMouse :: { SendInput , INPUT , INPUT_MOUSE } ,
20
+ WindowsAndMessaging :: {
21
+ EnumWindows , GetCursorPos , GetForegroundWindow , GetWindow , GetWindowLongPtrW ,
22
+ GetWindowPlacement , GetWindowTextW , GetWindowThreadProcessId , IsIconic ,
23
+ SetForegroundWindow , ShowWindow , GWL_EXSTYLE , GWL_STYLE , GWL_USERDATA , GW_OWNER ,
24
+ SW_RESTORE , WINDOWPLACEMENT , WS_EX_TOOLWINDOW , WS_ICONIC , WS_VISIBLE ,
25
+ } ,
25
26
} ,
26
27
} ;
27
28
@@ -146,24 +147,24 @@ pub fn get_window_exe(hwnd: HWND) -> Option<String> {
146
147
return None ;
147
148
}
148
149
let module_path = get_module_path ( pid) ?;
149
- module_path. split ( '\\' ) . map ( |v| v. to_string ( ) ) . last ( )
150
+ module_path. split ( '\\' ) . map ( |v| v. to_string ( ) ) . next_back ( )
150
151
}
151
152
152
153
pub fn set_foreground_window ( hwnd : HWND ) {
154
+ // ref https://github.com/microsoft/PowerToys/blob/4cb72ee126caf1f720c507f6a1dbe658cd515366/src/modules/fancyzones/FancyZonesLib/WindowUtils.cpp#L191
153
155
unsafe {
154
156
if is_iconic_window ( hwnd) {
155
157
let _ = ShowWindow ( hwnd, SW_RESTORE ) ;
156
158
}
157
- if hwnd == get_foreground_window ( ) {
158
- return ;
159
- }
160
- if SetForegroundWindow ( hwnd) . ok ( ) . is_err ( ) {
161
- let _ = AllocConsole ( ) ;
162
- let hwnd_console = GetConsoleWindow ( ) ;
163
- let _ = SetWindowPos ( hwnd_console, None , 0 , 0 , 0 , 0 , SWP_NOZORDER ) ;
164
- let _ = FreeConsole ( ) ;
165
- let _ = SetForegroundWindow ( hwnd) ;
166
- }
159
+
160
+ let input = INPUT {
161
+ r#type : INPUT_MOUSE ,
162
+ ..Default :: default ( )
163
+ } ;
164
+
165
+ SendInput ( & [ input] , std:: mem:: size_of :: < INPUT > ( ) as i32 ) ;
166
+
167
+ let _ = SetForegroundWindow ( hwnd) ;
167
168
} ;
168
169
}
169
170
0 commit comments