C# 使用WinApi操作剪切板Clipboard

前言:

最近正好写一个程序,需要操作剪切板

功能很简单,只需要从剪切板内读取字符串,然后清空剪切板,然后再把字符串导入剪切板

我想当然的使用我最拿手的C#来完成这项工作,原因无他,因为.Net框架封装了能实现这种功能的方法

然后就有了如下代码

 1             string Temp = "";
 2             while (true)
 3             {
 4                 string Tex = Clipboard.GetText().ToString();
 5                 if (!string.IsNullOrWhiteSpace(Tex) && Temp != Tex)
 6                 {
 7                     Clipboard.Clear();
 8                     Clipboard.SetDataObject(Tex, false);
 9                     Temp = Tex;
10                 }
11                 Thread.Sleep(1);
12             }
View Code

这段代码,也是网页上广泛流传的,使用.Net框架操作系统剪切板的方法,当然这个方法在某些情况下很管用

不过在我这确发生了点问题,主要的问题有两点

首先,我对剪切板的操作需求有实时性,也就是,操作人员复制的一瞬间就应该截取到剪切板的数据,处理完后再放入剪切板

结果

 Clipboard.SetDataObject(Tex, false);

没想到上面这条设置剪切板的指令竟然会卡焦点窗口的线程,比如说,我在A软件执行了一次复制操作,如果使用了上述代码,那么A软件强制线程堵塞大概几百毫秒的样子,反正很影响体验,我推测是因为该命令会锁定内存导致的

那怎么办,本着死马当活马医的态度,我专门为该指令启用了一个线程

       Task.Factory.StartNew(()=> {Clipboard.Clear();Clipboard.SetDataObject(Text, false);});

使用了线程以后,因为操作滞后(线程启动会延迟一会儿,并不实时)了,所以上述问题似乎解决了,但是没想到出现了新的问题

 string Tex = Clipboard.GetText().ToString();

上述从剪切板获得字符串的指令,在默写情况下,会卡滞住,然后程序在一分钟之后,因为超时而被系统吊销

emmmmm,在经过几番努力之后,我终于意识到,虽然.Net封装了不少操作系统API的方法,使得一些IO操作变简单不少,但是带来的问题也是同样大的,在遇到无法解决的问题的时候,会有点束手无策

于是不得已,我只能放弃使用过C#完成该项功能,想着幸好功能简单,而且操作WinAPI其实最好的还是使用C++来写,于是我用C++复现了上述功能

 1 #include "stdafx.h"
 2 #include <windows.h>
 3 #include <iostream>
 4 using namespace std;
 5 #pragma comment(linker,"/subsystem:windows /entry:mainCRTStartup")
 6 
 7 int main(int argc, _TCHAR* argv[])
 8 {
 9     HANDLE THandle = GlobalAlloc(GMEM_FIXED, 1000);//分配内存
10     char* Temp = (char*)THandle;//锁定内存,返回申请内存的首地址
11     while (true)
12     {
13         HWND hWnd = NULL;
14         OpenClipboard(hWnd);//打开剪切板
15         if (IsClipboardFormatAvailable(CF_TEXT))
16         {
17             HANDLE h = GetClipboardData(CF_TEXT);//获取剪切板数据
18             char* p = (char*)GlobalLock(h);
19             GlobalUnlock(h);
20             if (strcmp(Temp, p))
21             {
22                 EmptyClipboard();//清空剪切板
23                 HANDLE hHandle = GlobalAlloc(GMEM_FIXED, 1000);//分配内存
24                 char* pData = (char*)GlobalLock(hHandle);//锁定内存,返回申请内存的首地址
25                 strcpy(pData, p);
26                 strcpy(Temp, p);
27                 SetClipboardData(CF_TEXT, hHandle);//设置剪切板数据
28                 GlobalUnlock(hHandle);//解除锁定
29             }
30         }
31         CloseClipboard();//关闭剪切板
32         Sleep(500);
33     }
34     return 0;
35 }
View Code

