题
使用此事件标签就会消失,我该怎么做?
private void label4_MouseMove(object sender, MouseEventArgs e)
{
label4.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}
解决方案
label4
的位置是相对于容器( Form
或父控件),光标位置可能是相对于屏幕的。
您需要调整位置。例如,如果容器是 Form
,您可以在屏幕中找到它的位置,并通过它计算光标相对于屏幕的位置。
这只是原因的一种可能性,但这种情况经常发生:)
其他提示
handle these three event ...
Control actcontrol;
Point preloc;
void label1_Mousedown(object sender, MouseEventArgs e)
{
actcontrol = sender as Control;
preloc = e.Location;
Cursor = Cursors.Default;
}
void label1_MouseMove(object sender, MouseEventArgs e)
{
if (actcontrol == null || actcontrol != sender)
return;
var location = actcontrol.Location;
location.Offset(e.Location.X - preloc.X, e.Location.Y - preloc.Y);
actcontrol.Location = location;
}
void label1_MouseUp(object sender, MouseEventArgs e)
{
actcontrol = null;
Cursor = Cursors.Default;
}
使用表单的PointToClient()函数将鼠标X / Y坐标转换为相对于表单的点,应该这样做。
编辑:改为使用鼠标事件args对象属性:
Label1.Location = New Point(e.X, e.Y)
PS原谅VB,在这台PC上没有C# 不隶属于 StackOverflow