/* * MIT License * * Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ShiftOS.Engine; using System.IO; using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; /// /// Coherence commands. /// namespace ShiftOS.WinForms { [Namespace("coherence")] [RequiresUpgrade("kernel_coherence")] public static class CoherenceCommands { /// /// Sets the window position. /// /// The window position. /// H window. /// H window insert after. /// X. /// Y. /// Cx. /// Cy. /// U flags. [DllImport("user32.dll")] static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); /// /// The HWN d TOPMOS. /// static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); /// /// The SW p SHOWWINDO. /// const UInt32 SWP_SHOWWINDOW = 0x0040; [DllImport("user32.dll")] /// /// Gets the window rect. /// /// The window rect. /// H window. /// Lp rect. [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); /// /// REC. /// [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } [Command("launch", "process: \"C:\\path\\to\\process\" - The process path to launch.", "Launch a process inside kernel coherence.")] [RequiresArgument("process")] /// /// Launchs the app. /// /// The app. /// Arguments. public static bool LaunchApp(Dictionary args) { string process = args["process"].ToString(); var prc = Process.Start(process); StartCoherence(prc); return true; } /// /// Starts the coherence. /// /// The coherence. /// Prc. private static void StartCoherence(Process prc) { RECT rct = new RECT(); while (!GetWindowRect(prc.MainWindowHandle, ref rct)) { } AppearanceManager.Invoke(new Action(() => { IShiftOSWindow coherenceWindow = new Applications.CoherenceOverlay(prc.MainWindowHandle, rct); AppearanceManager.SetupWindow(coherenceWindow); SetWindowPos(prc.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); //MakeExternalWindowBorderless(prc.MainWindowHandle); })); } /// /// The W s BORDE. /// const int WS_BORDER = 8388608; /// /// The W s DLGFRAM. /// const int WS_DLGFRAME = 4194304; /// /// The W s CAPTIO. /// const int WS_CAPTION = WS_BORDER | WS_DLGFRAME; /// /// The W s SYSMEN. /// const int WS_SYSMENU = 524288; /// /// The W s THICKFRAM. /// const int WS_THICKFRAME = 262144; /// /// The W s MINIMIZ. /// const int WS_MINIMIZE = 536870912; /// /// The W s MAXIMIZEBO. /// const int WS_MAXIMIZEBOX = 65536; /// /// The GW l STYL. /// const int GWL_STYLE = -16; /// /// The GW l EXSTYL. /// const int GWL_EXSTYLE = -20; /// /// The W s E x DLGMODALFRAM. /// const int WS_EX_DLGMODALFRAME = 0x1; /// /// The SW p NOMOV. /// const int SWP_NOMOVE = 0x2; /// /// The SW p NOSIZ. /// const int SWP_NOSIZE = 0x1; /// /// The SW p FRAMECHANGE. /// const int SWP_FRAMECHANGED = 0x20; /// /// The M f BYPOSITIO. /// const uint MF_BYPOSITION = 0x400; /// /// The M f REMOV. /// const uint MF_REMOVE = 0x1000; /// /// Gets the window long. /// /// The window long. /// H window. /// N index. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int GetWindowLong(IntPtr hWnd, int nIndex); /// /// Sets the window long. /// /// The window long. /// H window. /// N index. /// Dw new long. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); /// /// Sets the window position. /// /// The window position. /// H window. /// H window insert after. /// X. /// Y. /// Cx. /// Cy. /// U flags. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); public static void MakeExternalWindowBorderless(IntPtr MainWindowHandle) { int Style = 0; Style = GetWindowLong(MainWindowHandle, GWL_STYLE); Style = Style & ~WS_CAPTION; Style = Style & ~WS_SYSMENU; Style = Style & ~WS_THICKFRAME; Style = Style & ~WS_MINIMIZE; Style = Style & ~WS_MAXIMIZEBOX; SetWindowLong(MainWindowHandle, GWL_STYLE, Style); Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE); SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style | WS_EX_DLGMODALFRAME); SetWindowPos(MainWindowHandle, new IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); } } }