不愧是C++,使用上述代码后,完美实现我需要的功能,而且不管是主程序,还是我写的这个程序,都不会出现卡滞或者不工作的情况了,真是可喜可贺。

那么本教程就到此为止。

 

 

以下是正文

想着,既然我能用C++调用WinAPI完美实现我需要的功能,而且C#也能调用非托管的代码来执行WinAPI,那么我不是可以把上面C++写的代码移植到C#里面执行?说干就干

首先,C#调用WinAPI需要先申明

        [DllImport("User32")]internal static extern bool OpenClipboard(IntPtr hWndNewOwner);[DllImport("User32")]internal static extern bool CloseClipboard();[DllImport("User32")]internal static extern bool EmptyClipboard();[DllImport("User32")]internal static extern bool IsClipboardFormatAvailable(int format);[DllImport("User32")]internal static extern IntPtr GetClipboardData(int uFormat);[DllImport("User32", CharSet = CharSet.Unicode)]internal static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem);

操作剪切板需要调用的API大致就上面这些

有了API以后,我们还需要自己手动封装方法

     internal static void SetText(string text){if (!OpenClipboard(IntPtr.Zero))
        {
        SetText(text);
        return;
        }EmptyClipboard();SetClipboardData(
13, Marshal.StringToHGlobalUni(text));CloseClipboard();}internal static string GetText(int format){string value = string.Empty;OpenClipboard(IntPtr.Zero);if (IsClipboardFormatAvailable(format)){IntPtr ptr = NativeMethods.GetClipboardData(format);if (ptr != IntPtr.Zero){value = Marshal.PtrToStringUni(ptr);}}CloseClipboard();return value;}

我们也就用到两个方法,从剪切板获得文本和设置文本到剪切板,哦关于SetClipboardData的第一个参数13是怎么来的问题,其实这个剪切板的格式参数,下面有一张表,就是自从这里来的

