【问题标题】:Keyboard events while dragging拖动时的键盘事件
【发布时间】:2011-12-06 19:07:48
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# winforms drag-and-drop


    【解决方案1】:

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

    编辑:

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

    并添加对 PresentaionCore 和:WindowBase 的引用(您会在:C:\Program Files (x86)\ReferenceAssemblies\Microsoft\Framework\v3.0\ 中找到它。)

    你必须玩这个。

    【讨论】:

    【解决方案2】:

    如果拖动的元素永远不会离开原始形式,请考虑解释鼠标事件而不是使用 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";
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      //尝试这样的事情并在需要的地方进行更改以适应您的工作示例

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

      【讨论】:

      • 这如何回答这个问题?
      • 他想要一个拖放标签的例子
      • 我认为 OP 卡在按键部分。
      • @DJ KRAZE:对于简单的拖放,我可以很容易地参考谷歌。问题是为什么我在拖动时没有收到任何键盘事件。
      猜你喜欢
      • 2013-08-05
      • 2023-04-06
      • 2017-03-25
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多