窗体置顶代码

2015-03-07 21:44:19  访问(1460) 赞(0) 踩(0)


        #region 窗体置顶 [代码一]

        /// <summary>
        /// 获得本窗体的句柄
        /// </summary>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow(); 

        /// <summary>
        /// 设置此窗体为活动窗体
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        
        /// <summary>
        /// 定义变量,句柄类型
        /// </summary>
        public IntPtr mainHandle;

        #endregion 窗体置顶

        /// <summary>
        /// 主窗体
     /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 窗体置顶 [代码二]

            mainHandle = this.Handle;

            #endregion 窗体置顶 [代码二]
        }


        /// <summary>
        /// 显示窗体的事件
     /// </summary>
        protected void ShowWindowClick()
        {
            this.Show();

            // 如果是最小化的状态 //
            if (this.WindowState == FormWindowState.Minimized)
            {
                if (curFormWindowState == FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Maximized;
                }
                else
                {
                    curFormWindowState = FormWindowState.Normal;
                    this.WindowState = FormWindowState.Normal;
                }
            }

            #region 窗体置顶 [代码三]

            // 设置窗体置顶 //
            SetForegroundWindow(mainHandle);

            #endregion 窗体置顶 [代码三]
        }


上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)