public static class ClipboardFormat
{/// <summary>/// Text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals/// the end of the data. Use this format for ANSI text./// </summary>public const int CF_TEXT = 1;/// <summary>/// A handle to a bitmap (<c>HBITMAP</c>)./// </summary>public const int CF_BITMAP = 2;/// <summary>/// Handle to a metafile picture format as defined by the <c>METAFILEPICT</c> structure. When passing a/// <c>CF_METAFILEPICT</c> handle by means of DDE, the application responsible for deleting <c>hMem</c> should/// also free the metafile referred to by the <c>CF_METAFILEPICT</c> handle./// </summary>public const int CF_METAFILEPICT = 3;/// <summary>/// Microsoft Symbolic Link (SYLK) format./// </summary>public const int CF_SYLK = 4;/// <summary>/// Software Arts' Data Interchange Format./// </summary>public const int CF_DIF = 5;/// <summary>/// Tagged-image file format./// </summary>public const int CF_TIFF = 6;/// <summary>/// Text format containing characters in the OEM character set. Each line ends with a carriage return/linefeed/// (CR-LF) combination. A null character signals the end of the data./// </summary>public const int CF_OEMTEXT = 7;/// <summary>/// A memory object containing a <c>BITMAPINFO</c> structure followed by the bitmap bits./// </summary>public const int CF_DIB = 8;/// <summary>/// Handle to a color palette. Whenever an application places data in the clipboard that depends on or assumes/// a color palette, it should place the palette on the clipboard as well. If the clipboard contains data in/// the <see cref="CF_PALETTE"/> (logical color palette) format, the application should use the/// <c>SelectPalette</c> and <c>RealizePalette</c> functions to realize (compare) any other data in the/// clipboard against that logical palette. When displaying clipboard data, the clipboard always uses as its/// current palette any object on the clipboard that is in the <c>CF_PALETTE</c> format./// </summary>public const int CF_PALETTE = 9;/// <summary>/// Data for the pen extensions to the Microsoft Windows for Pen Computing./// </summary>public const int CF_PENDATA = 10;/// <summary>/// Represents audio data more complex than can be represented in a CF_WAVE standard wave format./// </summary>public const int CF_RIFF = 11;/// <summary>/// Represents audio data in one of the standard wave formats, such as 11 kHz or 22 kHz PCM./// </summary>public const int CF_WAVE = 12;/// <summary>/// Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character/// signals the end of the data./// </summary>public const int CF_UNICODETEXT = 13;/// <summary>/// A handle to an enhanced metafile (<c>HENHMETAFILE</c>)./// </summary>public const int CF_ENHMETAFILE = 14;/// <summary>/// A handle to type <c>HDROP</c> that identifies a list of files. An application can retrieve information/// about the files by passing the handle to the <c>DragQueryFile</c> function./// </summary>public const int CF_HDROP = 15;/// <summary>/// The data is a handle to the locale identifier associated with text in the clipboard. When you close the/// clipboard, if it contains <c>CF_TEXT</c> data but no <c>CF_LOCALE</c> data, the system automatically sets/// the <c>CF_LOCALE</c> format to the current input language. You can use the <c>CF_LOCALE</c> format to/// associate a different locale with the clipboard text./// An application that pastes text from the clipboard can retrieve this format to determine which character/// set was used to generate the text./// Note that the clipboard does not support plain text in multiple character sets. To achieve this, use a/// formatted text data type such as RTF instead. /// The system uses the code page associated with <c>CF_LOCALE</c> to implicitly convert from/// <see cref="CF_TEXT"/> to <see cref="CF_UNICODETEXT"/>. Therefore, the correct code page table is used for/// the conversion./// </summary>public const int CF_LOCALE = 16;/// <summary>/// A memory object containing a <c>BITMAPV5HEADER</c> structure followed by the bitmap color space/// information and the bitmap bits./// </summary>public const int CF_DIBV5 = 17;/// <summary>/// Owner-display format. The clipboard owner must display and update the clipboard viewer window, and receive/// the <see cref="ClipboardMessages.WM_ASKCBFORMATNAME"/>, <see cref="ClipboardMessages.WM_HSCROLLCLIPBOARD"/>,/// <see cref="ClipboardMessages.WM_PAINTCLIPBOARD"/>, <see cref="ClipboardMessages.WM_SIZECLIPBOARD"/>, and/// <see cref="ClipboardMessages.WM_VSCROLLCLIPBOARD"/> messages. The <c>hMem</c> parameter must be <c>null</c>./// </summary>public const int CF_OWNERDISPLAY = 0x0080;/// <summary>/// Text display format associated with a private format. The <c>hMem</c> parameter must be a handle to data/// that can be displayed in text format in lieu of the privately formatted data./// </summary>public const int CF_DSPTEXT = 0x0081;/// <summary>/// Bitmap display format associated with a private format. The <c>hMem</c> parameter must be a handle to/// data that can be displayed in bitmap format in lieu of the privately formatted data./// </summary>public const int CF_DSPBITMAP = 0x0082;/// <summary>/// Metafile-picture display format associated with a private format. The <c>hMem</c> parameter must be a/// handle to data that can be displayed in metafile-picture format in lieu of the privately formatted data./// </summary>public const int CF_DSPMETAFILEPICT = 0x0083;/// <summary>/// Enhanced metafile display format associated with a private format. The <c>hMem</c> parameter must be a/// handle to data that can be displayed in enhanced metafile format in lieu of the privately formatted data./// </summary>public const int CF_DSPENHMETAFILE = 0x008E;/// <summary>/// Start of a range of integer values for application-defined GDI object clipboard formats. The end of the/// range is <see cref="CF_GDIOBJLAST"/>. Handles associated with clipboard formats in this range are not/// automatically deleted using the <c>GlobalFree</c> function when the clipboard is emptied. Also, when using/// values in this range, the <c>hMem</c> parameter is not a handle to a GDI object, but is a handle allocated/// by the <c>GlobalAlloc</c> function with the <c>GMEM_MOVEABLE</c> flag./// </summary>public const int CF_GDIOBJFIRST = 0x0300;/// <summary>/// See <see cref="CF_GDIOBJFIRST"/>./// </summary>public const int CF_GDIOBJLAST = 0x03FF;/// <summary>/// Start of a range of integer values for private clipboard formats. The range ends with/// <see cref="CF_PRIVATELAST"/>. Handles associated with private clipboard formats are not freed/// automatically; the clipboard owner must free such handles, typically in response to the/// <see cref="ClipboardMessages.WM_DESTROYCLIPBOARD"/> message./// </summary>public const int CF_PRIVATEFIRST = 0x0200;/// <summary>/// See <see cref="CF_PRIVATEFIRST"/>./// </summary>public const int CF_PRIVATELAST = 0x02FF;
}
View Code

