SHARE
TWEET

Код класса для работы с буфером обмена

HackcatDev Mar 12th, 2019 101 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Diagnostics;
  7.  
  8. namespace SharpClipboard
  9. {
  10.     //https://stackoverflow.com/questions/17762037/error-while-trying-to-copy-string-to-clipboard
  11.     //https://gist.github.com/glombard/7986317
  12.  
  13.     internal static class NativeMethods
  14.     {
  15.         //Reference https://docs.microsoft.com/en-us/windows/desktop/dataxchg/wm-clipboardupdate
  16.         public const int WM_CLIPBOARDUPDATE = 0x031D;
  17.         //Reference https://www.pinvoke.net/default.aspx/Constants.HWND
  18.         public static IntPtr HWND_MESSAGE = new IntPtr(-3);
  19.  
  20.         //Reference https://www.pinvoke.net/default.aspx/user32/AddClipboardFormatListener.html
  21.         [DllImport("user32.dll", SetLastError = true)]
  22.         [return: MarshalAs(UnmanagedType.Bool)]
  23.         public static extern bool AddClipboardFormatListener(IntPtr hwnd);
  24.  
  25.         //Reference https://www.pinvoke.net/default.aspx/user32.setparent
  26.         [DllImport("user32.dll", SetLastError = true)]
  27.         public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
  28.  
  29.         //Reference https://www.pinvoke.net/default.aspx/user32/getwindowtext.html
  30.         [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  31.         public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  32.  
  33.         //Reference https://www.pinvoke.net/default.aspx/user32.getwindowtextlength
  34.         [DllImport("user32.dll")]
  35.         public static extern int GetWindowTextLength(IntPtr hWnd);
  36.  
  37.         //Reference https://www.pinvoke.net/default.aspx/user32.getforegroundwindow
  38.         [DllImport("user32.dll")]
  39.         public static extern IntPtr GetForegroundWindow();
  40.     }
  41.  
  42.     public static class Clipboard
  43.     {
  44.         public static string GetText()
  45.         {
  46.             string ReturnValue = string.Empty;
  47.             Thread STAThread = new Thread(
  48.                 delegate ()
  49.                 {
  50.                     // Use a fully qualified name for Clipboard otherwise it
  51.                     // will end up calling itself.
  52.                     ReturnValue = System.Windows.Forms.Clipboard.GetText();
  53.                 });
  54.             STAThread.SetApartmentState(ApartmentState.STA);
  55.             STAThread.Start();
  56.             STAThread.Join();
  57.  
  58.             return ReturnValue;
  59.         }
  60.     }
  61.  
  62.     public sealed class ClipboardNotification
  63.     {
  64.         public class NotificationForm : Form
  65.         {
  66.             string lastWindow = "";
  67.  
  68.             public NotificationForm()
  69.             {
  70.                 //Turn the child window into a message-only window (refer to Microsoft docs)
  71.                 NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE);
  72.                 //Place window in the system-maintained clipboard format listener list
  73.                 NativeMethods.AddClipboardFormatListener(Handle);
  74.             }
  75.  
  76.             protected override void WndProc(ref Message m)
  77.             {
  78.                 //Listen for operating system messages
  79.                 if (m.Msg == NativeMethods.WM_CLIPBOARDUPDATE)
  80.                 {
  81.  
  82.                     //Write to stdout active window
  83.                     IntPtr active_window = NativeMethods.GetForegroundWindow();
  84.                     int length = NativeMethods.GetWindowTextLength(active_window);
  85.                     StringBuilder sb = new StringBuilder(length + 1);
  86.                     NativeMethods.GetWindowText(active_window, sb, sb.Capacity);
  87.                     Trace.WriteLine("");
  88.                     //Write to stdout clipboard contents
  89.                     Trace.WriteLine("\t[Сtrl-C] Clipboard Copied: " + Clipboard.GetText());
  90.                 }
  91.                 //Called for any unhandled messages
  92.                 base.WndProc(ref m);
  93.             }
  94.         }
  95.  
  96.     }
  97. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top