首页 > 解决方案 > 中键复制并在复制时删除选择

问题描述

除了 CTRL-C 之外,我希望您能够在我的程序中选择要复制的文本,以及自动复制它。所以举例。如果我选择“这个”,那么它会自动将其复制到中键。到目前为止,这是我的代码。我一直在查看中间点击事件,但我不知道如何调用它。我会喜欢它作为 if 语句

这是我的代码:

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
    //Startwith zoekt naar alle vragen die matchen vanaf het begin van de zin
    //en Endwith alle vragen die matchen vanaf het eind van de zin
    if (q._question.StartsWith(clipboardText) || q._question.EndsWith(clipboardText))
    {
      ShowNotification(q._question, q._answer);
      break;
    }   
  }
}

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

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));
    }
  }
}

标签: c#winformsclipboard

解决方案


我认为解决方案是处理表单的 KeyPress 事件。示例代码可能如下所示:

     private void form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==';')
            {
                //call whatever u want here
            }
        }

    private void form1_MouseClick(object sender, MouseEventArgs e)
    {
         if (e.Button == MouseButtons.Left)
         {
             //do whatever u want
         }
    }

推荐阅读