views:

129

answers:

4

Is it possible to disable the hittest on a Windows Forms window, and if so, how do I do it? I want to have a opaque window that cannot be clicked.

Thanks in advance, Christoph

A: 

Do you want a window that cannot be moved? Set FormBorderStyle to none.

Robert Harvey
Yes, it should not be possible to move the window and a have already set FormBorderStyle to none. But additionally, I want mouse clicks to be propagated to the window below, i. e. I want the window not to consume the events.
Christoph
A: 

Well, I still don't know much about your use case, but I'll take a stab anyway, and provide a simple example.

I assume that you want to control something on the main form from your floating form. To do this, you need a reference to your main form from your floating form. You do this by creating a constructor overload in your floating form that accepts an instance of your main form, like this:

    public FloatingForm(MainForm mainForm)
    {
        InitializeComponent();
        _mainForm = mainForm;
    }

The floating form contains a textbox named floatingFormTextBox, and a button named Button1. The partial class for the floating form looks like this:

public partial class FloatingForm : Form
{
    MainForm _mainForm;

    public FloatingForm()
    {
        InitializeComponent();
    }

    public FloatingForm(MainForm mainForm)
    {
        InitializeComponent();
        _mainForm = mainForm;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _mainForm.DoSomething(floatingFormTextBox.Text);
    }
}

The main form just contains a textbox named mainFormTextBox. When the main form loads, it creates an instance of the floating form, passing a reference to itself to the floating form's new constructor overload. The partial class for the main form looks like this:

public partial class MainForm : Form
{
    FloatingForm _floatingForm;

    public MainForm()
    {
        InitializeComponent();
    }

    public void DoSomething(string text)
    {
        mainFormTextBox.Text = text;
        this.Refresh();
    }

    private void MainForm_Load(object sender, EventArgs e)
    {
        _floatingForm = new FloatingForm(this);
        _floatingForm.Show();
    }
}

Now, when I put some text into the textbox of the floating form and click the button, the text shows up in the textbox of the main form.

Robert Harvey
Thanks for the response. Let me add some details about the use case: It is crucial for me that the two forms live in two separate processes - it is not sufficient to have simply two forms and it is not sufficient either to have two message pumps (two times Application.Run() in different threads. The form in the background should handle all events, the floating form in front (in a separate process) is only an overlay, which should not handle any mouse event - it must let it through to the background form.
Christoph
What is the purpose of the overlay? Isolation?
Robert Harvey
I need to have to two Virtual Earth controls which cannot live in the same process if in 3D mode.
Christoph
A: 

If you're talking to a different process, you need to send and retrieve Windows messages.

http://www.c-sharpcorner.com/UploadFile/thmok/SendingWindowsMessageinCSharp11262005042819AM/SendingWindowsMessageinCSharp.aspx

Have a look at this link:

Using Window Messages to Implement Global System Hooks in C# http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx

Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult (impossible, according to MSDN) to implement in C#. This article attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue.

Robert Harvey
Since both windows are under my control, passing the events from the front message loop to the background window explicitly, seems the best solution for me.
Christoph
A: 

found this with Google

Joel