Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms - Trying to make a background maximized form which stays in background and doesn't respond

Tags:

.net

winforms

In short, I want to make a background form which takes up the whole screen and always stays at the back - without setting any of the other application forms which pop up to be set to its child when they are created.

I've got a WinForms app I'm occasionally maintaining which needs a lot of architectural improvements. It's a touchscreen kiosk app. It's going to get some serious attention in the future, but right now, I need to put a band-aid on it.

Problem is that the app was designed with windows coming and going and there is no main windows which the other windows are children of. And everything is multi-threaded because it's responding to a lot of events from different sources. Plus everything is confusingly Castle-Windsor-ized and this makes all changes problematic and defect-multiplying.

So some of the "dialogs" which come up aren't in dialog mode and don't have parents and don't take up the full screen like they should. And I guess it wasn't picked up in test because sometimes prior windows would stick around and sometimes they wouldn't due to some bugs in the event handling. There are a lot of latent bugs and design flaws in the system, so things break when I disrupt it too much.

This means that sometimes the desktop gets visible (like around the edge of a dialog which isn't set to full-screen, when a previously lingering form is properly disposed) and this means that operators could get on the net and surf porn or whatever.

I made a form which works and I set an event handler on most of the relevant events (which just calls SendToBack()) if they happen - like a focus or mouse-click or whatever.

I'm concerned this won't catch everything and the users will see this blank grey form and do nothing (if they clicked on it, it would go to the back).

Is there a guaranteed way to force this form to stay in the background?

like image 253
Cade Roux Avatar asked Nov 23 '25 14:11

Cade Roux


1 Answers

public partial class BackgroundForm : Form
{

    const int WM_MOUSEACTIVATE = 0x21;
    const int MA_NOACTIVATEANDEAT = 0x0004;

    public BackgroundForm()
    {
        InitializeComponent();
        ShowInTaskbar = false;
    }

    protected override void DefWndProc(ref Message m)
    {
        if (m.Msg == WM_MOUSEACTIVATE)
        {
            m.Result = (IntPtr) MA_NOACTIVATEANDEAT;
            return;
        }

        base.DefWndProc(ref m);
    }

    protected override bool ShowWithoutActivation
    {
        get { return true; }
    }

}
like image 73
Skomski Avatar answered Nov 25 '25 02:11

Skomski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!