清除表格上的剪贴板关闭C#

时间:2019-03-23 10:07:02

标签: c# winforms clipboard formclosing

当我关闭程序时,它将在粘贴时执行我的最后一个剪贴板。当我关闭不再执行最后一个剪贴板.gettext命令的程序时,我想要。

我已经尝试过在wndproc后面编码此代码,但这对我没有任何帮助:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    ChangeClipboardChain(this.Handle, _clipboardViewerNext);        // Removes our from the chain of clipboard viewers when the form closes.
}

这是的代码

       [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
        private IntPtr _ClipboardViewerNext;

        //Make some global variables so we can access them somewhere else later
        //This will store all Questions and Answers
        //In here will be the Questions and Answers
        List<question> questionList = new List<question>();
        // Demonstrates SetText, ContainsText, and GetText.



        private void Form1_Load(object sender, EventArgs e)
        {
            //Set our application as a clipboard viewer
            _ClipboardViewerNext = SetClipboardViewer(Handle);
            this.FormClosing += Form1_FormClosing;

            //Add question/answer to list
            //hoofdstuk 3 it
            question newQuestion = new question("wat is de hoofdstad van nederland?", "Amsterdam.*");

        }

        private void GetAnswer(string clipboardText)
        {
            //Loop through all questions and answers
            foreach (question q in questionList)
            {
                //If we have found an answer that is exactly the same show an Notification
                if (q._question == clipboardText)
                {
                    ShowNotification(q._question, q._answer);
                }
            }
        }

        private void ShowNotification(string question, string answer)
        {
            notifyIcon1.Icon = SystemIcons.Exclamation;
            notifyIcon1.BalloonTipTitle = question;
            notifyIcon1.BalloonTipText = answer;
            notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
            Clipboard.Clear();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            {
                const int WM_DRAWCLIPBOARD = 0x308;
                if (m.Msg == WM_DRAWCLIPBOARD)
                {
                    GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
                }

            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Clipboard.Clear();
        }
    }
}


谢谢。

2 个答案:

答案 0 :(得分:1)

尝试使用Clipboard.Clear()方法。.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Clipboard.Clear();
}

完整的C#代码

using System;
using System.Windows.Forms;

namespace WindowsTestForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Clipboard.SetText("Testing...");
            this.FormClosing += Form1_FormClosing;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Clipboard.SetText(" "); // Value cannot be null
            Clipboard.Clear();
        }
    }
}

资源

答案 1 :(得分:0)

您可以使用

 Clipboard.SetDataObject(string.Empty);

完整代码

this.FormClosing += (s, ev) => { Clipboard.SetDataObject(string.Empty); };