在C++里面是不用指定数字的,只需要用CF_UNICODETEXT就行,不过.Net里面应该没有对应的索引表,所以只能手动输入(我这里是为了说明用才专门用数字,自己代码那是索引的枚举类)

上面两个工作做完以后,就能实现功能了,功能代码如下

                   var LastS = string.Empty;while (!CancelInfoClipboard.IsCancellationRequested){var Temp = ClipboardControl.GetText(ClipboardFormat.CF_UNICODETEXT);if (!string.IsNullOrEmpty(Temp) && Temp != LastS){ClipboardControl.SetText(Temp);LastS = Temp;}Thread.Sleep(50);}

是不是和最开始展示的调用.Net框架的方法一模一样(笑),不过使用底层API实现的功能,就没有那么多乱七八糟的Bug了,自己也很清楚到底实现了啥功能,同时也收获了不少新知识(主要是非托管代码调用的时候的注意事项什么的,还有,向非托管代码传递数据的时候,最好多用Marshal类里面的方法,不然可能会出错,毕竟这个类就是专门为非托管代码而设立的)

接下来是新的发现

在研究MSDN上面关于剪切板的API的时候,发现了一个函数

bool AddClipboardFormatListener(HWND hwnd);

根据描述来讲,是添加一个剪切板的监控,在剪切板有任何变动的时候,通知你所指定的句柄的窗口,我一想,这不就是我所需要的么,有了这么一个API以后,其实我上面所展示的,使用死循环轮询剪切板的方法就变得很傻逼,而且也很容易出错了,于是,基于这个新发现的API,我重新更改了全部的程序逻辑,反而比原先的实现更加简单了。

首先我们需要一个新的窗口或者控件来接收Windows消息更新后所发来的消息,只要New 一个form就行

        public Form2(){InitializeComponent();AddClipboardFormatListener(this.Handle);}

然后我们在初始化组件的命令后面,把使用添加剪切板监听的API把当前窗口的句柄发给系统,这样系统在接收到剪切板改变的命令后,会把消息发给当前窗口

然后我们需要复写WndProc方法

   protected override void WndProc(ref Message m){if (m.Msg == 0x031D && Onice){var Temp = ClipboardControl.GetText(ClipboardFormat.CF_UNICODETEXT);if (!string.IsNullOrEmpty(Temp)){ClipboardControl.SetText(Temp);Onice = false;}}else if (!Onice){Onice = true;}else{base.WndProc(ref m);}}
   private bool Onice = true;

首先WndProc如果是Form类下面一个专门用来接收系统发送过来的消息的方法

然后关于m.Msg == 0x031D的0x031D在WinAPI定义上的意义是WM_CLIPBOARDUPDATE ,也就是剪切板更新事件,这个通过查找MSDN能够找到

