我想将所有者表单设置为非托管窗口的表单。我有非托管窗口句柄。如何将此非托管窗口设置为托管表单的所有者窗口?
IntPtr hWnd = GetUnmanagedWindow();//assume the handle is returned correctly
Form form = new Form();
form.Show(ConvertToManaged(hWnd));//Need an implementation for ConvertOrSomething()
答案 0 :(得分:6)
执行此操作的标准方法是使用NativeWindow
类。
IntPtr hWnd = GetUnmanagedWindow();//assume the handle is returned correctly
Form form = new Form();
NativeWindow nativeWindow = new NativeWindow();
nativeWindow.AssignHandle(hWnd);
form.Show(nativeWindow);
正如汉斯指出的那样,记得在完成任务后给ReleaseHandle
打电话。
答案 1 :(得分:3)
public ManagedWindow ConvertToManaged(IntPtr hWnd)
{
return new ManagedWindow(hWnd);
}
public class ManagedWindow : IWin32Window
{
IntPtr _handle;
public IntPtr Handle
{
get { return _handle; }
}
public ManagedWindow(IntPtr handle)
{
_handle = handle;
}
}