响应全透明面板上的按键和鼠标点击

发布于 2024-11-27 21:04:47 字数 390 浏览 2 评论 0原文

我创建了一个在整个屏幕上最大化的表单。

在该表单中,我放置了一个面板,并将其大小设置为填充整个表单,并将背景颜色设置为红色。表单的 TransparencyKey 设置为 Red

因此,面板就像一个“锁孔”——您可以看到其正下方的桌面。

当用户点击面板,或者按下键盘上的某个键时,我想采取行动。

但是,因为面板是完全透明的,所以当单击它或按下某个键时,不会发生任何事情。如果我使面板不透明(例如,将其背景颜色设置为蓝色),它确实会对点击做出响应。

让面板响应点击和按键的最佳方法是什么?

我是否必须挂钩整个系统上的所有鼠标和键盘事件,还是有更简单的方法?

I've created a form that's maximized over the entire screen.

Within that form, and sized to fill the entire form, I've placed a panel with background color set to Red. The form's TransparencyKey is set to Red.

Therefore, the Panel is like a "keyhole" - you can see the desktop that's directly underneath it.

When the user clicks on the panel, OR, presses a key on the keyboard, I want to take action.

But, because the panel is completely transparent, when it is clicked or a key is pressed, nothing happens. If I make the panel non-transparent (setting it's background color to Blue, for example), it does respond to clicks.

What's the best way to get the panel to respond to clicks and keypresses?

Do I have to hook all mouse an keyboard events on the entire system or is there a simpler way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满身野味 2024-12-04 21:04:47

该面板不接受文本输入,您可以设置主窗体来处理按键事件。

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.form_KeyPress);

http://screensnapr.com/v/VovWAi.png

至于获取鼠标位置面板,您可以使用 mouseclick 事件

this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseClick);

http://screensnapr.com/v/DB3kCQ.png

我已经测试过它,它可以在全尺寸透明面板上运行

The panel does not take in text input, you can set your main form to handle the keypress event.

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.form_KeyPress);

http://screensnapr.com/v/VovWAi.png

As for getting the mouse location on your panel, you can use the mouseclick event

this.panel1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseClick);

http://screensnapr.com/v/DB3kCQ.png

I have tested it, and it works on a fullsize transparent panel

迎风吟唱 2024-12-04 21:04:47

我发现我可以使用 GetAsyncKeyState 来响应按键和鼠标单击。

[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(System.Windows.Forms.Keys vKey);

然后,例如,我可以调用 GetAsyncKeyState(Keys.LButton)GetAsyncKeyState(Keys.Escape)。比挂钩所有键盘和鼠标事件简单得多。

I've discovered that I can use GetAsyncKeyState to respond to both keypresses and mouse clicks.

[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(System.Windows.Forms.Keys vKey);

Then, I can call GetAsyncKeyState(Keys.LButton) and GetAsyncKeyState(Keys.Escape), for example. Much simpler than hooking all keyboard and mouse events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文