我能使用HWND / NativeWindow的设置我的WinForms表格的所有者的行为? [英] Can I get the behavior of setting my WinForms form's owner using an hwnd / NativeWindow?

查看:103
本文介绍了我能使用HWND / NativeWindow的设置我的WinForms表格的所有者的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个VB6的可执行文件,但系统中的一些较新的形式写在C#。我希望能够使用的句柄主应用程序窗口设置C#窗体的所有者财产,使对话Tab键来回我的应用程序和其他应用程序之间时留在上面。



我可以在主应用程序窗口的HWND。我不知道我可以在那里做什么?






更新10月20日在'08 17:06



斯科特,



感谢您的答复。我忽略了显示/ ShowDialog方法参数是类型的表格不是 - 我是在所有者属性只找



我略作修改,我使用的代码上面的 - 我们有一般加载我们的形式,并调用ShowDialog的一个组成部分。我的代码看起来是这样的:

 表单launchTarget = FormFactory.GetForm(XXX); //为通用的形式装载机
伪代码launchTarget.StartPosition = FormStartPosition.CenterParent;
IWin32Window parentWindow = GetWindowFromHwnd(HWND);

launchTarget.ShowDialog(parentWindow);



GetWindowFromHwnd 是一个方法包裹的版本您的代码:

 私人IWin32Window GetWindowFromHost(INT HWND)
{
IWin32Window窗口= NULL;
的IntPtr句柄=新​​的IntPtr(HWND);


{
的NativeWindow的nativeWindow =新的NativeWindow();
nativeWindow.AssignHandle(句柄);
窗口=的nativeWindow;
}
终于
{
手柄= IntPtr.Zero;
}

返回窗口;
}



可惜,这是不是做什么我所希望的。该表格​​显示做模态,但它没有显示在正确的位置了,也不是它仍然在上面,当我离开标签和回父窗口。我们的情态不显示在任务栏的任务,因此,窗口看似消失(虽然它仍然是在使用Alt-Tab窗口列表存在)。那对我表示我可能没有正确的HWND。如果您有任何虽然其他建议,请回信。再次感谢。






更新11月10日在08 16点25



一个跟进的话 - 如果您因素出来到一个try /最后一个方法调用,在斯科特的第二后,在finally块中调用应该是:

  parentWindow.ReleaseHandle(); 


解决方案

所以,你在呼唤从VB6一个C#Windows窗体类,这意味着你可能正在使用或者显示()的ShowDialog(),正确吗?这两种这些方法还采取IWin32Window参数,它只是定义了返回名为手柄一个IntPtr属性的对象。



所以...你需要添加一个重载的构造函数(或ShowDialog方法)为您的Windows窗体它采取作为参数,因此您可以通过VB6 HWND到窗体类。一旦C#代码里面,你需要从HWND创建一个IntPtr并将其分配给一个的NativeWindow 对象,然后传递作为所有者。



像这样的的工作,虽然它的未经测试的:

 公开的DialogResult的ShowDialog(HWND长)
{
的IntPtr句柄=新​​的IntPtr(HWND);

{
的NativeWindow的nativeWindow =新的NativeWindow();

nativeWindow.AssignHandle(句柄);
返回this.ShowDialog(的nativeWindow);
}
终于
{
手柄= IntPtr.Zero;
}
}


My application is a vb6 executable, but some newer forms in the system are written in C#. I would like to be able to set the C# form's Owner property using a handle to the main application window, so that the dialogs remain on top when tabbing back and forth between my app and other apps.

I can get the hwnd of the main application window. I'm not sure what I can do from there?


UPDATE Oct 20 '08 at 17:06:

Scott,

Thanks for the response. I had overlooked that the Show/ShowDialog method parameter was not of type Form - I was looking only at the Owner property.

I slightly modified the code I'm using from the above - we have a component that generically loads our Forms and calls ShowDialog. My code looks like this:

Form launchTarget = FormFactory.GetForm(xxx);  // psuedo-code for generic form loader
launchTarget.StartPosition = FormStartPosition.CenterParent;
IWin32Window parentWindow = GetWindowFromHwnd(hwnd);

launchTarget.ShowDialog(parentWindow);

GetWindowFromHwnd is a method-wrapped version of your code:

private IWin32Window GetWindowFromHost(int hwnd)
{
    IWin32Window window = null;
    IntPtr handle = new IntPtr(hwnd);

    try
    {
        NativeWindow nativeWindow = new NativeWindow();
        nativeWindow.AssignHandle(handle);
        window = nativeWindow;
    }
    finally
    {
        handle = IntPtr.Zero;
    }

    return window;
}

Unfortunately this isn't doing what I'd hoped. The form does display modally, but it's not showing up in the correct position nor is it still on top when I tab away and back to the parent window. Our modals do not show a task in the taskbar, so the window seemingly "disappears" (although it is still present in the alt-tab window list). That to me indicates I might not have the right hwnd. If you have any other suggestions though, please reply back. Thanks again.


UPDATE Nov 10 '08 at 16:25

One follow up remark - If you factor it out into a method call in a try/finally, as in Scott's 2nd post, the call in the finally block should be:

parentWindow.ReleaseHandle();

解决方案

So you are calling a C# Windows Form class from VB6, which means you are probably using either Show() or ShowDialog(), correct? Both of those methods also take an IWin32Window parameter, which simply defines an object that returns an IntPtr property named Handle.

So...you need to add an overloaded constructor (or ShowDialog method) for your Windows Forms classes which take a long as a parameter so you can pass the VB6 hwnd to the form. Once inside the C# code, you need to create an IntPtr from the hwnd and assign it to a NativeWindow object and then pass that as the owner.

Something like this should work, although it's untested:

public DialogResult ShowDialog(long hwnd)
{
   IntPtr handle = new IntPtr(hwnd);
   try
   {
      NativeWindow nativeWindow = new NativeWindow();

      nativeWindow.AssignHandle(handle);
      return this.ShowDialog(nativeWindow);
   }
   finally
   {
      handle = IntPtr.Zero;
   }
}

这篇关于我能使用HWND / NativeWindow的设置我的WinForms表格的所有者的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