Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top