简体   繁体   English

C#事件处理

[英]C# Event-Handling

I am working on a C# piano. 我正在研究C#钢琴。 I have already built the music keyboard and the staff. 我已经建立了音乐键盘和工作人员。 Everytime a user presses a key, it is displayed on the staff in its relevant position. 每次用户按下一个键,该键就会在工作人员的相应位置显示。

The music note displayed on the staff is stored in an array of pictureboxes, as shown below. 谱表上显示的音符存储在一组图片框中,如下所示。

public void addPictureBox(int x, int y, Image image)
{
    picBox[cnt] = new PictureBox();

    picBox[cnt].Image = image;
    picBox[cnt].Location = new Point(x, y);
    picBox[cnt].BackColor = Color.Transparent;

    panel3.Controls.Add(picBox[cnt]);
    picBox[cnt].BringToFront();
    picBox[cnt].MouseDown += new MouseEventHandler(Pic_MouseDown);
    picBox[cnt].MouseUp += new MouseEventHandler(Pic_MouseUp);

    cnt++;
}

The Pic_MouseDown and Pic_MouseUp events allow the user to play the note by clicking on it from the staff. Pic_MouseDown和Pic_MouseUp事件允许用户通过在工作人员上单击便可以播放便笺。

What I want to do now is to create an event on picBox[cnt] for dragging. 我现在想做的是在picBox [cnt]上创建一个事件以进行拖动。 However, picBox[cnt].MouseDown and picBox[cnt].MouseUp have already been registered to Pic_MouseDown and Pic_MouseUp event handlers. 但是,picBox [cnt] .MouseDown和picBox [cnt] .MouseUp已被注册到Pic_MouseDown和Pic_MouseUp事件处理程序中。

How can I do an event to handle dragging since MouseDown and MouseUp have already been registered to other event handlers? 由于MouseDown和MouseUp已经被注册到其他事件处理程序中,我该如何处理该事件呢?

Thanks :) 谢谢 :)

The great thing about event handlers is that you can have as many attached handlers as you want. 关于事件处理程序的伟大之处在于,您可以根据需要拥有任意数量的附加处理程序。 The += ( operator overload ) means you are attaching a new event handler to the existing handlers. +=运算符重载 )意味着您要将新的事件处理程序附加到现有的处理程序上。 You can add as many event handlers as you desire. 您可以根据需要添加任意数量的事件处理程序。

Event Handler Overview 事件处理程序概述

如果创建一个isDragging布尔实例字段,并在鼠标按下时将其设置为true,在鼠标按下时将其设置为false,则可以使用鼠标移动事件来检测是否应移动对象。

You'll need to use a combination of MouseDown, MouseMove and MouseUp events. 您需要结合使用MouseDown,MouseMove和MouseUp事件。 In MouseDown, you do little more than set a flag to indicate that the mouse was pressed, and record where it was pressed. 在的MouseDown,你做的比设置一个标志,表明该鼠标被按下,并记录被压制而已。 In MouseMove, you check to see if the button is still down and the cursor has moved further than SystemInformation.DragSize , which indicates that the user is dragging rather than clicking, and start the drag operation if needed. 在MouseMove中,检查按钮是否仍处于按下状态,并且光标已移至SystemInformation.DragSize ,这表示用户正在拖动而不是单击,并在需要时开始拖动操作。 In MouseUp, you either complete the drag or perform the click action. 在MouseUp中,您可以完成拖动或执行单击动作。

I believe the conventional wisdom for Drag-n-drop is 我相信拖放的传统智慧是

  • Call DoDragDrop in the (source control's) MouseDown handler 在源控件的MouseDown处理程序中调用DoDragDrop
  • set AllowDrop = true in the targe control's AllowDrop property 在目标控件的AllowDrop属性中设置AllowDrop = true
  • There's a whole series of drag events to fine tune behavior 有一系列拖动事件可以微调行为
  • One does not seem to have to worry about any latent MouseUp or MouseClick events. 似乎不必担心任何潜在的MouseUpMouseClick事件。 I'm assuming this is because the physical actions of button press and release are done over different controls and/or the mouse physically moved "far enough". 我假设这是因为按下和释放按钮的物理动作是在不同的控件上完成的,和/或鼠标物理移动了“足够远”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM