未触发鼠标事件
我在制作C#WinForms应用程序。由于某种原因,表单的MouseMove和MouseClick事件不会被解雇。 (当我找出原因时,我可能会觉得自己像一个白痴。) 它是一个透明窗体(TransparencyKey设置为背景颜色),在图片框中带有半透明动画gif。我正在做一个屏幕保护程序。 有什么建议吗?未触发鼠标事件
编辑: MainScreensaver.cs
Random randGen = new Random();
public MainScreensaver(Rectangle bounds)
{
InitializeComponent();
this.Bounds = Bounds;
}
private void timer1_Tick(object sender, EventArgs e)
{
Rectangle screen = Screen.PrimaryScreen.Bounds;
Point position = new Point(randGen.Next(0,screen.Width-this.Width)+screen.Left,randGen.Next(0,screen.Height-this.Height)+screen.Top);
this.Location = position;
}
private void MainScreensaver_Load(object sender, EventArgs e)
{
Cursor.Hide();
TopMost = true;
}
private Point mouseLocation;
private void MainScreensaver_MouseMove(object sender, MouseEventArgs e)
{
if (!mouseLocation.IsEmpty)
{
// Terminate if mouse is moved a significant distance
if (Math.Abs(mouseLocation.X - e.X) > 5 ||
Math.Abs(mouseLocation.Y - e.Y) > 5)
Application.Exit();
}
// Update current mouse location
mouseLocation = e.Location;
}
private void MainScreensaver_KeyPress(object sender, KeyPressEventArgs e)
{
Application.Exit();
}
private void MainScreensaver_Deactive(object sender, EventArgs e)
{
Application.Exit();
}
private void MainScreensaver_MouseClick(object sender, MouseEventArgs e)
{
Application.Exit();
}
摘自MainScreensaver.Designer.cs InitialiseComponent()
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseClick);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MainScreensaver_MouseMove);
你确定你的窗体具有焦点?如果表单没有焦点,则鼠标事件不会被触发。
10我的表格肯定有焦点,我有一个Deactivate事件,如果我点击其他任何地方,get的触发成功。 – 2012-02-11 10:55:22
我提出了你的问题,因为我没有提供太多的信息,我只是想提出建议,虽然你没有帮助,但它仍然是一个很好的(如果明显的)想法。 – 2012-02-11 10:56:37
请提供您的代码吗? – anonymous 2012-02-11 10:41:09
转到设计人员并使用“属性”面板删除事件处理程序。然后重新添加事件处理程序。有时候VS2010会在WinForms中玩一些有趣的游戏,并且读取处理程序可以纠正问题。此外,确保隐藏光标不会导致点击事件也被“隐藏”。 – MoonKnight 2012-02-11 10:58:19
@Killercam不好运:( – 2012-02-11 11:18:52