1. 程式人生 > >WPF 視窗控制代碼獲取和設定

WPF 視窗控制代碼獲取和設定

WPF 視窗控制代碼設定和獲取

1、控制代碼類

  WHwnd.cs    
    
    public class WHwnd
    {
        /// <summary>
        /// 主窗體控制代碼
        /// </summary>
        public static System.Windows.Interop.HwndSource Hwnd;
        /// <summary>
        /// 獲取窗體控制代碼
        /// </summary>
        /// <param name="window">窗體</param>
        public static IntPtr GetWindowHwndSource(DependencyObject window, bool isHwnd = true)
        {
            var formDependency = System.Windows.Interop.HwndSource.FromDependencyObject(window);
            System.Windows.Interop.HwndSource winformWindow = (formDependency as System.Windows.Interop.HwndSource);
            if (isHwnd)
                Hwnd = winformWindow;
            return winformWindow.Handle;
        }

    }

2、視窗大小

WndSize.cs
    
    public class WndSize
    {
        /// <summary>
        /// 獲取主窗體大小和狀態
        /// </summary>
        /// <returns></returns>
        public static WindowSize GetWindowSize(IntPtr hwnd)
        {
            //是否最小化
            bool min = IsIconic(hwnd);
            //是否最大化
            bool max = IsZoomed(hwnd);

            RECT size = new RECT();
            GetWindowRect(hwnd, ref size);

            WindowSize wind = new WindowSize();
            wind.Left = size.Left;
            wind.Top = size.Top;
            wind.Width = size.Right - size.Left;
            wind.Height = size.Bottom - size.Top;
            //if (max == true)
            //    wind.Windowstatue = WindowState.Maximized;
            //else if (min == true)
            //    wind.Windowstatue = WindowState.Minimized;
            //else
            //    wind.Windowstatue = WindowState.Normal;

            return wind;
        }

        public static void CloseSoftware(IntPtr hwnd)
        {
            SendMessage(hwnd, SC_CLOSE, 0, 0);
        }
        public static int SC_CLOSE = 0X10;

        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool IsIconic(IntPtr hWnd);
        [DllImport("user32.dll")]
        public static extern bool IsZoomed(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;//最左座標
            public int Top;//最上座標
            public int Right;//最右座標
            public int Bottom;//最下座標
        }

    }

    public struct WindowSize
    {
        public int Left;
        public int Top;
        public int Width;
        public int Height;
        //public WindowState Windowstatue;
    }

3、設定控制代碼和獲取

在主函式Loaded事件中設定初始控制代碼:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    WHwnd.GetWindowHwndSource(this);
}

//自定義對話方塊彈出測試
ExMessageBox.ShowDialog(0, false, 3, tipStr, "", "", WHwnd.Hwnd.Handle);