下面没有特别奇怪的函数,就是有一点需要注意,我们这里设置了剪切板数据,相当于进行了一次更新,所以会在这一瞬间再次产生剪切板更新事件,然后又会通知这个方法,然后就会形成死循环,我在这里用了一个布尔判断来通过布尔状态决定是否截取剪切板,不只有有没有更好的办法来实现

以上

转载于:https://www.cnblogs.com/ACDIV/p/9114472.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390357.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

聊聊spring cloud gateway的XForwardedHeadersFilter

序 本文主要研究spring cloud gateway的XForwardedHeadersFilter GatewayAutoConfiguration spring-cloud-gateway-core-2.0.0.RC1-sources.jar!/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java Configuration ConditionalOnProperty(name "sp…

node缓冲区_Node.js缓冲区介绍

node缓冲区什么是缓冲液&#xff1f; (What are Buffers?) Binary is simply a set or a collection of 1 and 0. Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. Fo…

专访赵加雨:WebRTC在网易云信的落地

去年的这个时候&#xff0c;在市面上公开表示使用WebRTC的公司还没几家&#xff0c;但2018年以来&#xff0c;宣布采用或支持WebRTC的公司已经越来越多。实时音视频提供商网易云信也在自研的NRTC中集成了WebRTC。在他们眼里&#xff0c;2017年是WebRTC的转折之年&#xff0c;而…

html/css杂题

1、css选择器&#xff1a;详细&#xff08;http://www.ruanyifeng.com/blog/2009/03/css_selectors.html&#xff09; 派生选择器&#xff1a;按标签 类别选择器&#xff1a;按class ID选择器&#xff1a;按ID 通用选择器&#xff1a;* 匹配所有 属性选择器&#xff1a;按属性&…

黑客马拉松 招募_我如何赢得第一次黑客马拉松-研究,设计和编码的2个狂野日子

黑客马拉松 招募I had no coding or engineering background. I studied biology in college, with no clue about what to do with my degree. 我没有编码或工程背景。 我在大学学习生物学&#xff0c;但不知道如何处理我的学位。 My first jobs were making cold calls in s…

1、Linux命令随笔

1 Linux命令总结2 3 man 命令帮助;4 help 命令的帮助&#xff08;bash的内置命令&#xff09;;5 ls list,查看目录列表;6 -ld&#xff1a;查看目录权限;7 -l:(long)长格式显示属性;8 -F:给不同的文件类型结尾加标识9 -p:给目录加斜线10 …

1137. 第 N 个泰波那契数

泰波那契序列 Tn 定义如下&#xff1a; T0 0, T1 1, T2 1, 且在 n > 0 的条件下 Tn3 Tn Tn1 Tn2 给你整数 n&#xff0c;请返回第 n 个泰波那契数 Tn 的值。 示例 1&#xff1a; 输入&#xff1a;n 4 输出&#xff1a;4 解释&#xff1a; T_3 0 1 1 2 T_4 1…

web图像_Web图像优化的基本介绍

web图像Images are an essential ingredient of most websites. The visual quality of pictures has a direct impact on the brand image and the message those images convey. And the weight of images usually accounts for a 40-60% of the data transferred on the web…

ElasticSearch客户端注解使用介绍

The best elasticsearch highlevel java rest api-----bboss 1.ElasticSearch客户端bboss提供了一系列注解 ESId 用于标识实体对象中作为docid的属性&#xff0c;该注解只有一个persistent 布尔值属性&#xff0c;用于控制被本注解标注的字段属性是否作为普通文档属性保存&am…

5827. 检查操作是否合法

给你一个下标从 0 开始的 8 x 8 网格 board &#xff0c;其中 board[r][c] 表示游戏棋盘上的格子 (r, c) 。棋盘上空格用 ‘.’ 表示&#xff0c;白色格子用 ‘W’ 表示&#xff0c;黑色格子用 ‘B’ 表示。 游戏中每次操作步骤为&#xff1a;选择一个空格子&#xff0c;将它变…

团队的远程管理_远程团队指南:如何管理您的远程软件开发团队

团队的远程管理Guides to help you work remotely seem to have swept through the Internet these days. 这些天来&#xff0c;帮助您远程工作的指南似乎席卷了Internet。 Do this, avoid that, stay productive, and all those run-of-the-mill tips we’ve already tried o…

JS 正则 钱

function ValidateIsDecial(sValue) {return (!sValue && !!!sValue && /^[0-9]{1,10}(\.[0-9]{0,2})?$/.test(sValue)); };验证 decimal(12,2) 小数点前允许10位,小数点后允许2位 1234567890 true 12345678901 false 0123456789 true 01234567891 false 123.…

5193. 删除字符使字符串变好

5193. 删除字符使字符串变好 一个字符串如果没有 三个连续 相同字符&#xff0c;那么它就是一个 好字符串 。 给你一个字符串 s &#xff0c;请你从 s 删除 最少 的字符&#xff0c;使它变成一个 好字符串 。 请你返回删除后的字符串。题目数据保证答案总是 唯一的 。 示例 …

2020计算机顶级大会_2020年顶级远程调试工具

2020计算机顶级大会When it comes to debugging, the tool you use is extremely important and can determine how easy is is to fix problems within your code. 在调试方面&#xff0c;您使用的工具非常重要&#xff0c;可以确定在代码中修复问题的难易程度。 In the earl…

BZOJ5292 洛谷4457 LOJ2513:[BJOI2018]治疗之雨——题解

https://www.lydsy.com/JudgeOnline/problem.php?id5292 https://www.luogu.org/problemnew/show/P4457 https://loj.ac/problem/2513 你现在有m1个数&#xff1a;第一个为p&#xff0c;最小值为0&#xff0c;最大值为n&#xff1b;剩下m个都是无穷&#xff0c;没有最小值或最…

PHP--------微信网页开发实现微信扫码功能

今天说说微商城项目中用到的扫一扫这个功能&#xff0c;分享一下&#xff0c;希望对各位有所帮助。 前提&#xff1a;要有公众号&#xff0c;和通过微信认证&#xff0c;绑定域名&#xff0c;得到相应信息&#xff0c;appid&#xff0c;appsecret等。 微信开发文档&#xff1a;…

313. 超级丑数

超级丑数 是一个正整数&#xff0c;并满足其所有质因数都出现在质数数组 primes 中。 给你一个整数 n 和一个整数数组 primes &#xff0c;返回第 n 个 超级丑数 。 题目数据保证第 n 个 超级丑数 在 32-bit 带符号整数范围内。 示例 1&#xff1a; 输入&#xff1a;n 12,…

初创公司股本结构_我如何向初创公司的开发团队添加一些结构-以及从过程中学到的东西

初创公司股本结构Until recently, Id spent the last 4 years of my career at FinTech start-ups. Id always worked for smaller companies, and being at a start-up was the next logical step in looking for roles where I could make the biggest difference. 直到最近…

拿什么拯救你,我的面试之——从零打卡刷Leetcode(No.003)

写在前边&#xff1a;小詹一直觉得自己编程能力不强&#xff0c;想在网上刷题&#xff0c;又怕不能坚持。不知道有木有和小伙伴和小詹一样想找个人一起刷题呢&#xff1f;欢迎和小詹一起定期刷leetcode&#xff0c;每周一周五更新一题&#xff0c;每一题都吃透&#xff0c;欢迎…

146. LRU 缓存机制

146. LRU 缓存机制 运用你所掌握的数据结构&#xff0c;设计和实现一个 LRU (最近最少使用) 缓存机制 。 实现 LRUCache 类&#xff1a; LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存 int get(int key) 如果关键字 key 存在于缓存中&#xff0c;则返回…