我认为我的剪贴板错了,但我想知道或得到一个提示,我将需要实现这一点。我希望这个程序在后台运行,并且每次我都复制它在程序中结束的时候。那样,如果我想回去看看我10分钟前复制的内容,我会在程序中找到它。所以我想我需要将它保存在一个文本文件中。我如何实现这个?按ctrl + c并将其保存到texbox
回答
C#在剪贴板更改时通常无法引发事件。您可以从剪贴板读取数据,并且可以忙于等待轮询剪贴板,但这些对我来说似乎并非最佳。
但是,有一点extern
用法,你应该能够得到你想要的。在一类的子类Form
:
/// <summary>
/// Message id for data being copied to the clipboard
/// </summary>
/// <value>776</value>
private const int WM_DRAWCLIPBOARD = 0x0308;
/// <summary>
/// Message id for a window being removed from the viewer chain
/// </summary>
/// <value>781</value>
private const int WM_CHANGECBCHAIN = 0x030D;
/// <summary>
/// Message id for the window being destroyed
/// </summary>
/// <value>2</value>
private const int WM_DESTROY = 0x0002;
/// <summary>
/// The next window in the clipboard viewer chain
/// </summary>
private IntPtr nextClipboardViewer;
/// <summary>
/// Adds the specified window to the chain of clipboard viewers. Clipboard viewer windows receive a <c>WM_DRAWCLIPBOARD</c>
/// message whenever the content of the clipboard changes.
/// </summary>
/// <param name="hWnd">A handle to the window to be added to the clipboard chain.</param>
/// <returns>If the function succeeds, the return value identifies the next window in the clipboard viewer chain. If an
/// error occurs or there are no other windows in the clipboard viewer chain, the return value is <c>null</c>.</returns>
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetClipboardViewer(IntPtr hWnd);
/// <summary>
/// Removes a specified window from the chain of clipboard viewers.
/// </summary>
/// <param name="hWndRemove">A handle to the window to be removed from the chain. The handle must have been passed to the
/// <see cref="SetClipboardViewer"/> function.</param>
/// <param name="hWndNewNext">A handle to the window that follows the <paramref name="hWndRemove"/> window in the clipboard
/// viewer chain. (This is the handle returned by <see cref="SetClipboardViewer"/>, unless the sequence was changed in response
/// to a <c>WM_CHANGECBCHAIN</c> message.)</param>
/// <returns>The return value indicates the result of passing the <c>WM_CHANGECBCHAIN</c> message to the windows in the
/// clipboard viewer chain. Because a window in the chain typically returns <c>false</c> when it processes <c>WM_CHANGECBCHAIN</c>,
/// the return value from <see cref="ChangeClipboardChain"/> is typically <c>false</c>. If there is only one window in the chain,
/// the return value is typically <c>true</c>.</returns>
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
/// <summary>
/// Sends the specified message to a window or windows. The <c>SendMessage</c> function calls the window
/// procedure for the specified window and does not return until the window procedure has processed the message.
/// </summary>
/// <param name="hwnd">A handle to the window whose window procedure will receive the message.</param>
/// <param name="wMsg">The message to be sent.</param>
/// <param name="wParam">Additional message-specific information.</param>
/// <param name="lParam">Additional message-specific information.</param>
/// <returns>The return value specifies the result of the message processing; it depends on the message sent.</returns>
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
/// <inheritdoc/>
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_DRAWCLIPBOARD)
{
// The user copied something to the clipboard
IDataObject clipData = Clipboard.GetDataObject();
if (clipData.GetDataPresent(DataFormats.Text))
{
// Copied data is text
}
SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
}
// Handle necessary native clipboard stuff
else if (m.Msg == WM_DESTROY)
{
// Remove MyForm from the clipboard chain
ChangeClipboardChain(this.Handle, nextClipboardViewer);
}
else if (m.Msg == WM_CHANGECBCHAIN)
{
if (m.WParam == nextClipboardViewer)
{
nextClipboardViewer = m.LParam;
}
else
{
SendMessage(nextClipboardViewer, m.Msg, m.WParam, m.LParam);
}
}
else
{
base.WndProc(ref m);
}
}
private void MyForm_Load(object sender, EventArgs e)
{
// Include MyForm in the clipboard chain
nextClipboardViewer = SetClipboardViewer(this.Handle);
}
确保MyForm_Load
添加为(从设计器窗口最容易)的Form
相应事件。
只是一个小纸条。您可以通过在WndProc()中捕获WM_DESTROY来消除订阅FormClosing()事件的需要,并从那里的链中删除自己。 –
很高兴知道!我承认,我从我写的现有应用程序中提取这些代码,删除无关位。在我的应用程序中,我正在'_Load'和'_FormClosing' _anyway_中执行其他操作。 WM_DESTROY的价值是什么? –
'私人const int WM_DESTROY = 0x2;' –
- 1. contentEditable,CTRL-B CTRL-I并保存
- 2. 收起C#块,并将其保存
- 3. 哈希密码并将其保存并将其保存到mysql pdo utf8编码
- 4. Rails:从URL保存文件并将其保存到Amazon S3
- 5. 保存一个int数并将其检索到其他活动
- 6. 创建SVG并将其保存到数据存储区(GAE + Python)
- 7. C#将数据输入到Texbox
- 8. phpexcel将qrcode插入到excel并将其保存到客户端
- 9. 并保存到数组C++
- 10. 按Ctrl + L将
- 11. 将NSArray保存为NSUserDefaults并将其保存在NSMutableArray中
- 12. 复制$ firebaseObject并将其保存到其他位置
- 13. 复制图像并将其保存到其他目录
- 14. 将照片保存到相机胶卷并确保其实际保存
- 15. 获取单选按钮,并将其保存在一个ArrayList中
- 16. 上传文件,zip并将其保存到内存
- 17. 上传多个文件并将其保存到文件夹中
- 18. C#发现texbox
- 19. 将ID保存到本地存储中,并使用按钮angularjs
- 20. 裁切图像,并将其保存
- 21. 将FILE *保存到C
- 22. 从.txt数据保存和加载到右侧texbox
- 23. 淘汰赛viewModel:将其保存在localStrorage并将其回收
- 24. 保存页面状态并将其恢复到相同状态
- 25. c#如何插入文本框的值,并将其保存到SQL数据库?
- 26. 如何找到百分比,并将其保存到mysql
- 27. 将高分保存到其他分类
- 28. Mongoldb,删除数据并将其保存到集合中问题
- 29. 更改一个javascript变量并将其保存到文件
- 30. 编辑xml文件并将其保存在c#
这个问题是如此混乱:)))删除它自己是最好的方式,第二个最好的方法是尝试改进它。 –
该程序将不得不持续轮询剪贴板中的内容。它也必须为非文本内容做好准备。应该是可以的。 –
签出一些已经这样做的开源程序:http://ditto-cp.sourceforge.net/ –