如何设置子窗口显示在父窗口的中间,还有如果子窗口的宽度或高度大于父窗口
如何设置子窗口的大小自动适应父窗口大小?

解决方案 »

  1.   

    子窗口   pChildWin,父窗口pParentWinRECT   rect;
    pChildWin-> GetWindowRect(&rect);
    pParentWin-> ScreenToClient(&rect);
      

  2.   

    Form2 form = new Form2(); 
    form.MdiParent = this; 
    form.Parent = splitContainer1.Panel2;
    form.Left =(this.splitContainer1.Panel2.Width-form.Width) / 2;
    form.Top = (this.splitContainer1.Panel2.Height - form.Height) / 2;
    this.splitContainer1.Panel2.Controls.Add(form); 
    form.Show(); StartPosition 
    Location =
      

  3.   

    写了个简单的实现,还有一些情况没有考虑,LZ可以试着自己修改一下
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private Form m_ChildForm;        protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
                m_ChildForm = new Form();
                m_ChildForm.MdiParent = this;            ChangeChildFromSize();            m_ChildForm.Show();            ChangeChildFormLocation();
            }        protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
                if (m_ChildForm == null)
                    return;            ChangeChildFromSize();
                ChangeChildFormLocation();
            }        private void ChangeChildFromSize()
            {
                if (m_ChildForm.Width > this.ClientSize.Width)
                    m_ChildForm.Width = this.ClientSize.Width;
                if (m_ChildForm.Height > this.ClientSize.Height)
                    m_ChildForm.Height = this.ClientSize.Height;
            }        private void ChangeChildFormLocation()
            {
                m_ChildForm.Location = new Point(
                    (this.ClientSize.Width - m_ChildForm.Width) / 2,
                    (this.ClientSize.Height - m_ChildForm.Height) / 2
                    );
            }