获取更新剪贴板应用程序的进程ID或名称的方法
一分耕耘,一分收获!既然打开了这篇文章《获取更新剪贴板应用程序的进程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学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

- 上一篇
- 将 BST 和 GMT 时区的客户端处理,以 UTC 格式存储在数据库中的方法是什么?

- 下一篇
- Postgres 数据库中使用 Gorm 传入的查询数据类型错误
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 477浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 美图AI抠图
- 美图AI抠图,依托CVPR 2024竞赛亚军技术,提供顶尖的图像处理解决方案。适用于证件照、商品、毛发等多场景,支持批量处理,3秒出图,零PS基础也能轻松操作,满足个人与商业需求。
- 13次使用
-
- PetGPT
- SEO摘要PetGPT 是一款基于 Python 和 PyQt 开发的智能桌面宠物程序,集成了 OpenAI 的 GPT 模型,提供上下文感知对话和主动聊天功能。用户可高度自定义宠物的外观和行为,支持插件热更新和二次开发。适用于需要陪伴和效率辅助的办公族、学生及 AI 技术爱好者。
- 15次使用
-
- 可图AI图片生成
- 探索快手旗下可灵AI2.0发布的可图AI2.0图像生成大模型,体验从文本生成图像、图像编辑到风格转绘的全链路创作。了解其技术突破、功能创新及在广告、影视、非遗等领域的应用,领先于Midjourney、DALL-E等竞品。
- 43次使用
-
- MeowTalk喵说
- MeowTalk喵说是一款由Akvelon公司开发的AI应用,通过分析猫咪的叫声,帮助主人理解猫咪的需求和情感。支持iOS和Android平台,提供个性化翻译、情感互动、趣味对话等功能,增进人猫之间的情感联系。
- 40次使用
-
- Traini
- SEO摘要Traini是一家专注于宠物健康教育的创新科技公司,利用先进的人工智能技术,提供宠物行为解读、个性化训练计划、在线课程、医疗辅助和个性化服务推荐等多功能服务。通过PEBI系统,Traini能够精准识别宠物狗的12种情绪状态,推动宠物与人类的智能互动,提升宠物生活质量。
- 38次使用
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- Golang取得代码运行时间的问题
- 2023-02-24 501浏览
-
- 请问 go 代码如何实现在代码改动后不需要Ctrl+c,然后重新 go run *.go 文件?
- 2023-01-08 501浏览
-
- 如何从同一个 io.Reader 读取多次
- 2023-04-11 501浏览