Skip to content

Commit 28e720a

Browse files
committed
fix: try fix #827
1 parent d0a11a4 commit 28e720a

File tree

1 file changed

+70
-13
lines changed

1 file changed

+70
-13
lines changed

Ui/View/Host/TabWindowView.xaml_timer.cs

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Windows;
55
using System.Windows.Forms;
66
using _1RM.Model.Protocol;
7+
using _1RM.View.Host.ProtocolHosts;
78
using Shawn.Utils;
89
using Stylet;
910
using ProtocolHostType = _1RM.View.Host.ProtocolHosts.ProtocolHostType;
@@ -35,12 +36,13 @@ private void TimerDispose()
3536
}
3637

3738
private IntPtr _lastActivatedWindowHandle = IntPtr.Zero;
39+
3840
private void Timer4CheckForegroundWindowOnElapsed(object? sender, ElapsedEventArgs e)
3941
{
4042
_timer4CheckForegroundWindow.Stop();
4143
try
4244
{
43-
RunForRdp();
45+
RunForRdpV2();
4446
RunForIntegrate();
4547
}
4648
catch (Exception ex)
@@ -59,6 +61,7 @@ private void Timer4CheckForegroundWindowOnElapsed(object? sender, ElapsedEventAr
5961

6062
[DllImport("user32.dll")]
6163
private static extern IntPtr GetForegroundWindow();
64+
6265
[DllImport("user32.dll")]
6366
private static extern bool SetForegroundWindow(IntPtr hWnd);
6467

@@ -89,12 +92,13 @@ private void RunForIntegrate()
8992
_lastActivatedWindowHandle = nowActivatedWindowHandle;
9093
}
9194

92-
/****
95+
/****
9396
* THE PURPOSE OF THIS FUNCTION IS TO:
9497
* - LET YOUR LOCAL DESKTOP WINDOW GET FOCUS WHEN YOU MOVE THE CURSOR OUT OF THE RDP WINDOW
9598
* - LET THE RDP WINDOW GET FOCUS WHEN YOU MOVE THE CURSOR INTO THE RDP WINDOW
9699
* - 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+
98102
#region RunForRdp
99103

100104
[StructLayout(LayoutKind.Sequential)]
@@ -103,15 +107,18 @@ internal struct Win32Point
103107
public Int32 X;
104108
public Int32 Y;
105109
};
110+
106111
[DllImport("user32.dll")]
107112
[return: MarshalAs(UnmanagedType.Bool)]
108113
internal static extern bool GetCursorPos(ref Win32Point pt);
114+
109115
private static Point GetMousePosition()
110116
{
111117
var w32Mouse = new Win32Point();
112118
GetCursorPos(ref w32Mouse);
113119
return new Point(w32Mouse.X, w32Mouse.Y);
114120
}
121+
115122
[DllImport("user32.dll")]
116123
private static extern IntPtr GetDesktopWindow();
117124

@@ -143,17 +150,17 @@ private void RunForRdp()
143150
if (Vm?.SelectedItem?.Content?.Status != ProtocolHosts.ProtocolHostStatus.Connected)
144151
return;
145152

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+
{
152159
#if DEBUG
153-
SimpleLogHelper.Debug("Tab focus: Mouse is pressed, do nothing");
160+
SimpleLogHelper.Debug("Tab focus: Mouse is pressed, do nothing");
154161
#endif
155-
return;
156-
}
162+
return;
163+
}
157164

158165
var nowActivatedWindowHandle = GetForegroundWindow();
159166
var desktopHandle = GetDesktopWindow();
@@ -194,6 +201,56 @@ private void RunForRdp()
194201
_rdpStage = 1;
195202
}
196203
}
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+
}
198255
}
199256
}

0 commit comments

Comments
 (0)