您的位置:首页 > 编程语言 > C#

C# 如何设置窗口的大小

2011-08-23 13:33 483 查看
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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: