1:程序使用反射机制,动态加载DLL窗体。
2:主程序分为验证窗体及主界面,在验证窗体中能获取主界面窗体句柄,但是当主界面窗体加载DLL窗体(已显示)后,无法获取该DLL窗体句柄。
3:系统环境VS2010+win7 32位系统。
4:主程序中获取句柄方法
//函数声明
 [DllImport("User32.dll", EntryPoint = "FindWindowA")]
  public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
 public static extern IntPtr FindWindowEx(Intprt ParentIntPtr,Intptr ChildIntPtr,string lpClassName, string lpWindowName);
//函数实现
 IntPtr ParentIntPtr = new IntPtr(0);
 IntPtr ChildIntPtr = new IntPtr(0);
 ParentIntPtr = FindWindow(null, "MainForm");
ChildIntPtr=FindWindowEx(ParentIntPtr,Intptr.Zero,null,"窗体1");ParentIntPt不为0,ChildIntPtr始终为0,将FindWindow的EntryPoint修改为FindWindowExW,FindWindowExA均不行。
反射时设置窗体的函数 
NowGetType.InvokeMember("MdiParent", BindingFlags.SetProperty, null, obj, new object[] { this }); 
在网上看了很多资料也不行,有没有知道的大虾说下,非常感谢!子窗体句柄问题

解决方案 »

  1.   

    以前使用delphi都成功了的,不知使用C#为什么不能成功!
      

  2.   

    方法没问题,用Spy++找到父窗体,检查ParentIntPt是否正确,然后看看它的子窗体中有没有窗体1
    程序里面也可以用EnumChildWindows看看
      

  3.   

    谢谢,问题到是找到了,以前一直认为主程序界面的句柄就是DLL窗体中的句柄,所以一直没有用spy++查询过。刚查询了,回来看帖子你就回复了,3Q
      

  4.   

        问题解决,从主窗体有个MdiChildren数组,表示MDI窗体的一个列表,使用你要查找的MDI窗体的标题就可以确定该MDI窗体的位置Index,然后使用this.MdiChildren[index].Handle,就可以获取到句柄。