C# 使用 hook 绕过新的 libvlc api

发布于 2024-12-14 10:22:05 字数 1791 浏览 4 评论 0 原文

我正在使用 vlc 捕获视频和音频流并将其显示在图片框中。新的 libvlc api 不再支持 Windows 中的双击/全屏,我需要拥有该功能。我在创建新表单、向其添加图片框并在其中显示视频时没有问题,但在捕获 vlc 窗口中的双击事件以告诉应用程序制作视频时确实遇到问题全屏馈送。我发现我需要使用钩子。我已经安装了钩子等等。我唯一的问题是,我只想在单击我的图片框之一时处理该消息。因此,从我的回调方法中,这就是我需要的:

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        {
            //Here I need to grab the Handle of the control that the mouse was clicked in.
            //Now I need to cast the Control.FromHandle() as PictureBox.
            // then if(control != null)
            // send the event to the form via. form.on_double_click or whatever.


        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

有什么想法吗?

更新

这是我现在得到的,看起来不错吗?

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        {
            MOUSEHOOKSTRUCT msg = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));

            PictureBox control = Control.FromHandle(msg.hwnd) as PictureBox;

            if (control != null)
            {
                PreviewForm.pbox_MouseDoubleClick(control, new MouseEventArgs(MouseButtons.Left, 2, msg.pt.x, msg.pt.y, 0));
            }                
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

更新

对于那些来自 Google 的人来说,还有一个小问题。在 Windows 7 中,如果将调试器附加到回调,它看起来会被破坏。钩子响应有一个超时值,如果超时过期,您的钩子在钩子的生命周期内将永远不会再次触发。根据我的阅读,这似乎是 Windows 7 问题,但它可以在 Windows Vista 及更低版本上运行。调试器中的断点肯定会强制超时到期,因此,您的回调只会被调用一次。但是,如果没有断点,它也能正常工作。

I am using vlc to capture a video and audio stream and display it in a picture box. The new libvlc api no longer supports the double click/fullscreen in windows and I need to have that functionality. I don't have a problem creating a new form, adding a picture box to it and showing the video in that, but I do have a problem capturing the double click event in the vlc window in order to tell the app to make the video feed fullscreen. I found out that I need to use a hook. I have the hook installed and all of that. My only problem is, I only want to handle the message if it is a click in one of my pictureboxes. So, from my callback method, here is what I need:

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        {
            //Here I need to grab the Handle of the control that the mouse was clicked in.
            //Now I need to cast the Control.FromHandle() as PictureBox.
            // then if(control != null)
            // send the event to the form via. form.on_double_click or whatever.


        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

Any ideas?

Ubdate:

Here is what I've got now, look good?

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 &&  MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        {
            MOUSEHOOKSTRUCT msg = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));

            PictureBox control = Control.FromHandle(msg.hwnd) as PictureBox;

            if (control != null)
            {
                PreviewForm.pbox_MouseDoubleClick(control, new MouseEventArgs(MouseButtons.Left, 2, msg.pt.x, msg.pt.y, 0));
            }                
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

Update

Another little gotcha for those coming in from Google. In Windows 7, If you attach your debugger to the callback, it will appear to be broken. There is a timeout value on hook responses, if that timeout ever expires, your hook will never fire again for the life of the hook. From my reading, it appears that this is a Windows 7 issue while it works on Windows Vista and less. The break point in your debugger will most certainly force this timeout to expire and as a result, your callback will only be called once. However, it will work fine without the break point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一张白纸 2024-12-21 10:22:05

您的 lParam 是指向 MOUSEHOOKSTRUCT 将包含窗口句柄。

您可以使用定义 a Pinvoke.net 并将 lParam 编组为该类型。

Your lParam is a pointer to a MOUSEHOOKSTRUCT which will contain the window handle.

You can use the definition a Pinvoke.net and marshal lParam to that type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文