11using FreeControl . Utils ;
22using Sunny . UI ;
33using System ;
4- using System . Diagnostics ;
54using System . Drawing ;
6- using System . Runtime . InteropServices ;
7- using System . Timers ;
85using System . Windows . Forms ;
96
107namespace FreeControl
118{
129 public partial class Controller : UIForm
1310 {
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-
4311 public Controller ( )
4412 {
4513 InitializeComponent ( ) ;
4614 InitButton ( ) ;
4715 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- } ;
6016 }
6117
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 ( )
6922 {
7023 Action action = ( ) =>
7124 {
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 ) ;
8727 } ;
88- if ( InvokeRequired )
89- Invoke ( action ) ;
28+ Invoke ( action ) ;
9029 }
9130
9231 /// <summary>
@@ -108,26 +47,18 @@ void InitFormSizeAndLocation()
10847 }
10948 if ( Main . _Setting . ScrcpyPointX + Main . _Setting . ScrcpyPointY > 0 )
11049 {
111- // Location = new Point(Main._Setting.ControllerStartPointX, Main._Setting.ControllerStartPointY);
11250 StartPosition = FormStartPosition . Manual ;
11351 Location = new Point ( Main . _Setting . ScrcpyPointX - 57 , Main . _Setting . ScrcpyPointY ) ;
11452 }
11553 else
11654 {
11755 StartPosition = FormStartPosition . CenterScreen ;
11856 }
119- // LocationChanged += (sender, e) =>
120- // {
121- // Main._Setting.ControllerStartPointX = Location.X;
122- // Main._Setting.ControllerStartPointY = Location.Y;
123- // };
12457 SizeChanged += ( sender , e ) =>
12558 {
12659 Main . _Setting . ControllerStartWidth = Width ;
12760 Main . _Setting . ControllerStartHeight = Height ;
12861 } ;
129- // MouseDown += (s, e) => DragWindow();
130- // flowPanel.FlowLayoutPanel.MouseDown += (s, e) => DragWindow();
13162 }
13263
13364 /// <summary>
@@ -159,24 +90,5 @@ void InitButton()
15990 }
16091 }
16192 }
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
18193 }
18294}
0 commit comments