Beispiel #1
0
 private void ResetWindowPosition(IntPtr windowHandle, User32.WINDOWINFO windowInfo)
 {
     if (!User32.SetWindowPos(windowHandle, default, 0, 0, windowInfo.rcWindow.GetWidth(), windowInfo.rcWindow.GetHeight(), 0))
     {
         throw new Exception(Kernel32.GetLastError().ToString());
     }
 }
 public void GetWindowInfo()
 {
     User32.WINDOWINFO info = new User32.WINDOWINFO();
     info.cbSize = (uint)Marshal.SizeOf(info);
     User32.GetWindowInfo(Window, ref info);
     Info = info;
 }
Beispiel #3
0
 private User32.WINDOWINFO GetWindowInfo(IntPtr windowHandle)
 {
     User32.WINDOWINFO windowInfo = default;
     if (!User32.GetWindowInfo(windowHandle, ref windowInfo))
     {
         throw new Exception(Kernel32.GetLastError().ToString());
     }
     return(windowInfo);
 }
Beispiel #4
0
        //todo: 活动窗口时shell的情况

        bool IsTopMostWindow(IntPtr hwnd)
        {
            User32.WINDOWINFO info = new User32.WINDOWINFO();
            info.cbSize = (uint)Marshal.SizeOf(info);
            User32.GetWindowInfo(hwnd, ref info);

            const uint TOP_MOST = (uint)User32.WS_EX.WS_EX_TOPMOST;

            return((info.dwExStyle & TOP_MOST) == TOP_MOST);
        }
        private static bool GetBorderSize(IntPtr handle, out Size size)
        {
            var wi = new User32.WINDOWINFO();

            var result = User32.GetWindowInfo(handle, ref wi);

            size = result ? new Size((int)wi.cxWindowBorders, (int)wi.cyWindowBorders) : Size.Empty;

            return(result);
        }
Beispiel #6
0
        //public static string SettingGet(int row, int col)
        //{
        //    if (row == 0 && col == 0)
        //        return Properties.Settings.Default.AppTitle0x0;
        //    if (row == 0 && col == 1)
        //        return Properties.Settings.Default.AppTitle0x1;
        //    if (row == 0 && col == 2)
        //        return Properties.Settings.Default.AppTitle0x2;
        //    if (row == 1 && col == 0)
        //        return Properties.Settings.Default.AppTitle1x0;
        //    if (row == 1 && col == 1)
        //        return Properties.Settings.Default.AppTitle1x1;
        //    if (row == 1 && col == 2)
        //        return Properties.Settings.Default.AppTitle1x2;
        //    return null;
        //}

        //public static void SettingSave(string appTitle, int row, int col)
        //{
        //    if (row == 0 && col == 0)
        //        Properties.Settings.Default.AppTitle0x0 = appTitle;
        //    if (row == 0 && col == 1)
        //        Properties.Settings.Default.AppTitle0x1 = appTitle;
        //    if (row == 0 && col == 2)
        //        Properties.Settings.Default.AppTitle0x2 = appTitle;
        //    if (row == 1 && col == 0)
        //        Properties.Settings.Default.AppTitle1x0 = appTitle;
        //    if (row == 1 && col == 1)
        //        Properties.Settings.Default.AppTitle1x1 = appTitle;
        //    if (row == 1 && col == 2)
        //        Properties.Settings.Default.AppTitle1x2 = appTitle;

        //    Properties.Settings.Default.Save();
        //}

        public static BitmapSource CaptureApplication(IntPtr hWnd, bool bRestoreBeforeSnapshot)
        {
            if (bRestoreBeforeSnapshot)
            {
                User32.RestoreWindow(hWnd); //to get snapshot
            }
            //User32.PROCESS_DPI_AWARENESS pda = User32.GetProcessDpiAwareness(process.Handle);

            User32.WINDOWINFO wi = User32.GetWindowInfo(hWnd);
            int width            = wi.rcWindow.Width;
            int height           = wi.rcWindow.Height;

            if (width == 0 || height == 0)
            {
                return(null);
            }

            using (Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                using (Graphics graphics = Graphics.FromImage(bmp))
                {
                    graphics.Clear(Color.Wheat);

                    IntPtr hdc = graphics.GetHdc();
                    if (!User32.PrintWindow(hWnd, hdc, User32.PW_RENDERFULLCONTENT))// User32.PW_CLIENTONLY))
                    {
                        int error     = Marshal.GetLastWin32Error();
                        var exception = new System.ComponentModel.Win32Exception(error);
                        Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
                    }

                    graphics.ReleaseHdc(hdc);
                    graphics.Dispose();
                }

                //bmp.Save("c:\\temp\\test.png", ImageFormat.Png);
                BitmapSource b = GetImageSourceFromBitmap(bmp);
                return(b);
            }
        }
