c# HwndSourceHook 与 Windows.Forms

标签 c# hwnd

我找到了这段代码:

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;

        if (hwndSource != null)
        {
            installedHandle = hwndSource.Handle;
            viewerHandle = SetClipboardViewer(installedHandle);
            hwndSource.AddHook(new HwndSourceHook(this.hwndSourceHook));
        }
    }

启动 hwndSourceHook(捕获剪贴板)。但是此代码仅适用于“Window”,不适用于“Windows.Form”。

如何获取我的表单的 hwndSource 以添加 hwndSourceHook?

(我认为我应该使用 Form_Load 函数而不是覆盖...)

编辑:谢谢,但是表单没有添加我的 hwndSourceHook 的 AddHook 函数

最佳答案

如果您使用的是 WinForms,那么它只是 myForm.Handle

HwndSource 用于 WPF。

所以你可以这样做:

viewerHandle = SetClipboardViewer(myForm.Handle);

编辑:AddHook 也是一种 WPF 方法。

您需要使用:

Application.AddMessageFilter(...);

或者,在您的 Form 类中覆盖 WndProc 方法:

protected override void WndProc(ref Message m) {...}

AddMessageFilter 可以捕获应用程序中任何窗口的消息,而 WndProc 只会接收给定窗口的消息。

关于c# HwndSourceHook 与 Windows.Forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/845240/

相关文章:

c# - 用于指定 ComparisonMode 的枚举

java - .NET REST API 与 Vert.x

c# - 反转简单字符串数组

c# - 访问泛型类型的静态嵌套类

c# - Windows Powershell SDK 和 System.Management.Automation.PSObject

ms-access - 我如何知道单击了多选 Microsoft Access 列表框中的哪个项目?

c++ - 是否有任何应用程序具有 HWND?

multithreading - 非窗口类中的自定义消息 - 需要默认处理程序吗?

c++ - Win32 在应用程序内使用资源字体

c++ - 如何在 C++ 中将 HWND 转换为 LPCSTR