我怎么可以嵌入非托管C ++形式到.NET应用程序?应用程序、形式、我怎么、NET

2023-09-03 13:18:01 作者:把难过拉黑

我已经能够成功地缠上了我的非托管的Borland C ++ DLL,并启动它是从C#.NET 4.0的申请表格。是否有可能嵌入在DLL中的形式直接进入.NET应用程序?

I have been able to successfully wrap my unmanaged Borland C++ dll, and launch it's forms from a C# .NET 4.0 application. Is it possible to embed a form from the dll directly into a .NET application?

要澄清,原来的形式被用作一个用Borland C ++项目已经在嵌入式控制。它实质上是看起来像一个自定义的控制,坐在应用程序中的面板上。

To clarify, the original form is being used as an embedded control in a Borland C++ project already. It essentially looks like a custom control, sitting on a panel within the application.

当我说'嵌入'我的意思是,投中拖放到一个表单按钮,面板等以同样的方式的一种形式。我不希望只是做一个子窗体。

When I say 'embed' I mean place INTO a form in the same way you drop buttons, panels, etc on to a form. I'm not looking to just make a child form.

如果做不到这一点,那么也许一个更好的问题是我如何嵌入unmanged自定义控件到.Net应用程序?

If this is not possible, then perhaps a better question would be how do I embed an unmanged custom control into a .Net application?

推荐答案

是的,你只需要使用一些低级别的Win32函数从user32.dll中:的setparent,GetWindowLog,SetWindowLong函数,的MoveWindow。您可以创建一个空的.NET容器控件,设置本机窗口的父到.NET控件,然后(可选)修改窗口样式(即除去原生窗口边框),并与注意调整其大小一起.NET控件。需要注意的是,在管理水平,.NET控件将不知道有任何孩子。

Yes, you just need to use some low-level win32 functions from user32.dll : SetParent, GetWindowLog, SetWindowLong , MoveWindow . You can create an empty .NET container control, set the parent of the native window to the .NET control, then (optionally) modify the window style (i.e. to remove borders of native window), and pay attention to resize it together with the .NET control. Note that, at a managed level, the .NET control will be unaware it has any children.

在.NET控件做这样的事情

In the .NET control do something like

public void AddNativeChildWindow(IntPtr hWndChild){

        //adjust window style of child in case it is a top-level window
        int iStyle = GetWindowLong(hWndChild, GWL_STYLE);
        iStyle = iStyle & (int)(~(WS_OVERLAPPEDWINDOW | WS_POPUP));
        iStyle = iStyle | WS_CHILD;
        SetWindowLong(hWndChild, GWL_STYLE, iStyle);


        //let the .NET control  be the parent of the native window
        SetParent((IntPtr)hWndChild, this.Handle);
         this._childHandle=hWndChild;

        // just for fun, send an appropriate message to the .NET control 
        SendMessage(this.Handle, WM_PARENTNOTIFY, (IntPtr)1, (IntPtr)hWndChild);

}

然后重写.NET控件的WndProc中,使其适当地调整了原生形式 - 例如,以填补客户区。

Then override the WndProc of the .NET control to make it resize the native form appropriately -- for example to fill the client area.

 protected override unsafe void WndProc(ref Message m)
    {

        switch (m.Msg)
        {
            case WM_PARENTNOTIFY:
                   //... maybe change the border styles , etc
                   break;
              case WM_SIZE:
                iWid =(int)( (int)m.LParam & 0xFFFF);
                iHei= (int) (m.LParam) >> 16;
                if (_childHandle != (IntPtr)0)
                {

                    MoveWindow(_childHandle, 0, 0, iWid, iHei, true);

                }
                break;

        }

 }
 
精彩推荐
图片推荐