Created
March 7, 2021 03:35
-
-
Save AlphaNecron/33e6a4ac43119f49f082012c06802395 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
using System.Windows.Forms; | |
using System.Drawing; | |
namespace ADBToolkit | |
{ | |
public partial class Scrcpy : Window | |
{ | |
public Scrcpy() | |
{ | |
InitializeComponent(); | |
this.LocationChanged += OnLocationChanged; | |
this.SizeChanged += OnSizeChanged; | |
this.FormClosing += OnClosing; | |
this.Controls.Add(main); | |
} | |
public void OnClosing(object sender, FormClosingEventArgs e) | |
{ | |
Embed(true); | |
} | |
public void OnSizeChanged(object sender, EventArgs e) | |
{ | |
MoveWindow(hWndDocked, 0, 0, main.Width, main.Height, true); | |
} | |
private void OnLoad(object sender, EventArgs e) | |
{ | |
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); | |
Embed(false); | |
} | |
#region Import DLLs | |
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true, | |
CharSet = CharSet.Unicode, ExactSpelling = true, | |
CallingConvention = CallingConvention.StdCall)] | |
private static extern long GetWindowThreadProcessId(long hWnd, long lpdwProcessId); | |
[DllImport("user32.dll")] | |
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); | |
[DllImport("user32.dll", SetLastError = true)] | |
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); | |
[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)] | |
private static extern long GetWindowLong(IntPtr hwnd, int nIndex); | |
[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)] | |
private static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong); | |
[DllImport("user32.dll")] | |
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); | |
[DllImport("user32.dll", SetLastError = true)] | |
private static extern long SetWindowPos(IntPtr hwnd, long hWndInsertAfter, long x, long y, long cx, long cy, long wFlags); | |
[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)] | |
private static extern bool PostMessage(IntPtr hwnd, uint Msg, long wParam, long lParam); | |
[DllImport("user32.dll", SetLastError = true)] | |
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); | |
private Process dockProcess; | |
private Panel main; | |
private IntPtr hWndOriginalParent; | |
private IntPtr hWndDocked; | |
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); | |
private const UInt32 SWP_NOSIZE = 0x0001; | |
private const UInt32 SWP_NOMOVE = 0x0002; | |
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE; | |
#endregion | |
#region Ints | |
private const int SW_HIDE = 0; | |
private const int SW_SHOW = 5; | |
public const int GWL_STYLE = -16; | |
public const uint WS_OVERLAPPED = 0x00000000; | |
public const uint WS_POPUP = 0x80000000; | |
public const uint WS_CHILD = 0x40000000; | |
public const uint WS_MINIMIZE = 0x20000000; | |
public const uint WS_VISIBLE = 0x10000000; | |
public const uint WS_DISABLED = 0x08000000; | |
public const uint WS_CLIPSIBLINGS = 0x04000000; | |
public const uint WS_CLIPCHILDREN = 0x02000000; | |
public const uint WS_MAXIMIZE = 0x01000000; | |
public const uint WS_CAPTION = 0x00C00000; /* WS_BORDER | WS_DLGFRAME */ | |
public const uint WS_BORDER = 0x00800000; | |
public const uint WS_DLGFRAME = 0x00400000; | |
public const uint WS_VSCROLL = 0x00200000; | |
public const uint WS_HSCROLL = 0x00100000; | |
public const uint WS_SYSMENU = 0x00080000; | |
public const uint WS_THICKFRAME = 0x00040000; | |
public const uint WS_GROUP = 0x00020000; | |
public const uint WS_TABSTOP = 0x00010000; | |
public const uint WS_MINIMIZEBOX = 0x00020000; | |
public const uint WS_MAXIMIZEBOX = 0x00010000; | |
public const uint WS_TILED = WS_OVERLAPPED; | |
public const uint WS_ICONIC = WS_MINIMIZE; | |
public const uint WS_SIZEBOX = WS_THICKFRAME; | |
public const uint WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW; | |
// Common Window Styles | |
public const uint WS_OVERLAPPEDWINDOW = | |
(WS_OVERLAPPED | | |
WS_CAPTION | | |
WS_SYSMENU | | |
WS_THICKFRAME | | |
WS_MINIMIZEBOX | | |
WS_MAXIMIZEBOX); | |
public const uint WS_POPUPWINDOW = | |
(WS_POPUP | | |
WS_BORDER | | |
WS_SYSMENU); | |
public const uint WS_CHILDWINDOW = WS_CHILD; | |
//Extended Window Styles | |
public const uint WS_EX_DLGMODALFRAME = 0x00000001; | |
public const uint WS_EX_NOPARENTNOTIFY = 0x00000004; | |
public const uint WS_EX_TOPMOST = 0x00000008; | |
public const uint WS_EX_ACCEPTFILES = 0x00000010; | |
public const uint WS_EX_TRANSPARENT = 0x00000020; | |
//#if(WINVER >= 0x0400) | |
public const uint WS_EX_MDICHILD = 0x00000040; | |
public const uint WS_EX_TOOLWINDOW = 0x00000080; | |
public const uint WS_EX_WINDOWEDGE = 0x00000100; | |
public const uint WS_EX_CLIENTEDGE = 0x00000200; | |
public const uint WS_EX_CONTEXTHELP = 0x00000400; | |
public const uint WS_EX_RIGHT = 0x00001000; | |
public const uint WS_EX_LEFT = 0x00000000; | |
public const uint WS_EX_RTLREADING = 0x00002000; | |
public const uint WS_EX_LTRREADING = 0x00000000; | |
public const uint WS_EX_LEFTSCROLLBAR = 0x00004000; | |
public const uint WS_EX_RIGHTSCROLLBAR = 0x00000000; | |
public const uint WS_EX_CONTROLPARENT = 0x00010000; | |
public const uint WS_EX_STATICEDGE = 0x00020000; | |
public const uint WS_EX_APPWINDOW = 0x00040000; | |
public const uint WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE); | |
public const uint WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST); | |
//#endif /* WINVER >= 0x0400 */ | |
//#if(_WIN32_WINNT >= 0x0500) | |
public const uint WS_EX_LAYERED = 0x00080000; | |
//#endif /* _WIN32_WINNT >= 0x0500 */ | |
//#if(WINVER >= 0x0500) | |
public const uint WS_EX_NOINHERITLAYOUT = 0x00100000; // Disable inheritence of mirroring by children | |
public const uint WS_EX_LAYOUTRTL = 0x00400000; // Right to left mirroring | |
//#endif /* WINVER >= 0x0500 */ | |
//#if(_WIN32_WINNT >= 0x0500) | |
public const uint WS_EX_COMPOSITED = 0x02000000; | |
public const uint WS_EX_NOACTIVATE = 0x08000000; | |
//#endif /* _WIN32_WINNT >= 0x0500 */ | |
#endregion | |
public void Embed(bool close, string args = "") | |
{ | |
ProcessStartInfo startInfo = new ProcessStartInfo(); | |
startInfo.UseShellExecute = true; | |
startInfo.FileName = @"ADB\scrcpy-noconsole.exe"; | |
startInfo.Arguments = $"--window-borderless {args}"; | |
dockProcess = new Process(); | |
dockProcess = Process.Start(startInfo); | |
if (close) { dockProcess.Kill(); return; } | |
else | |
{ | |
while (hWndDocked == IntPtr.Zero) | |
{ | |
IntPtr hWnd = dockProcess.MainWindowHandle; | |
dockProcess.WaitForInputIdle(1000); //wait | |
ShowWindow(hWnd, SW_HIDE); | |
dockProcess.Refresh();//update process info | |
if (dockProcess.HasExited) | |
{ | |
return; //abort if the process finished before we got a handle. | |
} | |
SetWindowLong(dockProcess.MainWindowHandle, GWL_STYLE, WS_VISIBLE); | |
hWndDocked = dockProcess.MainWindowHandle; //cache the window handle | |
hWndOriginalParent = SetParent(hWndDocked, main.Handle); | |
MoveWindow(hWndDocked, 0, 0, main.Width, main.Height, true); | |
ShowWindow(hWnd, SW_SHOW); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment