在Form怎么嵌入子Form

来源:互联网 发布:信用卡套现用什么软件 编辑:程序博客网 时间:2024/05/17 00:54

写界面时,经常要更换界面很多内容,可用MDI解决这种问题。

这里提供一种在SDI中更换界面内容的方法。

在Form中新建一个panel控件,在子Form的load事件中加载setParent().

在主form中的button事件中

son.dlg =new son(this.panel1);
    dlg.Show();

把panel句柄传给子窗体。

在子窗体中

using System.Runtime.InteropServices;

private Control ctl=null;

private void SonLoad(object sender, System.EventArgs e)
  {
   
   //this.Parent=this.Owner;
   Form control = this.FindForm();
   control.Text ="hehehe";

   //this.Parent = this.
   SetParent((int)this.Handle,(int)ctl.Handle);
   this.Location= new Point(0, 0);
  }

  [DllImport("user32.dll")]
  static extern int SetParent(int hWndChild, int hWndNewParent);

这些就搞定了,这是C#代码,其它语言也是类似处理。关健是API SetParent函数。

如果不想一定打开多个,每次dlg.show之前,应该判断一下是否已经打开,打开过就先close前一个子窗体。

if(bSonForm)
   {
    dlg.Close();
    dlg = null;
    dlg =new son(this.panel1);
    dlg.Show();
    bSonForm = true;
   }
   else
   {
    dlg =new son(this.panel1);
    dlg.Show();
    bSonForm = true;
   }

原创粉丝点击