我想在一定时间后隐藏光标(如果鼠标不移动),并且我想在图片框中移动鼠标时显示光标。我就是无法正常工作...这就是我尝试过的:
// this Never seem to hide the cursor
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Show();
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Hide();
tim.Stop();
}
-
// works but in this case I want cursor.ico to be a resource
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Default;
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Current = new Cursor("cursor.ico");
tim.Stop();
}
-
// Properties.Resources.cursor gives an error even though I added it to my resources
// cannot convert from 'System.Drawing.Icon' to 'System.IntPtr'
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Default;
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Current = new Cursor(Properties.Resources.cursor);
tim.Stop();
}
答案 0 :(得分:0)
您需要有一个计时器并处理其Tick
事件。在Tick
事件中,检查鼠标的最后移动是否在特定时间之前,然后使用Cursor.Hide()
隐藏光标。还要处理MouseMove
中的PictureBox
,并使用Cursor.Show()
方法显示光标。
注意:不要忘记启用计时器并将计时器Interval
设置为短值,例如300
并在其中更改duration
的值。以下代码,以缩短/延长闲置时间:
DateTime? lastMovement;
bool hidden = false;
int duration = 2;
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
lastMovement = DateTime.Now;
if (hidden)
{
Cursor.Show();
hidden = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!lastMovement.HasValue)
return;
TimeSpan elaped = DateTime.Now - lastMovement.Value;
if (elaped >= TimeSpan.FromSeconds(duration) && !hidden)
{
Cursor.Hide();
hidden = true;
}
}