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

C# 清除表单关闭C上的剪贴板#,c#,winforms,clipboard,formclosing,C#,Winforms,Clipboard,Formclosing,当我关闭程序时,当我粘贴它时,它将执行我的最后一个clipboard.gettext。当我关闭不再执行最后一个clipboard.gettext命令的程序时,我需要 我已经尝试在wndproc后面对代码进行编码,但这对我没有任何帮助: private void Form1_FormClosing(object sender, FormClosingEventArgs e) { ChangeClipboardChain(this.Handle, _clipboardViewerNext);

当我关闭程序时,当我粘贴它时,它将执行我的最后一个clipboard.gettext。当我关闭不再执行最后一个clipboard.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();
        }
    }
}


[DllImport(“User32.dll”,CharSet=CharSet.Auto)]
公共静态外部IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
私有Intptru剪贴板ViewerNext;
//制作一些全局变量,以便我们以后可以在其他地方访问它们
//这将存储所有问题和答案
//下面是问题和答案
List questionList=新列表();
//演示SetText、ContainsText和GetText。
私有void Form1\u加载(对象发送方、事件参数e)
{
//将应用程序设置为剪贴板查看器
_ClipboardViewerNext=设置ClipboardViewer(句柄);
this.FormClosing+=Form1\u FormClosing;
//将问题/答案添加到列表中
//hoofdstuk 3它
问题newQuestion=新问题(“荷兰是什么?”,“阿姆斯特丹*);
}
私有void GetAnswer(字符串剪贴板文本)
{
//循环浏览所有问题和答案
foreach(问题列表中的问题q)
{
//如果我们找到了完全相同的答案,则显示通知
如果(q.(U问题==剪贴簿文本)
{
展示通知(问题、答案);
}
}
}
私有void ShowNotification(字符串问题、字符串答案)
{
notifyIcon1.Icon=SystemIcons.感叹号;
notifyIcon1.balloodtiptitle=问题;
notifyIcon1.balloodtiptext=答案;
notifyIcon1.BALOTIPICON=工具提示错误;
剪贴板。清除();
}
受保护的覆盖无效WndProc(参考消息m)
{
基准WndProc(参考m);
{
常量int WM_DRAWCLIPBOARD=0x308;
如果(m.Msg==WM_)
{
GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
}
}
}
私有作废Form1\u FormClosing(对象发送方,FormClosingEventArgs e)
{
剪贴板。清除();
}
}
}

提前感谢。

您尝试过使用
剪贴板.Clear()
方法吗

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Clipboard.Clear();
}
完整C#代码

资源

    • 您可以使用

       Clipboard.SetDataObject(string.Empty);
      
      完整代码

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

      clipboard.clear();我已经尝试过的方法。然后发生的事情是,他是最后一个剪贴板;方法仍然有效。我已将我的代码更新为完整代码,因为您的代码对我也不起作用。我建议使用较新的、不易中断的(与)方法来管理剪贴板通知。您是否为每个问题创建新帐户?这至少是我在过去两周内第三次看到不同账户对WndProc进行编码;用户#10479535(帐户已关闭),#11238037,还有这个。我想我需要一些东西来清除WndProc中的WM#U DRAWCLIPBOARD
      this.FormClosing += (s, ev) => { Clipboard.SetDataObject(string.Empty); };