0

I have loaded a Winform using following code, with semi-transparent effect.

    frmTest frm=new frmTest();
    frm.Opacity = 0.051;
    frm.Left = 0;
    frm.Top = 0;
    frm.Width= Screen.PrimaryScreen.Bounds.Width;
    frm.Height= Screen.PrimaryScreen.Bounds.Height;
    frm.Show();

Now I'm looking for some way to bypass my form from responding to mouse clicks. For example, the form displays over the desktop with semi-transparent, the user clicks on form, and the desktop should get that mouse click instead of my form. My form only need to be visible to user, but no collision with it.

I tried disabling my form, but having no idea about how to pass the mouse event to desktop or other program below my form. Can someone please point me in right direction to solve this?

Ruwan Liyanage
  • 333
  • 1
  • 13
  • Check this thread: https://stackoverflow.com/a/11043661/2109769 They make Label semi-transparent, same thing may work for form. – Quercus Nov 17 '20 at 10:48
  • @Quercus I tried it this way. It gets the API call but not bypass mouse events. public class TransForm : Form { private const int WM_NCHITTEST = 0x84; private const int HTTRANSPARENT = -1; protected override void WndProc(ref Message message) { if (message.Msg == (int)WM_NCHITTEST) message.Result = (IntPtr)HTTRANSPARENT; else base.WndProc(ref message); } } – Ruwan Liyanage Nov 17 '20 at 11:04
  • You didn't mention what this Form is used for. Should Users interact with it? What does it do? If it doesn't need to do anything specific, it's just and overlay of sort, you can use a Per-pixel Alpha Layered Form - adding `WS_EX_TRANSPARENT` to the styles - and draw an image on its surface. The Form is now *untouchable*, everything goes through even if it looks like it has a surface. If you just need the Form's frame and the ClientArea can be transparent, use a TrasparencyKey color that's red-ish (Magenta is *special*). – Jimi Nov 17 '20 at 11:13
  • @Jimi My form contains some controls updating some information timely, and any of them are not intractable to user. Just for display results, and some small animations. I'm unable to do something like back-buffering since my form will only load it's contents properly on visible screen area. – Ruwan Liyanage Nov 17 '20 at 11:26
  • Both of those methods I've described will give you that. 1) A Layered-Transparent Form cannot be interacted with in any way, but it cannot show Controls on its surface: you have to draw what you want to show on a Bitmap, then drawn the Bitmap on the Form's surface (you may update only part of the Bitmap). 2) Using the BackgroundColor + TransparencyKey set to Magenta, you have a Form with a fully transparent ClientArea which clicks-through. You can have Controls on the Form's surface and these Controls can be interacted with, if required. – Jimi Nov 17 '20 at 11:37
  • I forgot to link the code for the Layered Form. You can find it here: (this implementation derives from sample code provided by Microsoft) [Windows Form Transparent Background Image](https://stackoverflow.com/a/33531201/7444103). As mentioned, add `WS_EX_TRANSPARENT` to `WS_EX_LAYERED` (or test both). – Jimi Nov 17 '20 at 11:57

1 Answers1

0

Maybe something like this :

  1. Capture all Mouse event
  2. Hide your form frm.Visible = false;
  3. Fire the same mouse event as the one captured (see How can I simulate a mouse click at a certain position on the screen? )
  4. Show your form frm.Visible = true;
novaXire
  • 138
  • 1
  • 8
  • Thanks for the nice idea. But I guess it will flicker the screen on every mouse event, for example mouse moving, dragging etc. I'm looking for some way to show the form without any user collision. "My form only need to be visible to user, but no collision with it." – Ruwan Liyanage Nov 17 '20 at 11:15
  • To avoid the flickering, suspend the drawing and resume it after : https://stackoverflow.com/questions/487661/how-do-i-suspend-painting-for-a-control-and-its-children – novaXire Nov 17 '20 at 12:35