好吧,很多人都以这种或那种方式问过这个问题,但是除了在调用子对象时传递父对象之外似乎没有太多答案。问题是,我无权访问父代码。
这是我的情况。
一些我无法访问的代码,我只是使用了 dll(父窗体),在我的代码(子窗体)中调用了一个函数。
我的函数调用需要父表单作为参数之一的第 3 方 SDK(不太重要)。此外,我无法访问第 3 方代码,只能通过一些 C++ 库。
我的子表单能否认识其父表单,还是注定成为孤儿?
最佳答案
在最一般的情况下(因为您的表单位于 dll 中的某处并且您必须将父表单传递给第 3 方软件)- WinAPI - 您可以使用 检索父窗口句柄GetParent
函数
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633510(v=vs.85).aspx
类似的东西:
[DllImport("user32.dll",
EntryPoint = "GetParent",
CharSet = CharSet.Auto)]
internal static extern IntPtr GetParent(IntPtr hWnd);
...
IntPtr parentHandle = GetParent(myForm.Handle); // <- If you have a form
...
IntPtr myFormHandle = ...
IntPtr myFormParentHandle = GetParent(myFormHandle); // <- If you have WHND only
// If there's a .net form with myFormParentHandle Handle you can retrieve it
// Otherwise (e.g. form is not a .net one) you get null
Form parentForm = Control.FromHandle(myFormParentHandle) as Form;
关于c# - 如何访问父表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574956/