获取面板的新大小,并在 winform 变为全屏后传输到用户控件
本文关键字:传输 控件 用户 新大小 winform 并在 获取 | 更新日期: 2023-09-27 18:34:54
有没有办法在FormWindowState.Maximized之后检索面板的更新大小?我可以得到面板的大小,但它给出了面板的原始尺寸。
谢谢;
顺便说一下,这是用户控制的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
//this is the way i retrieve the panel size
Form1 f = new Form1();
this.Size = f.Size;
}
}
}
这是表单代码,面板有 4 个激活的锚点
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 n = new UserControl1();
panel1.Controls.Add(n);
}
private void Form1_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
}
}
}
如果要做的是每次调整父窗体的大小时调整用户控件的大小,则可以使用ParentForm
的Resize
事件:
主要形式:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ans
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
var uc = new UserControl2();
uc.Location = new Point(0, 0);
this.Controls.Add(uc);
WindowState = FormWindowState.Maximized;
}
}
}
用户控制:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ans
{
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
private void UserControl2_Load(object sender, EventArgs e)
{
this.BackColor = Color.Aqua;
if (this.ParentForm != null)
{
this.Size = this.ParentForm.Size;
this.ParentForm.Resize += ParentForm_Resize;
}
}
private void ParentForm_Resize(object sender, EventArgs e)
{
this.Size = ((Form)sender).Size;
}
}
}
如果希望在运行时动态创建的用户控件随放置它的控件一起增长和收缩,则将用户控件的 Dock 属性设置为在运行时填充:
private void button1_Click(object sender, EventArgs e)
{
MyUserControl userControl = new MyUserControl();
panel1.Controls.Add(userControl);
userControl.Dock = DockStyle.Fill;
}