Transparent user control with click through to the control below

O

\(O\)enone

I'm trying to create a usercontrol that is transparent, both visibly and to
mouse clicks.

I can make the usercontrol visibly transparent by setting the
SupportsTransparentBackColor style and providing some additional processing
in CreateParams and OnPaintBackground, as detailed here:

http://www.codeproject.com/KB/edit/TransparentTextBox.aspx

This works fine, but if I click the transparent part of my control, the
mouse click is still received by my usercontrol instead of by the control
that is behind it. Is there a way to make clicks fall through the
transparent parts of the control to whatever is underneath?

Thanks,
 
C

cfps.Christian

It sounds like you're attempting to either dynamically add the user
control or make it scalable by not having the user control refer to a
specific button underneath. The only idea I can think of without
seeing the project is to make the usercontrol some kind of container
and when it receives a click event to hit something on the underlying
control.

For instance if you had an extended panel control that only could
contain buttons and when it was clicked it would loop through every
button and fire the Click event.

You could also do what ASP.NET does with most of their controls that
have to link together and feed your usercontrol the ID of the item
that needs to be clicked and dynamically click it that way.

Another option is to have the usercontrol throw an event to the main
form when its been clicked that says its been clicked and on that
event fire the click code of the underlying control.
 
O

\(O\)enone

cfps.Christian wrote:
[...]
Another option is to have the usercontrol throw an event to the main
form when its been clicked that says its been clicked and on that
event fire the click code of the underlying control.

Thanks for the suggestions. I didn't perhaps clarify in my original post
that the control will be moving around my form, and exactly what's
underneath it could therefore change. I did consider having the control fire
an event back to its containing form telling it about clicks that it wasn't
interested in, but then I would have to go to all the effort of working out
what control (if any) was actually under the mouse cursor. As there could be
overlapping controls and all sorts, that could be a pain.

In the end I did find a solution however: setting the user control's Region
property. As the shape I want for my control consists only of a series of
rectangles, this works perfectly. The control only renders and responds to
pixels that fall within the defined region; all other parts of the control
show through the form or controls beneath, and mouse clicks are received by
those controls exactly as expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top