简体   繁体   中英

Responding to keypress and mouse click on fully-transparent panel

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM