拖动时的键盘事件



在一个表单上有两个标签。我希望能够将一个标签拖到另一个标签上,而鼠标左键仍然按下,我希望能够按空格键在"foo"one_answers"bar"之间切换目标标签的文本。

当鼠标左键未释放时,似乎所有输入事件都被抑制。

我错过了什么吗?有样品吗?

查看GiveFeedback事件。也许你可以从那里检查是否有一个键被按下了。

编辑:

void panel1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
    if (Keyboard.IsKeyDown(Key.Space))
    {
        if (label1.Text == "foo") label1.Text = "bar"; else label1.Text = "foo";
    }
}

并添加一个引用到presentationcore和:WindowBase(你会发现:C:Program Files (x86) ReferenceAssembliesMicrosoftFrameworkv3.0 .)

如果被拖动的元素从未离开原始形式,请考虑解释鼠标事件而不是使用D&D机制。它不会那么好,但它可以让您在拖动过程中解释其他消息。

public class MyForm : Form
{
    private Label label;
    public MyForm()
    {
        KeyPress += new KeyPressEventHandler(Form_KeyPress);
        label = new Label();
        label.Text = "foo";
        label.MouseMove += new MouseEventHandler(label_MouseMove);
        Controls.Add(label);
    }
    private void label_MouseMove(object sender, MouseEventArgs e)
    {
        if (MouseButtons == MouseButtons.Left)
        {
            Point loc = label.Location;
            loc.Offset(e.X, e.Y);
            label.Location = loc;
        }
    }
    private void Form_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == ' ')
        {
            if (label.Text == "foo")
                label.Text = "bar";
            else
                label.Text = "foo";
        }
    }
}

//尝试这样做,并在需要的地方更改内容以适合您的工作示例

  public partial class Form1 : Form 
  {
      public Form1()
      {
        InitializeComponent();
        label1.MouseDown += new MouseEventHandler(label1_MouseDown);         
        textBox1.AllowDrop = true;
        textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
        textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);
      }
      void label1_MouseDown(object sender, MouseEventArgs e)
      {
        DoDragDrop(label1.Text, DragDropEffects.Copy);
      }
      void textBox1_DragEnter(object sender, DragEventArgs e)
      {
        if (e.Data.GetDataPresent(DataFormats.Text))
         e.Effect = DragDropEffects.Copy;
      }
      void textBox1_DragDrop(object sender, DragEventArgs e)
      {
        textBox1.Text = (string)e.Data.GetData(DataFormats.Text);
      }
  }

最新更新