当在控件上按下鼠标按钮时,它不再触发任何事件。 我需要这个,因为我想让自己通过“拖动”它来导航控件。
Drag事件也没有被激活。不知道为什么会这样。这毫无用处。 我需要一个在鼠标移动时被触发的事件。它在哪里?
修改 看,当您在Google-Map上按住鼠标左键时,您可以使用鼠标移动在地图上移动。我想用UserControl做同样的事情。我重写了OnPaint-Method,因此只显示一个网格。我还实现了使用键移动的功能。这一切都有效。现在我想通过按住鼠标左键并移动它来移动鼠标。它应该是容易和明显的,但事实并非如此。
所以我订阅了所有鼠标和拖动事件。像那样:
public partial class GameBoard : UserControl
{
private int m_CellWidth = 5;
private int m_CellHeight = 5;
private Point m_Position = Point.Empty;
private Point m_MousePoint = Point.Empty;
public GameBoard()
{
InitializeComponent();
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Point firstPoint = new Point(m_Position.X % m_CellWidth, m_Position.Y % m_CellHeight);
int countVisibleCols = (int)Math.Ceiling((double)((double)Width / (double)m_CellWidth));
int countVisibleRows = (int)Math.Ceiling((double)((double)Height / (double)m_CellHeight));
Pen artistsPen = new Pen(Brushes.Black);
for (int i = 0; i < countVisibleCols; i++)
{
Point startPoint = new Point(firstPoint.X + i * m_CellWidth, 0);
Point endPoint = new Point(firstPoint.X + i * m_CellWidth, Height);
e.Graphics.DrawLine(artistsPen, startPoint, endPoint);
}
for (int i = 0; i < countVisibleRows; i++)
{
Point startPoint = new Point(0, firstPoint.Y + i * m_CellHeight);
Point endPoint = new Point(Width, firstPoint.Y + i * m_CellHeight);
e.Graphics.DrawLine(artistsPen, startPoint, endPoint);
}
}
private void GameBoard_MouseUp(object sender, MouseEventArgs e)
{
}
private void GameBoard_MouseMove(object sender, MouseEventArgs e)
{
}
private void GameBoard_MouseLeave(object sender, EventArgs e)
{
}
private void GameBoard_MouseHover(object sender, EventArgs e)
{
}
private void GameBoard_MouseEnter(object sender, EventArgs e)
{
}
private void GameBoard_MouseDown(object sender, MouseEventArgs e)
{
}
private void GameBoard_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void GameBoard_MouseClick(object sender, MouseEventArgs e)
{
}
private void GameBoard_DragDrop(object sender, DragEventArgs e)
{
}
private void GameBoard_DragEnter(object sender, DragEventArgs e)
{
}
private void GameBoard_DragOver(object sender, DragEventArgs e)
{
}
private void GameBoard_DragLeave(object sender, EventArgs e)
{
}
}
(实际订阅发生在设计师中。) 问题是:如果点击鼠标左键并且点击了鼠标左键。坚持我的控制,没有一个事件被解雇了。所以我不知道如何实现所需的功能。
答案 0 :(得分:1)
使用MouseMove()
事件。您可以使用e.Button
参数确定在移动过程中左按钮是否已关闭。这是一个带按钮的例子:
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Text = "Left: " + e.X.ToString() + ", " + e.Y.ToString();
}
else if (e.Button == System.Windows.Forms.MouseButtons.None)
{
this.Text = e.X.ToString() + ", " + e.Y.ToString();
}
}
然而,并非所有控件的行为都相同。详细了解您尝试使用哪种控件以及如何使用。
答案 1 :(得分:0)
在表单的Visual Studio设计器中,组件的“属性”窗口中有一个“事件”按钮,通常看起来像闪电。您可以使用它将事件绑定到函数。这会丢失吗?