XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.InputSimulator

【DLL】XFE各类拓展的模拟键盘输入

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.InputSimulator

添加剪切板操作方法

f838b78
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

2 个文件 +302 -0
Added XFEExtension.NetCore.InputSimulator/Clipboard.cs +129 -0
@@ -0,0 +1,129 @@
1 using System.Runtime.InteropServices;
2 using System.Runtime.Versioning;
3 using System.Text;
4
5 namespace XFEExtension.NetCore.InputSimulator;
6
7 /// <summary>
8 /// 剪切板操作类
9 /// </summary>
10 [SupportedOSPlatform("windows")]
11 public static partial class Clipboard
12 {
13 /// <summary>
14 /// 打开剪贴板以进行检查,并防止其他应用程序修改剪贴板内容
15 /// </summary>
16 /// <param name="hWndNewOwner"></param>
17 /// <returns></returns>
18 [LibraryImport("user32.dll")]
19 [return: MarshalAs(UnmanagedType.Bool)]
20 public static partial bool OpenClipboard(IntPtr hWndNewOwner);
21
22 /// <summary>
23 /// 清空剪贴板内容
24 /// </summary>
25 /// <returns></returns>
26 [LibraryImport("user32.dll")]
27 [return: MarshalAs(UnmanagedType.Bool)]
28 public static partial bool EmptyClipboard();
29
30 /// <summary>
31 /// 将数据放入剪贴板
32 /// </summary>
33 /// <param name="uFormat"></param>
34 /// <param name="data"></param>
35 /// <returns></returns>
36 [LibraryImport("user32.dll")]
37 private static partial IntPtr SetClipboardData(uint uFormat, IntPtr data);
38
39 /// <summary>
40 /// 获取剪贴板数据
41 /// </summary>
42 /// <param name="uFormat"></param>
43 /// <returns></returns>
44 [LibraryImport("user32.dll")]
45 private static partial IntPtr GetClipboardData(uint uFormat);
46
47 /// <summary>
48 /// 关闭剪贴板
49 /// </summary>
50 /// <returns></returns>
51 [LibraryImport("user32.dll")]
52 [return: MarshalAs(UnmanagedType.Bool)]
53 public static partial bool CloseClipboard();
54
55 /// <summary>
56 /// 分配内存
57 /// </summary>
58 /// <param name="uFlags"></param>
59 /// <param name="dwBytes"></param>
60 /// <returns></returns>
61 [LibraryImport("kernel32.dll", SetLastError = true)]
62 public static partial IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
63
64 /// <summary>
65 /// 锁定内存
66 /// </summary>
67 /// <param name="hMem"></param>
68 /// <returns></returns>
69 [LibraryImport("kernel32.dll", SetLastError = true)]
70 public static partial IntPtr GlobalLock(IntPtr hMem);
71
72 /// <summary>
73 /// 解锁内存
74 /// </summary>
75 /// <param name="hMem"></param>
76 /// <returns></returns>
77 [LibraryImport("kernel32.dll", SetLastError = true)]
78 [return: MarshalAs(UnmanagedType.Bool)]
79 public static partial bool GlobalUnlock(IntPtr hMem);
80
81 /// <summary>
82 /// 设置剪贴板内容
83 /// </summary>
84 /// <param name="text"></param>
85 /// <param name="format"></param>
86 /// <returns></returns>
87 public static bool SetClipboardContent(string text, uint format = ClipboardFormat.CF_UNICODETEXT)
88 {
89 if (!OpenClipboard(IntPtr.Zero))
90 return false;
91 EmptyClipboard();
92 IntPtr hGlobal = GlobalAlloc(0x2000, (UIntPtr)((text.Length + 1) * 2));
93 IntPtr pGlobal = GlobalLock(hGlobal);
94 byte[] bytes = Encoding.Unicode.GetBytes(text);
95 Marshal.Copy(bytes, 0, pGlobal, bytes.Length);
96 GlobalUnlock(hGlobal);
97 SetClipboardData(format, hGlobal);
98 CloseClipboard();
99 return true;
100 }
101
102 /// <summary>
103 /// 获取剪贴板内容
104 /// </summary>
105 /// <param name="format"></param>
106 /// <returns></returns>
107 public static object? GetClipboardContent(uint format = ClipboardFormat.CF_UNICODETEXT)
108 {
109 if (!OpenClipboard(IntPtr.Zero))
110 return null;
111 IntPtr hClipboardData = GetClipboardData(format);
112
113 if (hClipboardData == IntPtr.Zero)
114 {
115 CloseClipboard();
116 return null;
117 }
118 IntPtr pClipboardData = GlobalLock(hClipboardData);
119 if (pClipboardData == IntPtr.Zero)
120 {
121 CloseClipboard();
122 return null;
123 }
124 var content = Marshal.PtrToStringAnsi(pClipboardData);
125 GlobalUnlock(hClipboardData);
126 CloseClipboard();
127 return content;
128 }
129 }
Added XFEExtension.NetCore.InputSimulator/ClipboardFormat.cs +173 -0
@@ -0,0 +1,173 @@
1 namespace XFEExtension.NetCore.InputSimulator;
2
3 /// <summary>
4 /// 剪切板数据格式
5 /// </summary>
6 public static class ClipboardFormat
7 {
8 /// <summary>
9 /// Text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character signals
10 /// the end of the data. Use this format for ANSI text.
11 /// </summary>
12 public const uint CF_TEXT = 1;
13
14 /// <summary>
15 /// A handle to a bitmap (<c>HBITMAP</c>).
16 /// </summary>
17 public const uint CF_BITMAP = 2;
18
19 /// <summary>
20 /// Handle to a metafile picture format as defined by the <c>METAFILEPICT</c> structure. When passing a
21 /// <c>CF_METAFILEPICT</c> handle by means of DDE, the application responsible for deleting <c>hMem</c> should
22 /// also free the metafile referred to by the <c>CF_METAFILEPICT</c> handle.
23 /// </summary>
24 public const uint CF_METAFILEPICT = 3;
25
26 /// <summary>
27 /// Microsoft Symbolic Link (SYLK) format.
28 /// </summary>
29 public const uint CF_SYLK = 4;
30
31 /// <summary>
32 /// Software Arts' Data Interchange Format.
33 /// </summary>
34 public const uint CF_DIF = 5;
35
36 /// <summary>
37 /// Tagged-image file format.
38 /// </summary>
39 public const uint CF_TIFF = 6;
40
41 /// <summary>
42 /// Text format containing characters in the OEM character set. Each line ends with a carriage return/linefeed
43 /// (CR-LF) combination. A null character signals the end of the data.
44 /// </summary>
45 public const uint CF_OEMTEXT = 7;
46
47 /// <summary>
48 /// A memory object containing a <c>BITMAPINFO</c> structure followed by the bitmap bits.
49 /// </summary>
50 public const uint CF_DIB = 8;
51
52 /// <summary>
53 /// Handle to a color palette. Whenever an application places data in the clipboard that depends on or assumes
54 /// a color palette, it should place the palette on the clipboard as well. If the clipboard contains data in
55 /// the <see cref="CF_PALETTE"/> (logical color palette) format, the application should use the
56 /// <c>SelectPalette</c> and <c>RealizePalette</c> functions to realize (compare) any other data in the
57 /// clipboard against that logical palette. When displaying clipboard data, the clipboard always uses as its
58 /// current palette any object on the clipboard that is in the <c>CF_PALETTE</c> format.
59 /// </summary>
60 public const uint CF_PALETTE = 9;
61
62 /// <summary>
63 /// Data for the pen extensions to the Microsoft Windows for Pen Computing.
64 /// </summary>
65 public const uint CF_PENDATA = 10;
66
67 /// <summary>
68 /// Represents audio data more complex than can be represented in a CF_WAVE standard wave format.
69 /// </summary>
70 public const uint CF_RIFF = 11;
71
72 /// <summary>
73 /// Represents audio data in one of the standard wave formats, such as 11 kHz or 22 kHz PCM.
74 /// </summary>
75 public const uint CF_WAVE = 12;
76
77 /// <summary>
78 /// Unicode text format. Each line ends with a carriage return/linefeed (CR-LF) combination. A null character
79 /// signals the end of the data.
80 /// </summary>
81 public const uint CF_UNICODETEXT = 13;
82
83 /// <summary>
84 /// A handle to an enhanced metafile (<c>HENHMETAFILE</c>).
85 /// </summary>
86 public const uint CF_ENHMETAFILE = 14;
87
88 /// <summary>
89 /// A handle to type <c>HDROP</c> that identifies a list of files. An application can retrieve information
90 /// about the files by passing the handle to the <c>DragQueryFile</c> function.
91 /// </summary>
92 public const uint CF_HDROP = 15;
93
94 /// <summary>
95 /// The data is a handle to the locale identifier associated with text in the clipboard. When you close the
96 /// clipboard, if it contains <c>CF_TEXT</c> data but no <c>CF_LOCALE</c> data, the system automatically sets
97 /// the <c>CF_LOCALE</c> format to the current input language. You can use the <c>CF_LOCALE</c> format to
98 /// associate a different locale with the clipboard text.
99 /// An application that pastes text from the clipboard can retrieve this format to determine which character
100 /// set was used to generate the text.
101 /// Note that the clipboard does not support plain text in multiple character sets. To achieve this, use a
102 /// formatted text data type such as RTF instead.
103 /// The system uses the code page associated with <c>CF_LOCALE</c> to implicitly convert from
104 /// <see cref="CF_TEXT"/> to <see cref="CF_UNICODETEXT"/>. Therefore, the correct code page table is used for
105 /// the conversion.
106 /// </summary>
107 public const uint CF_LOCALE = 16;
108
109 /// <summary>
110 /// A memory object containing a <c>BITMAPV5HEADER</c> structure followed by the bitmap color space
111 /// information and the bitmap bits.
112 /// </summary>
113 public const uint CF_DIBV5 = 17;
114
115 /// <summary>
116 /// Owner-display format. The clipboard owner must display and update the clipboard viewer window, and receive
117 /// the <see cref="ClipboardMessages.WM_ASKCBFORMATNAME"/>, <see cref="ClipboardMessages.WM_HSCROLLCLIPBOARD"/>,
118 /// <see cref="ClipboardMessages.WM_PAINTCLIPBOARD"/>, <see cref="ClipboardMessages.WM_SIZECLIPBOARD"/>, and
119 /// <see cref="ClipboardMessages.WM_VSCROLLCLIPBOARD"/> messages. The <c>hMem</c> parameter must be <c>null</c>.
120 /// </summary>
121 public const uint CF_OWNERDISPLAY = 0x0080;
122
123 /// <summary>
124 /// Text display format associated with a private format. The <c>hMem</c> parameter must be a handle to data
125 /// that can be displayed in text format in lieu of the privately formatted data.
126 /// </summary>
127 public const uint CF_DSPTEXT = 0x0081;
128
129 /// <summary>
130 /// Bitmap display format associated with a private format. The <c>hMem</c> parameter must be a handle to
131 /// data that can be displayed in bitmap format in lieu of the privately formatted data.
132 /// </summary>
133 public const uint CF_DSPBITMAP = 0x0082;
134
135 /// <summary>
136 /// Metafile-picture display format associated with a private format. The <c>hMem</c> parameter must be a
137 /// handle to data that can be displayed in metafile-picture format in lieu of the privately formatted data.
138 /// </summary>
139 public const uint CF_DSPMETAFILEPICT = 0x0083;
140
141 /// <summary>
142 /// Enhanced metafile display format associated with a private format. The <c>hMem</c> parameter must be a
143 /// handle to data that can be displayed in enhanced metafile format in lieu of the privately formatted data.
144 /// </summary>
145 public const uint CF_DSPENHMETAFILE = 0x008E;
146
147 /// <summary>
148 /// Start of a range of integer values for application-defined GDI object clipboard formats. The end of the
149 /// range is <see cref="CF_GDIOBJLAST"/>. Handles associated with clipboard formats in this range are not
150 /// automatically deleted using the <c>GlobalFree</c> function when the clipboard is emptied. Also, when using
151 /// values in this range, the <c>hMem</c> parameter is not a handle to a GDI object, but is a handle allocated
152 /// by the <c>GlobalAlloc</c> function with the <c>GMEM_MOVEABLE</c> flag.
153 /// </summary>
154 public const uint CF_GDIOBJFIRST = 0x0300;
155
156 /// <summary>
157 /// See <see cref="CF_GDIOBJFIRST"/>.
158 /// </summary>
159 public const uint CF_GDIOBJLAST = 0x03FF;
160
161 /// <summary>
162 /// Start of a range of integer values for private clipboard formats. The range ends with
163 /// <see cref="CF_PRIVATELAST"/>. Handles associated with private clipboard formats are not freed
164 /// automatically; the clipboard owner must free such handles, typically in response to the
165 /// <see cref="ClipboardMessages.WM_DESTROYCLIPBOARD"/> message.
166 /// </summary>
167 public const uint CF_PRIVATEFIRST = 0x0200;
168
169 /// <summary>
170 /// See <see cref="CF_PRIVATEFIRST"/>.
171 /// </summary>
172 public const uint CF_PRIVATELAST = 0x02FF;
173 }