Beispiel #7
0
        private void SetMagnification(User32.WINDOWINFO windowInfo)
        {
            var width  = windowInfo.rcClient.GetWidth();
            var height = windowInfo.rcClient.GetHeight();

            var isHigherThanWider = desktopHeight / height <= desktopWidth / width;
            var magnification     = isHigherThanWider ?
                                    (float)desktopHeight / height :
                                    (float)desktopWidth / width;

            var xOffset = Math.Max(0, isHigherThanWider ?
                                   (int)Math.Ceiling((desktopWidth / magnification - width) / 2f) :
                                   (int)Math.Floor((desktopWidth / magnification - width) / 2f)
                                   );
            var yOffset = Math.Max(0, isHigherThanWider ?
                                   (int)Math.Floor((desktopHeight / magnification - height) / 2f) :
                                   (int)Math.Ceiling((desktopHeight / magnification - height) / 2f)
                                   );

            if (!Magnification.MagSetFullscreenTransform(magnification, windowInfo.rcClient.left - xOffset, windowInfo.rcClient.top - yOffset))
            {
                throw new Exception(Kernel32.GetLastError().ToString());
            }
        }
Beispiel #8
0
        public void Run()
        {
            Process process = null;

            try
            {
                process = Process.Start(executablePath);
                process.WaitForInputIdle();

                int width = 0, height = 0;
                User32.WINDOWINFO windowInfo = default;
                windowInfo = GetWindowInfo(process.MainWindowHandle);
                width      = windowInfo.rcWindow.right - windowInfo.rcWindow.left;
                height     = windowInfo.rcWindow.bottom - windowInfo.rcWindow.top;

#if DEBUG
                // If Notepad was started, test different window sizes.
                if (executablePath == "notepad")
                {
                    width  = 300;
                    height = 600;
                }
#endif

                ResetWindowPosition(process.MainWindowHandle, width, height);
                InitializeMagnification();

                backgroundWindow = new BackgroundWindow(process.MainWindowHandle, desktopWidth, desktopHeight).Show();

                User32.ShowCursor(false);

                var  refreshInterval = 200;
                var  previousRect = new RECT();
                bool pause = false, forceUpdate = false;
                while (!process.HasExited)
                {
                    if ((User32.GetAsyncKeyState((int)User32.VirtualKey.VK_LWIN)) != 0 &&
                        (User32.GetAsyncKeyState((int)User32.VirtualKey.VK_ESCAPE)) != 0)
                    {
                        pause = !pause;
                        if (pause)
                        {
                            backgroundWindow.Hide();
                            ResetMagnification();
                        }
                        else
                        {
                            backgroundWindow.Show();
                            forceUpdate = true;
                        }
                    }

                    if (pause)
                    {
                        Thread.Sleep(refreshInterval);
                        continue;
                    }

                    if (User32.GetForegroundWindow() != process.MainWindowHandle)
                    {
                        User32.SetForegroundWindow(process.MainWindowHandle);
                    }

                    windowInfo = GetWindowInfo(process.MainWindowHandle);
                    if (windowInfo.rcWindow.left != 0 || windowInfo.rcWindow.top != 0)
                    {
                        ResetWindowPosition(process.MainWindowHandle, windowInfo);
                        windowInfo = GetWindowInfo(process.MainWindowHandle);
                    }

                    if (forceUpdate || !windowInfo.rcClient.Equals(previousRect))
                    {
                        SetMagnification(windowInfo);
                        previousRect = windowInfo.rcClient;
                    }

                    if (forceUpdate)
                    {
                        forceUpdate = false;
                    }

                    Thread.Sleep(refreshInterval);
                }
            }
            catch (Exception ex)
            {
                ex.Message.Log();

                if (process != null && !process.HasExited)
                {
                    process.Kill();
                }
            }
            finally
            {
                try { User32.ShowCursor(true); } catch { }
                try { Magnification.MagUninitialize(); } catch { }
                try { backgroundWindow?.Hide(); } catch { }
            }
        }
Beispiel #9
0
        public static void changeFolderViewState()
        {
            var winfo = new User32.WINDOWINFO();

            User32.GetWindowInfo(folderview, ref winfo);
        }