HerrimanCoder
HerrimanCoder

Reputation: 7226

.NET winforms app won't come to front on Windows 8.1

My customer has two Windows 8.1 PCs sitting side-by-side in their business. My .NET 4.5 winforms app runs on both systems. If the app goes to the background when the user opens some other program, or clicks some other open program, that is fine. But when a barcode scan occurs, my winforms app is supposed to come to the front and be in the foreground in front of all other open programs.

It works perfectly on Windows 7, but doesn't jump to the front on Windows 8.1. Here is the relevant code:

private void BringToTheFront() {
    System.Threading.Thread.Sleep(400);

    if (this.WindowState == FormWindowState.Minimized) {
        this.WindowState = FormWindowState.Normal;
    }

    //this.TopMost = true;
    this.Activate();
    System.Threading.Thread.Sleep(100);
    BringToFront();
}

private static class User32 {
    [DllImport("User32.dll")]
    internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    internal static readonly IntPtr InvalidHandleValue = IntPtr.Zero;
}

public void Activate() {
    Process currentProcess = Process.GetCurrentProcess();
    IntPtr hWnd = currentProcess.MainWindowHandle;
    if (hWnd != User32.InvalidHandleValue) {
        User32.SetForegroundWindow(hWnd);
        User32.ShowWindow(hWnd, 1);
    }
}

A few notes on this code: BringToFront() is a built-in .net function available with winforms, so I call that inside my custom BringToTheFront() function.

The last function shown, Activate(), overrides/hides the built-in .net Activate() function in winforms. You can see the DllImport stuff.

Originally, on my Win7 box, I didn't need to resort to p/invoking win32 calls to get this to work. I simply called this.Activate and it worked fine.

On Win8.1, if my app is merely minimized, when the barcode scan occurs, it pops up and receives focus. But if it's BEHIND anything else (not minimized), its icon flashes at the bottom when a scan occurs, but it doesn't come to the front.

Notice the commented-out this.TopMost = true. When I uncomment that code, the app stays in the front always and will not allow anything to go in front of it. This approach is disruptive and the customers don't like it.

Notice my thread sleeps. Those are just experiments in hopes that a slight delay would help. It doesn't seem to.

Is there anything I can do to make this work?

Moderator: This is not a duplicate of Bring .NET winform to front (focus) on Windows 8 -- I already tried everything posted there and it doesn't work on Win8. So this clarifies the issue and illustrates what I have tried.

Upvotes: 2

Views: 454

Answers (1)

David
David

Reputation: 10718

Have you tried setting the TopMost on and off again? According to the Reference Source, that property is doing a little more than simply setting an underlying value.

Upvotes: 2

Related Questions