如何自动将剪贴板上的文本粘贴到文本框中

如何自动将剪贴板上的文本粘贴到文本框中

问题描述:

你好

我在WPF中做了一个简单的下载器,我想检测链接并自动将其复制并粘贴到文本框中.我已经可以在WindowsForms中使用它,但不能在WPF中使用它:(我在WindowsForms中的代码是这样的,我在某个地方复制并修改了它,然后它起作用了 完美.我如何在WPF中更轻松地实现这一目标?如果我选中了复选框ist,我希望它自动将文本粘贴到文本框中,如我的代码示例中所示.如果有人可以帮助我,那会很好,我对编程很陌生.

I made a simple downloader in WPF and I want to detect the link I copy and paste it automatically in a textbox. I've got it to work in WindowsForms but not in WPF :( my code in WindowsForms was like this, I copied and modified it somewhere and it worked perfectly. How can i achieve this a bit easier in WPF ? I want it automatically paste the text into the textbox if a checkbox ist checked, like in my code example. Would be nice if someone can help me, I'm very new to programming.

提前感谢:D

namespace WindowsFormsApp8
{
    public partial class Form1 : 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);
        IntPtr nextClipboardViewer;

        public Form1()
        {
            

           
            InitializeComponent();
            nextClipboardViewer = (IntPtr)SetClipboardViewer((int)
                         this.Handle);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
        protected override void WndProc(ref System.Windows.Forms.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;
            }
        }
        void DisplayClipboardData()
        {
            
            
      
                    IDataObject iData = new DataObject();
                    iData = Clipboard.GetDataObject();
            if (checkBox1.Checked)
            {
                if (iData.GetDataPresent(DataFormats.Text))
                {
                    txtUrl.Text = (string)iData.GetData(DataFormats.Text);
                }
           
                  
          
              }


以下位置有完整的WPF(不使用winforms):

There is a full WPF (with no winforms use) at :

https://www.nuget.org/packages/WpfClipboardMonitor/

https://www.nuget.org/packages/WpfClipboardMonitor/

试过了,效果很好.  有一个带有包的.chm文件,其中包含示例代码(有效).

Tried it and it works really well.  There is a .chm file with the package which has example code (which works).