c# – 未触发鼠标事件
发布时间:2020-12-15 21:30:32 所属栏目:百科 来源:网络整理
导读:我正在制作一个C#WinForms应用程序.由于某种原因,表单的MouseMove和MouseClick事件不会被触发. (当我发现原因时,我可能会觉得自己像个白痴.) 它是一个透明的表单(TransparencyKey设置为背景颜色),在图片框中有一个半透明的动画gif.我正在制作一个屏幕保护程
我正在制作一个C#WinForms应用程序.由于某种原因,表单的MouseMove和MouseClick事件不会被触发. (当我发现原因时,我可能会觉得自己像个白痴.)
它是一个透明的表单(TransparencyKey设置为背景颜色),在图片框中有一个半透明的动画gif.我正在制作一个屏幕保护程序. 有什么建议? 编辑: 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); 解决方法
你确定你的表格有焦点吗?如果表单没有焦点,则不会触发鼠标事件.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |