获取更新剪贴板应用程序的进程ID或名称的方法
来源:stackoverflow
时间:2024-02-05 22:51:22 140浏览 收藏
一分耕耘,一分收获!既然打开了这篇文章《获取更新剪贴板应用程序的进程ID或名称的方法》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!
我正在用 c# 创建剪贴板管理器,有时我会遇到剪贴板被某些应用程序设置为空的情况。
这发生在例如excel 取消选择刚刚复制的内容时,因此我需要确定剪贴板是否为空,但是如何获取更新剪贴板的应用程序名称?
我希望我能以某种方式获得更新剪贴板的应用程序的 hwnd
句柄,以便我可以使用以下代码查找其背后的进程:
[dllimport("user32.dll", setlasterror = true)] public static extern uint getwindowthreadprocessid(intptr hwnd, out uint lpdwprocessid); ... protected override void wndproc(ref message m) { switch (m.msg) { case wm_clipboardupdate: // how to get the "handle" hwnd? intptr handle = ??? <============= how to get this one ??? // get the process id from the hwnd uint processid = 0; getwindowthreadprocessid(handle, out processid); // get the process name from the process id string processname = process.getprocessbyid((int)processid).processname; console.writeline("clipboard update event from [" + processname + "]"); break; } default: base.wndproc(ref m); break; } }
我希望我可以使用 message
对象中的 hwnd
,但这似乎是我自己的应用程序 - 可能是用此进程 id 通知应用程序:
如果我可以通过其他方式获得它,那么这当然也完全没问题,但我将不胜感激对此的任何见解:-)
解决方案
根据@jimi的回答,这很简单。我可以将以下 3 行添加到我的原始代码中:
// Import the "GetClipboardOwner" function from the User32 library [DllImport("user32.dll")] public static extern IntPtr GetClipboardOwner(); ... // Replace the original line with "HOW TO GET THIS ONE" with this line below - this will give the HWnd handle for the application that has changed the clipboard: IntPtr handle = GetClipboardOwner();
正确答案
您可以调用getclipboardowner() 获取上次设置或清除剪贴板(触发通知的操作)的窗口句柄。
[...] 一般来说,剪贴板所有者是最后将数据放入剪贴板的窗口。
emptyclipboard 函数分配剪贴板所有权。
在某些特殊情况下,进程会将空句柄传递给 openclipboard():阅读此函数的备注部分和 emptyclipboard 函数。
在调用 emptyclipboard 之前,应用程序必须打开剪贴板 通过使用 openclipboard 函数。如果应用程序指定 打开剪贴板时窗口句柄为null,emptyclipboard成功 但将剪贴板所有者设置为 null。请注意,这会导致 setclipboarddata 失败。
▶ 这里我使用的是 nativewindow 派生类来设置剪贴板侦听器。处理剪贴板更新消息的窗口是通过初始化 创建的createparams 对象并将此参数传递给 nativewindow.createhandle(createparams) 方法,用于创建一个不可见窗口。
然后重写初始化的nativewindow的wndproc
,接收 wm_clipboardupdate
通知。
addclipboardformatlistener 函数用于将窗口放置在系统剪贴板侦听器链中。
▶ clipboardupdatemonitor
类在收到剪贴板通知时生成一个事件。事件中传递的自定义 clipboardchangedeventargs
对象包含由 getclipboardowner()
返回的剪贴板所有者的句柄、由 getwindowthreadprocessid() 和进程名称,由 process.getprocessbyid()。
您可以像这样设置 clipboardupdatemonitor
对象:
该类也可以在program.cs
中初始化
private clipboardupdatemonitor clipboardmonitor = null; // [...] clipboardmonitor = new clipboardupdatemonitor(); clipboardmonitor.clipboardchangednotify += this.clipboardchanged; // [...] private void clipboardchanged(object sender, clipboardchangedeventargs e) { console.writeline(e.processid); console.writeline(e.processname); console.writeline(e.threadid); }
using system.diagnostics; using system.runtime.interopservices; using system.security.permissions; using system.windows.forms; public sealed class clipboardupdatemonitor : idisposable { private bool isdisposed = false; private static clipboardwindow window = null; public event eventhandlerclipboardchangednotify; public clipboardupdatemonitor() { window = new clipboardwindow(); if (!nativemethods.addclipboardformatlistener(window.handle)) { throw new typeinitializationexception(nameof(clipboardwindow), new exception("clipboardformatlistener could not be initialized")); } window.clipboardchanged += clipboardchangedevent; } private void clipboardchangedevent(object sender, clipboardchangedeventargs e) => clipboardchangednotify?.invoke(this, e); public void dispose() { if (!isdisposed) { // cannot allow to throw exceptions here: add more checks to verify that // the nativewindow still exists and its handle is a valid handle nativemethods.removeclipboardformatlistener(window.handle); window?.destroyhandle(); isdisposed = true; } } ~clipboardupdatemonitor() => dispose(); private class clipboardwindow : nativewindow { public event eventhandler clipboardchanged; public clipboardwindow() { new securitypermission(securitypermissionflag.unmanagedcode).demand(); var cp = new createparams(); cp.caption = "clipboardwindow"; cp.height = 100; cp.width = 100; cp.parent = intptr.zero; cp.style = nativemethods.ws_clipchildren; cp.exstyle = nativemethods.ws_ex_controlparent | nativemethods.ws_ex_toolwindow; this.createhandle(cp); } protected override void wndproc(ref message m) { switch (m.msg) { case nativemethods.wm_clipboardupdate: intptr owner = nativemethods.getclipboardowner(); var threadid = nativemethods.getwindowthreadprocessid(owner, out uint processid); string processname = string.empty; if (processid != 0) { using (var proc = process.getprocessbyid((int)processid)) { processname = proc?.processname; } } clipboardchanged?.invoke(null, new clipboardchangedeventargs(processid, processname, threadid)); m.result = intptr.zero; break; default: base.wndproc(ref m); break; } } } }
自定义 eventargs
对象,用于携带收集的有关剪贴板所有者的信息:
public class clipboardchangedeventargs : eventargs { public clipboardchangedeventargs(uint processid, string processname, uint threadid) { this.processid = processid; this.processname = processname; this.threadid = threadid; } public uint processid { get; } public string processname { get; } public uint threadid { get; } }
nativemethods
类:
internal static class NativeMethods { [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool AddClipboardFormatListener(IntPtr hwnd); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool RemoveClipboardFormatListener(IntPtr hwnd); [DllImport("user32.dll")] internal static extern IntPtr GetClipboardOwner(); [DllImport("user32.dll", SetLastError = true)] internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); internal const int WM_CLIPBOARDUPDATE = 0x031D; internal const int WS_CLIPCHILDREN = 0x02000000; internal const int WS_EX_TOOLWINDOW = 0x00000080; internal const int WS_EX_CONTROLPARENT = 0x00010000; }
今天关于《获取更新剪贴板应用程序的进程ID或名称的方法》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习