1
1
using FreeControl . Utils ;
2
2
using Sunny . UI ;
3
3
using System ;
4
- using System . Diagnostics ;
5
4
using System . Drawing ;
6
- using System . Runtime . InteropServices ;
7
- using System . Timers ;
8
5
using System . Windows . Forms ;
9
6
10
7
namespace FreeControl
11
8
{
12
9
public partial class Controller : UIForm
13
10
{
14
- [ DllImport ( "user32.dll" ) ]
15
- public static extern bool GetWindowRect ( IntPtr hWnd , out Rect lpRect ) ;
16
-
17
- [ StructLayout ( LayoutKind . Sequential ) ]
18
- public struct Rect
19
- {
20
- public int Left ;
21
- public int Top ;
22
- public int Right ;
23
- public int Bottom ;
24
- }
25
-
26
- /// <summary>
27
- /// 监听scrcpy窗体移动后的坐标
28
- /// </summary>
29
- private readonly System . Timers . Timer timer ;
30
- /// <summary>
31
- /// scrcpy句柄
32
- /// </summary>
33
- private IntPtr scrcpyWindow = IntPtr . Zero ;
34
- /// <summary>
35
- /// 进程名称
36
- /// </summary>
37
- private const string scrcpyWindowName = "scrcpy" ;
38
- /// <summary>
39
- /// 上一个有效的scrcpy坐标
40
- /// </summary>
41
- private Rect lastRect = new Rect ( ) ;
42
-
43
11
public Controller ( )
44
12
{
45
13
InitializeComponent ( ) ;
46
14
InitButton ( ) ;
47
15
InitFormSizeAndLocation ( ) ;
48
-
49
- timer = new System . Timers . Timer
50
- {
51
- Interval = 15
52
- } ;
53
- timer . Elapsed += OnTimedEvent ;
54
- timer . Enabled = true ;
55
- Disposed += ( s , e ) =>
56
- {
57
- Main . _Setting . ScrcpyPointX = lastRect . Left ;
58
- Main . _Setting . ScrcpyPointY = lastRect . Top ;
59
- } ;
60
16
}
61
17
62
- public void StopTimer ( )
63
- {
64
- timer . Stop ( ) ;
65
- timer . Enabled = false ;
66
- }
67
-
68
- private void OnTimedEvent ( Object source , ElapsedEventArgs e )
18
+ /// <summary>
19
+ /// 更新控制器位置
20
+ /// </summary>
21
+ public void UpdateLocation ( )
69
22
{
70
23
Action action = ( ) =>
71
24
{
72
- if ( scrcpyWindow == IntPtr . Zero )
73
- {
74
- Process [ ] processes = Process . GetProcessesByName ( scrcpyWindowName ) ;
75
- scrcpyWindow = processes [ 0 ] . MainWindowHandle ;
76
- }
77
- GetWindowRect ( scrcpyWindow , out Rect rect ) ;
78
- if ( rect . Top == lastRect . Top && rect . Left == lastRect . Left )
79
- return ;
80
- if ( rect . Left + rect . Top > 0 )
81
- {
82
- rect . Left += 8 ;
83
- rect . Top += 31 ;
84
- lastRect = rect ;
85
- Location = new Point ( rect . Left - 57 , rect . Top ) ;
86
- }
25
+ // 减去控制器自身默认宽度
26
+ Location = new Point ( Main . _Setting . ScrcpyPointX - 57 , Main . _Setting . ScrcpyPointY ) ;
87
27
} ;
88
- if ( InvokeRequired )
89
- Invoke ( action ) ;
28
+ Invoke ( action ) ;
90
29
}
91
30
92
31
/// <summary>
@@ -108,26 +47,18 @@ void InitFormSizeAndLocation()
108
47
}
109
48
if ( Main . _Setting . ScrcpyPointX + Main . _Setting . ScrcpyPointY > 0 )
110
49
{
111
- // Location = new Point(Main._Setting.ControllerStartPointX, Main._Setting.ControllerStartPointY);
112
50
StartPosition = FormStartPosition . Manual ;
113
51
Location = new Point ( Main . _Setting . ScrcpyPointX - 57 , Main . _Setting . ScrcpyPointY ) ;
114
52
}
115
53
else
116
54
{
117
55
StartPosition = FormStartPosition . CenterScreen ;
118
56
}
119
- // LocationChanged += (sender, e) =>
120
- // {
121
- // Main._Setting.ControllerStartPointX = Location.X;
122
- // Main._Setting.ControllerStartPointY = Location.Y;
123
- // };
124
57
SizeChanged += ( sender , e ) =>
125
58
{
126
59
Main . _Setting . ControllerStartWidth = Width ;
127
60
Main . _Setting . ControllerStartHeight = Height ;
128
61
} ;
129
- // MouseDown += (s, e) => DragWindow();
130
- // flowPanel.FlowLayoutPanel.MouseDown += (s, e) => DragWindow();
131
62
}
132
63
133
64
/// <summary>
@@ -159,24 +90,5 @@ void InitButton()
159
90
}
160
91
}
161
92
}
162
-
163
- #region 拖动窗口
164
- [ DllImport ( "user32.dll" ) ] //拖动无窗体的控件
165
- public static extern bool ReleaseCapture ( ) ;
166
- [ DllImport ( "user32.dll" ) ]
167
- public static extern bool SendMessage ( IntPtr hwnd , int wMsg , int wParam , int lParam ) ;
168
- public const int WM_SYSCOMMAND = 0x0112 ;
169
- public const int SC_MOVE = 0xF010 ;
170
- public const int HTCAPTION = 0x0002 ;
171
-
172
- /// <summary>
173
- /// 拖动窗体
174
- /// </summary>
175
- public void DragWindow ( )
176
- {
177
- ReleaseCapture ( ) ;
178
- SendMessage ( this . Handle , WM_SYSCOMMAND , SC_MOVE + HTCAPTION , 0 ) ;
179
- }
180
- #endregion
181
93
}
182
94
}
0 commit comments