响应全透明面板上的按键和鼠标点击
我创建了一个在整个屏幕上最大化的表单。
在该表单中,我放置了一个面板,并将其大小设置为填充整个表单,并将背景颜色设置为红色
。表单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该面板不接受文本输入,您可以设置主窗体来处理按键事件。
http://screensnapr.com/v/VovWAi.png
至于获取鼠标位置面板,您可以使用 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.
http://screensnapr.com/v/VovWAi.png
As for getting the mouse location on your panel, you can use the mouseclick event
http://screensnapr.com/v/DB3kCQ.png
I have tested it, and it works on a fullsize transparent panel
我发现我可以使用
GetAsyncKeyState
来响应按键和鼠标单击。然后,例如,我可以调用
GetAsyncKeyState(Keys.LButton)
和GetAsyncKeyState(Keys.Escape)
。比挂钩所有键盘和鼠标事件简单得多。I've discovered that I can use
GetAsyncKeyState
to respond to both keypresses and mouse clicks.Then, I can call
GetAsyncKeyState(Keys.LButton)
andGetAsyncKeyState(Keys.Escape)
, for example. Much simpler than hooking all keyboard and mouse events.