K
Of course, an endless cycle can be set up somewhere in a separate flow, but in my view, this is at least incorrect. Windows provides an opportunity to track changes in the exchange buffer - that is, changes will be tracked and processed in real time. I understand that's what you're interested in, not while (true) :♪ http://www.codeguru.com/columns/dotnettips/article.php/c7315/Monitoring-Clipboard-Activity-in-C.htm It says how to set up a tracing at WinForms. I'll give you a brief relay of the section "Start instructions" (with translation and some changes in the code).1We need to call for a few functions from the Win32.SetClipboardViewer♪ ChangeClipboardChainand SendMessage In order to do so in Annex NET, they need to be imported using attribution DllImport♪ In the example below, these functions are imported into the WinForms Annex:using System.Runtime.InteropServices;
...
public partial class frmMain : Form
{
[DllImport("User32.dll")]
protected static extern int
SetClipboardViewer(int hWndNewViewer);
[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern bool
ChangeClipboardChain(IntPtr hWndRemove,
IntPtr hWndNewNext);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg,
IntPtr wParam,
IntPtr lParam);
...
2Determine a class member to accommodate the current window in the buffer notification chain
Original text: Define a class member to hold the current first window in the Clipboard notification chain:public partial class frmMain : Form
{
...
IntPtr nextClipboardViewer;
3Call for a function. SetClipboardViewer♪ This is illustrated in the design form:public frmMain()
{
InitializeComponent();
nextClipboardViewer = (IntPtr)SetClipboardViewer((int)
this.Handle);
}
4Inside class frmMain Redefine the method WndProc♪
In the example, only two communications are processed: WM_DRAWCLIPBOARD and WM_CHANGECBCHAIN♪In the processing code WM_DRAWCLIPBOARDuser method DisplayClipboardData()which removes the contents of the buffer; the same message is then transmitted to the next in the window chain.In the processing code WM_CHANGECBCHAINchecks whether the window removed from the buffer chain is as follows (in relation to the current). If so, nextClipboardViewer a window following the deleted.protected override void WndProc(ref Message m){
const int WM_DRAWCLIPBOARD = 0x308;
const int WM_CHANGECBCHAIN = 0x030D;
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
DisplayClipboardData();
SendMessage(nextClipboardViewer, m.Msg, m.WParam,
m.LParam);
break;
case WM_CHANGECBCHAIN:
if (m.WParam == nextClipboardViewer)
nextClipboardViewer = m.LParam;
else
SendMessage(nextClipboardViewer, m.Msg, m.WParam,
m.LParam);
break;
default:
base.WndProc(ref m);
break;
}
}
5Finally, the window is removed from the buffal chain when .NET Runtime causes the method Dispose Class frmMainprotected override void Dispose( bool disposing )
{
ChangeClipboardChain(this.Handle, nextClipboardViewer);
...
The instructions do not actually describe the method DisplayClipboardData♪ I've got him lookprivate void DisplayClipboardData()
{
txtBox.Text = Clipboard.GetText();
}
I mean, the text field in the form is just a text from the buffer. In your case, we need to set up a mass processing.Just in case, full of leaflets of what happened to me:using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace clipborad
{
public partial class frmMain : Form
{
Random r = new Random();
[DllImport("User32.dll")]
protected static extern int
SetClipboardViewer(int hWndNewViewer);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool
ChangeClipboardChain(IntPtr hWndRemove,
IntPtr hWndNewNext);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg,
IntPtr wParam,
IntPtr lParam);
IntPtr nextClipboardViewer;
public frmMain()
{
InitializeComponent();
nextClipboardViewer = (IntPtr)SetClipboardViewer((int)
this.Handle);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// defined in winuser.h
const int WM_DRAWCLIPBOARD = 0x308;
const int WM_CHANGECBCHAIN = 0x030D;
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
DisplayClipboardData();
SendMessage(nextClipboardViewer, m.Msg, m.WParam,
m.LParam);
break;
case WM_CHANGECBCHAIN:
if (m.WParam == nextClipboardViewer)
nextClipboardViewer = m.LParam;
else
SendMessage(nextClipboardViewer, m.Msg, m.WParam,
m.LParam);
break;
default:
base.WndProc(ref m);
break;
}
}
private void DisplayClipboardData()
{
txtBox.Text = Clipboard.GetText();
}
}
}
Plus don't forget to add a line. ChangeClipboardChain(this.Handle, nextClipboardViewer); Method Dispose in file frmMain.Designer.csThe code is compiled and works perfectly in Win10 x64. The text from the buffer shall be inserted into the text field immediately after copying, no additional button shall be pressed.