gpt4 book ai didi

c# - 将 c# windows 面板转换为 c HWND

转载 作者:行者123 更新时间:2023-11-30 15:01:40 25 4
gpt4 key购买 nike

我有一个接受 HWND 的 dll,(dll 中的代码);

void VideoCapture::SetVideoWindow(HWND VidWind)
{
VideoWindow = VidWind;
}

我通过在引用中添加 dll 在示例 c#.net 应用程序中调用上述 dll,在 c#.net 中我有一个带有面板的表单,是否可以将该面板传递给 dll?我在 c# 中给出了如下代码

VidCapWrapper.ManagedVideoCapture cc = new VidCapWrapper.ManagedVideoCapture();

cc.SetVideoWindow( panel1);

我收到如下错误:'错误 2 'VidCapWrapper.ManagedVideoCapture.SetVideoWindow(HWND__)' 的最佳重载方法匹配有一些无效参数 D:\DirectShow_Capture_GUI\DirectShow_Capture_GUI\Form1.cs 44 13 DirectShow_Capture_GUI错误 3 参数 1:无法从“System.Windows.Forms.Panel”转换为“HWND__” D:\DirectShow_Capture_GUI\DirectShow_Capture_GUI\Form1.cs 44 32 DirectShow_Capture_GUI`

谁能告诉我如何将面板传递给 dll,(任何示例都可以)?(抱歉,我是 .net 的新手,但我正在尝试创建一个示例应用程序,它显示可用的设备,如集成网络摄像头......然后在 c#.net 表单面板上显示预览)

编辑:感谢@Blachshma 和@Hans Passant,现在我可以将 C# Windows 窗体的面板传递到我的 C++ DLL。

我把dll中的func改成了

void VideoCapture::SetVideoWindow(IntPtr windowHandle)
{
VideoWindow = (HWND)windowHandle.ToPointer();
}

在 C# 中我称它为cc.SetVideoWindow(panel1.Handle);

最佳答案

您必须小心,不要将 HWND 之类的基本非托管类型暴露给 C# 代码。 C# 编译器不允许您传递此类类型的值。这里正确的互操作类型是 IntPtr,它可以存储句柄值。所以让你的 C++/CLI 方法看起来像这样:

void VideoCapture::SetVideoWindow(IntPtr windowHandle)
{
VideoWindow = (HWND)windowHandle.ToPointer();
}

您现在可以简单地将 panel1.Handle 传递给同样是 IntPtr 类型的方法。

关于c# - 将 c# windows 面板转换为 c HWND,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13821680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com