使用这个事件标签就消失了,我该怎么做?
private void label4_MouseMove(object sender, MouseEventArgs e)
{
label4.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}
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;
}
的位置label4
是相对于容器(Form
或父控件)的,光标位置可能是相对于屏幕的。
您需要调整位置。例如,如果容器是 ,Form
您可以在屏幕中找到它的位置并通过它计算光标相对于屏幕的位置。
这只是原因的一种可能性,但这种情况经常发生:)
使用表单的 PointToClient() 函数将鼠标 X/Y 坐标转换为相对于您的表单的点,应该这样做。
编辑:改为使用鼠标事件 args 对象属性:
Label1.Location = New Point(e.X, e.Y)
PS原谅VB,这台PC上没有C#