4
4
using System . Windows ;
5
5
using System . Windows . Forms ;
6
6
using _1RM . Model . Protocol ;
7
+ using _1RM . View . Host . ProtocolHosts ;
7
8
using Shawn . Utils ;
8
9
using Stylet ;
9
10
using ProtocolHostType = _1RM . View . Host . ProtocolHosts . ProtocolHostType ;
@@ -35,12 +36,13 @@ private void TimerDispose()
35
36
}
36
37
37
38
private IntPtr _lastActivatedWindowHandle = IntPtr . Zero ;
39
+
38
40
private void Timer4CheckForegroundWindowOnElapsed ( object ? sender , ElapsedEventArgs e )
39
41
{
40
42
_timer4CheckForegroundWindow . Stop ( ) ;
41
43
try
42
44
{
43
- RunForRdp ( ) ;
45
+ RunForRdpV2 ( ) ;
44
46
RunForIntegrate ( ) ;
45
47
}
46
48
catch ( Exception ex )
@@ -59,6 +61,7 @@ private void Timer4CheckForegroundWindowOnElapsed(object? sender, ElapsedEventAr
59
61
60
62
[ DllImport ( "user32.dll" ) ]
61
63
private static extern IntPtr GetForegroundWindow ( ) ;
64
+
62
65
[ DllImport ( "user32.dll" ) ]
63
66
private static extern bool SetForegroundWindow ( IntPtr hWnd ) ;
64
67
@@ -89,12 +92,13 @@ private void RunForIntegrate()
89
92
_lastActivatedWindowHandle = nowActivatedWindowHandle ;
90
93
}
91
94
92
- /****
95
+ /****
93
96
* THE PURPOSE OF THIS FUNCTION IS TO:
94
97
* - LET YOUR LOCAL DESKTOP WINDOW GET FOCUS WHEN YOU MOVE THE CURSOR OUT OF THE RDP WINDOW
95
98
* - LET THE RDP WINDOW GET FOCUS WHEN YOU MOVE THE CURSOR INTO THE RDP WINDOW
96
99
* - CAUTION: PAY ATTENTION TO THE RESIZE OF THE RDP WINDOW, IT MAY CAUSE THE CURSOR TO MOVE OUT OF THE RDP WINDOW, SO WE NEED TO CHECK IF THE LEFT MOUSE BUTTON IS PRESSED OR NOT
97
- ***/
100
+ ***/
101
+
98
102
#region RunForRdp
99
103
100
104
[ StructLayout ( LayoutKind . Sequential ) ]
@@ -103,15 +107,18 @@ internal struct Win32Point
103
107
public Int32 X ;
104
108
public Int32 Y ;
105
109
} ;
110
+
106
111
[ DllImport ( "user32.dll" ) ]
107
112
[ return : MarshalAs ( UnmanagedType . Bool ) ]
108
113
internal static extern bool GetCursorPos ( ref Win32Point pt ) ;
114
+
109
115
private static Point GetMousePosition ( )
110
116
{
111
117
var w32Mouse = new Win32Point ( ) ;
112
118
GetCursorPos ( ref w32Mouse ) ;
113
119
return new Point ( w32Mouse . X , w32Mouse . Y ) ;
114
120
}
121
+
115
122
[ DllImport ( "user32.dll" ) ]
116
123
private static extern IntPtr GetDesktopWindow ( ) ;
117
124
@@ -143,17 +150,17 @@ private void RunForRdp()
143
150
if ( Vm ? . SelectedItem ? . Content ? . Status != ProtocolHosts . ProtocolHostStatus . Connected )
144
151
return ;
145
152
146
- // Fix the resizing bug introduced by #648, see https://github.com/1Remote/1Remote/issues/797 for more details
147
- bool isMousePressed = System . Windows . Forms . Control . MouseButtons == MouseButtons . Left
148
- || System . Windows . Forms . Control . MouseButtons == MouseButtons . Right
149
- || System . Windows . Forms . Control . MouseButtons == MouseButtons . Middle ;
150
- if ( isMousePressed )
151
- {
153
+ // Fix the resizing bug introduced by #648, see https://github.com/1Remote/1Remote/issues/797 for more details
154
+ bool isMousePressed = System . Windows . Forms . Control . MouseButtons == MouseButtons . Left
155
+ || System . Windows . Forms . Control . MouseButtons == MouseButtons . Right
156
+ || System . Windows . Forms . Control . MouseButtons == MouseButtons . Middle ;
157
+ if ( isMousePressed )
158
+ {
152
159
#if DEBUG
153
- SimpleLogHelper . Debug ( "Tab focus: Mouse is pressed, do nothing" ) ;
160
+ SimpleLogHelper . Debug ( "Tab focus: Mouse is pressed, do nothing" ) ;
154
161
#endif
155
- return ;
156
- }
162
+ return ;
163
+ }
157
164
158
165
var nowActivatedWindowHandle = GetForegroundWindow ( ) ;
159
166
var desktopHandle = GetDesktopWindow ( ) ;
@@ -194,6 +201,56 @@ private void RunForRdp()
194
201
_rdpStage = 1 ;
195
202
}
196
203
}
197
- #endregion
204
+
205
+
206
+ private void RunForRdpV2 ( )
207
+ {
208
+ if ( Vm ? . SelectedItem ? . Content ? . ProtocolServer . Protocol != RDP . ProtocolName )
209
+ return ;
210
+ //if (Vm?.SelectedItem?.Content is not IntegrateHostForWinFrom ihfw)
211
+ // return;
212
+ if ( Vm ? . SelectedItem ? . Content ? . Status != ProtocolHosts . ProtocolHostStatus . Connected )
213
+ return ;
214
+
215
+ // Fix the resizing bug introduced by #648, see https://github.com/1Remote/1Remote/issues/797 for more details
216
+ bool isMousePressed = System . Windows . Forms . Control . MouseButtons == MouseButtons . Left
217
+ || System . Windows . Forms . Control . MouseButtons == MouseButtons . Right
218
+ || System . Windows . Forms . Control . MouseButtons == MouseButtons . Middle ;
219
+ if ( isMousePressed )
220
+ {
221
+ //SimpleLogHelper.Debug("Tab focus: Mouse is pressed, do nothing");
222
+ return ;
223
+ }
224
+
225
+ var nowActivatedWindowHandle = GetForegroundWindow ( ) ;
226
+ var desktopHandle = GetDesktopWindow ( ) ;
227
+ IntPtr rdpHandle = IntPtr . Zero ;
228
+ if ( Vm ? . SelectedItem ? . Content is AxMsRdpClient09Host rdpHost )
229
+ {
230
+ rdpHandle = _myHandle ;
231
+ }
232
+ else
233
+ {
234
+ //rdpHandle = ihfw.GetHostHwnd();
235
+ throw new NotImplementedException ( ) ;
236
+ }
237
+
238
+ bool isMouseInside = IsMouseInside ( this ) ;
239
+ //#if DEBUG
240
+ // SimpleLogHelper.Debug($"Tab focus: isMouseInside = {isMouseInside}, rdpHandle = {rdpHandle}, nowActivatedWindowHandle = {nowActivatedWindowHandle}, desktopHandle = {desktopHandle}");
241
+ //#endif
242
+ if ( ! isMouseInside && rdpHandle == nowActivatedWindowHandle )
243
+ {
244
+ // 1 - RDP has focus AND mouse is not inside the tab window, then switch focus to desktop, user input will not be sent to RDP
245
+ SetForegroundWindow ( desktopHandle ) ;
246
+ }
247
+ else if ( isMouseInside && ( nowActivatedWindowHandle == desktopHandle || nowActivatedWindowHandle == IntPtr . Zero ) )
248
+ {
249
+ // 2 - desktop has focus
250
+ SetForegroundWindow ( rdpHandle ) ;
251
+ }
252
+
253
+ #endregion
254
+ }
198
255
}
199
256
}
0 commit comments