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;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值