我找到了这段代码:
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/