Transparent, click-through Window

R

Rob Whiteside

Hi,

I am trying to bring a secondary window over top of my main form. The
goal is for it to act as a transparent canvas that I can custom paint
some objects to. This seems like the best method for painting on top
of other controls.

So, there are a few issues I need to overcome. The first is that this
new "window" can't take focus from the main form. I don't want the
user to really know that there's another "window" open.

The second is that the transparent portions of this "window" need to
be click-through. That is, the mouse should be able to interact with
the main form where the top "window" is transparent.

I have attacked it from two angles. I can open another form and set
things like transparencykey to make the click-through works. but this
new form steals the focus from main form when you click on the custom
painted objects.

The other angle is from an example I found online, where you
essentially make a user control show as a window. This allows me to
use WS_EX_NOACTIVATE to prevent the "window" from getting focus, but
now I can't set a transparencykey that let's me click through the
"window" to the main form below.

Each tactic solves one problem, but not the other. Any suggestions?

Put simply, can I bring up a form without it ever taking focus from
the other form, or can i show a Control as a window but allow the
transparent parts to be click through?

Thanks for the help!

--Rob
 
R

Rob Whiteside

Hi,

I am trying to bring a secondary window over top of my main form.  The
goal is for it to act as a transparent canvas that I can custom paint
some objects to.  This seems like the best method for painting on top
of other controls.

So, there are a few issues I need to overcome.  The first is that this
new "window" can't take focus from the main form.  I don't want the
user to really know that there's another "window" open.

The second is that the transparent portions of this "window" need to
be click-through.  That is, the mouse should be able to interact with
the main form where the top "window" is transparent.

I have attacked it from two angles.  I can open another form and set
things like transparencykey to make the click-through works.  but this
new form steals the focus from main form when you click on the custom
painted objects.

The other angle is from an example I found online, where you
essentially make a user control show as a window.  This allows me to
use WS_EX_NOACTIVATE to prevent the "window" from getting focus, but
now I can't set a transparencykey that let's me click through the
"window" to the main form below.

Each tactic solves one problem, but not the other.  Any suggestions?

Put simply, can I bring up a form without it ever taking focus from
the other form, or can i show a Control as a window but allow the
transparent parts to be click through?

Thanks for the help!

--Rob

I found a solution! Below is the appropriate code. I ended up using
a Form without borders and a TransparencyKey set to the backcolor.
Next, I'll need to match up the movement(and resize) of the main
window with this, but that shouldn't pose a problem.

the code:
public partial class TrasparentClickThroughForm : Form
{
private const int WM_MOUSEACTIVATE = 0x0021;
private const int MA_NOACTIVATE = 0x0003;

public TrasparentClickThroughForm()
{
InitializeComponent();
}

/// <summary>
/// Prevents it from being activated when shown
/// </summary>
protected override bool ShowWithoutActivation
{
get { return true; }
}

/// <summary>
/// Prevents it from being activated when the
/// mouse clicks the body of the form
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEACTIVATE)
{
m.Result = (IntPtr)MA_NOACTIVATE;
return;
}
base.WndProc(ref m);
}
}
 

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