WPF窗口置顶

    public class TopMostTool
    {
        public static int SW_SHOW = 5;
        public static int SW_NORMAL = 1;
        public static int SW_MAX = 3;
        public static int SW_HIDE = 0;
        //窗体置顶
        public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        //取消窗体置顶
        public static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);   
        //不调整窗体位置
        public const uint SWP_NOMOVE = 0x0002;    
        //不调整窗体大小
        public const uint SWP_NOSIZE = 0x0001;    

        /// <summary>
        /// 是否显示最前
        /// </summary>
        public static bool isFirst = true;

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow);

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);



        /// <summary>
        /// 在外面的方法中掉用这个方法就可以窗体始终置顶
        /// </summary>
        /// <param name="Name">需要置顶的窗体的名字</param>
        public static void setTop(string Name)
        {
            IntPtr CustomBar = FindWindow(null, Name);
            if (CustomBar != null)
            {
                if(isFirst) SetWindowPos(CustomBar, TopMostTool.HWND_TOPMOST, 0, 0, 0, 0, TopMostTool.SWP_NOMOVE | TopMostTool.SWP_NOSIZE);
            }
        }
    }
THE END
分享
二维码
< <上一篇
下一篇>>