1、窗体属性
通过控件的Anchor和Dock属性来调整,Dock的优先级比Anchor高。
Dock属性:表示控件在窗体中停靠的位置,其取值Top,Bottom,Left,Right和Fill分别表示停靠在窗体的顶部,底部,左边,右边和填充整个窗体。
Anchor属性:表示控件的指定边缘锚定到窗体上,即该边缘与窗体对应边的距离保持不变。
Parent属性:获取或设置控件的父容器。
Owner属性:获取或设置拥有此窗体的窗体。若要使某窗体归另一个窗体所有,请为其 Owner 属性分配一个对将成为所有者的窗体的引用。当一个窗体归另一窗体所有时,它便随着所有者窗体最小化和关闭。例如,如果 Form2 归窗体 Form1 所有,则关闭或最小化 Form1 时,Form2 也会关闭或最小化。
ShowInTaskbar:用于程序不在系统任务栏中显示
2、窗体动态出现:
Timer timer = new Timer();
private void timer_Tick(object sender, EventArgs e)
{
if (this.Height >= 300) timer.Stop();
this.Width += 4;
this.Height += 4;
}
protected override void OnClick(EventArgs e)
{
timer.Start();
}
3、窗体透明渐变
bool increase = false;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity < .02) increase = true;
if (this.Opacity > .98) increase = false;
if (increase) this.Opacity += .02d;
else this.Opacity -= .02d;
if (this.Opacity < .2) this.Hide();
else this.Show();
}
4、拖动无边窗体
bool isMouseDown=false;
Point pointMouse=new Point();
protected override void OnMouseDown(MouseEventArgs e)
{
pointMouse=e.Location;
isMouseDown=true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if(isMouseDown)
{
Point location =this.PointToScreen(e.Location);//记录鼠标屏幕坐标
location.Offset(-pointMouse.X,-pointMouse.Y);//将鼠标屏幕坐标转换为窗体坐标
this.Location=location;//设置窗体坐标
this.Text = string.Format("窗体当前坐标:({0},{1})\n 鼠标当前坐标:({2},{3})" ,this.Left,this.Top,pointMouse.X,pointMouse.Y);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
isMouseDown=false;
}
5、实现多文档窗体
将主窗体的IsMdiContainer属性设置为True;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
foreach (var item in this.Controls)
{
if (item is MdiClient)
{
MdiClient mdi = item as MdiClient;
mdi.Click+=new EventHandler(mdi_Click);
break;
}
}
}
void mdi_Click(object sender, EventArgs e)
{
Form form = new Form();
form.MdiParent = this;
form.Text = "MDI子窗体";
form.Show();
}