C# 窗口全屏、置顶、获取焦点

很简单的几行代码

            this.FormBorderStyle = FormBorderStyle.None;     //设置窗体为无边框样式  
            this.WindowState = FormWindowState.Maximized;    //最大化窗体 
            this.TopMost = true;                            //设置窗体置顶

始终获取焦点

        //调用API
        [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow(); //获得本窗体的句柄
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体
        //定义变量,句柄类型
        public IntPtr Handle1;

        public Form1()
        {

            InitializeComponent();
            
        //    this.FormBorderStyle = FormBorderStyle.None;     //设置窗体为无边框样式  
          //  this.WindowState = FormWindowState.Maximized;    //最大化窗体 
            this.TopMost = true;                            //设置窗体置顶
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Handle1 = this.Handle;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //设置此窗体为活动窗体
            SetForegroundWindow(Handle1);
        }

 

posted @ 2019-06-15 11:03  随遇而安(huangjun)  阅读(6590)  评论(0编辑  收藏  举报