public partial class FrmMain : Form
{
private const int WM_DRAWCLIPBOARD = 0x308;
private const int WM_CHANGECBCHAIN = 0x30D;
private IntPtr nextClipHwnd;
[DllImport("user32")]
private static extern IntPtr SetClipboardViewer(IntPtr hwnd);
[DllImport("user32")]
private static extern IntPtr ChangeClipboardChain(IntPtr hwnd, IntPtr hWndNext);
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
nextClipHwnd = SetClipboardViewer(this.Handle);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
//将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口
SendMessage(nextClipHwnd, m.Msg, m.WParam, m.LParam);
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Text) | data.GetDataPresent(DataFormats.OemText))
{
this.richTextBox1.Text = (String)data.GetData(DataFormats.Text);
}
break;
default:
base.WndProc(ref m);
break;
}
}
private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
{
//从观察链中删除本观察窗口
ChangeClipboardChain(this.Handle, nextClipHwnd);
//将WM_DRAWCLIPBOARD消息传递到下一个观察链中的窗口
SendMessage(nextClipHwnd, WM_CHANGECBCHAIN, this.Handle, nextClipHwnd);
}
}
C# 监视粘贴板
最新推荐文章于 2023-03-20 16:50:03 发布