当前位置:   article > 正文

C# 如何设置窗口的大小_c#设置窗口大小

c#设置窗口大小

C#窗口默认有三种窗口格式,

Form fr = new Form();

fr.WindowState = WindowState.Maxmize;

fr.WindowState = WindowState.MinMize;

fr.WindwoState = WindowState.Normal;

如果想设置窗口大小的话则首先窗口的格式必须是可调节的,

fr.FormBorderStyle = FormBoderStyle..Sizable//或者FixeSingle

本人遇到一种情况就是当全屏显示时,调用 了API函数

 int TaskBarHwnd = FindWindow("Shell_traywnd", "");
  SetWindowPos(TaskBarHwnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);

将状态栏隐藏了,再重新退出全屏之后,设置最大化窗口发现状态栏把程序的窗口的状态栏给盖住了。这样的话就得自己设置窗口的大小了。

我采用了SCreen.GetWorkArea(); 此函数获得除状态栏浮动窗口之外的屏幕区域。这样如果我将窗口设置的尺寸与屏幕工作区大小相等时就不会被状态栏盖住了,废话少说代码如下:

 public const int SWP_HIDEWINDOW = 0x80;
        public const int SWP_SHOWWINDOW = 0x40;
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(
                                int hWnd,                                                     //     handle     to     window          
                                int hWndInsertAfter,         //     placement-order     handle          
                                short X,                                                                     //     horizontal     position          
                                short Y,                                                                     //     vertical     position          
                                short cx,                                                                 //     width          
                                short cy,                                                                 //     height          
                                uint uFlags                                                 //     window-positioning     options          
                                );
        [DllImport("user32.dll")]
        public static extern int FindWindow(
                                string lpClassName,         //     class     name          
                                string lpWindowName         //     window     name          
                                );

 public void SetFullScreen()//全屏
        {
            int TaskBarHwnd = FindWindow("Shell_traywnd", "");
           
            SetWindowPos(TaskBarHwnd, 0, 0, 0, 0, 0, SWP_HIDEWINDOW);
            this.Hide();
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            menuStrip1.Visible = false;
            this.Show();


        }

  public void ExFullScreen()//退出全屏
        {
            int TaskBarHwnd = FindWindow("Shell_traywnd", "");
            SetWindowPos(TaskBarHwnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.WindowState = FormWindowState.Normal;
            this.Size = new Size(Screen.GetWorkingArea(this).Width, Screen.GetWorkingArea(this).Height);
            menuStrip1.Visible = true;
        }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/511785
推荐阅读
相关标签
  

闽ICP备14008679号