我在製作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);
請提供您的代碼嗎? – anonymous 2012-02-11 10:41:09
轉到設計人員並使用「屬性」面板刪除事件處理程序。然後重新添加事件處理程序。有時候VS2010會在WinForms中玩一些有趣的遊戲,並且讀取處理程序可以糾正問題。此外,確保隱藏光標不會導致點擊事件也被「隱藏」。 – MoonKnight 2012-02-11 10:58:19
@Killercam不好運:( – 2012-02-11 11:18:52