user238801
user238801

Reputation:

TopMost not working when tabbing

A few minutes ago I encountered a weird problem with TopMost. I created a topmost form (WinForms) did some tabbing and everything was fine.

Then I tried to open a computer game (fullscren-windowed) which means the game is fullscreen but in a windowed mode which means you can still overlay it with forms. Everything fine until I opened my Browser for example (could be any other program (editor)). Just as a quick side note I have two monitors.

  1. I tabbed out of the computer game (where my form is TopMost) into the browser
  2. I clicked somewhere in the browser (url)
  3. I tabbed back into the game, clicked somewhere in the game (my form is still TopMost)
  4. I do step 1-3 again and suddenly my form (topmost) disappeared.

This happens every time I do this and this problem can be reproduced on at least 2 other machines.

EDIT:

I tried to implement the suggested solution

public partial class Form1 : Form
{
    const UInt32 SWP_NOSIZE = 0x0001;
    const UInt32 SWP_NOMOVE = 0x0002;
    const UInt32 SWP_NOACTIVATE = 0x0010;

    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    public void setTOP()
    {
        SetWindowPos(this.Handle, new IntPtr(-1), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.setTOP();
    }
}

But this didn't work.

Upvotes: 1

Views: 617

Answers (3)

user238801
user238801

Reputation:

It's a horrible solution but it's better than nothing so far.

I implemented a timer which calls topmost every x seconds. Just like I said, it's not a good solution but the only one which came to my mind. I'll continue to work on this but for the meantime this is what i do.

Upvotes: 1

Fernando
Fernando

Reputation: 1259

Your best bet may be to call topmost again whenever your application gets focus.

Upvotes: 1

Nasenbaer
Nasenbaer

Reputation: 4900

The topMost API just set the order of windows. I recommend to just call the "TopMost" flag again. TopMost = False TopMost = True to reset the order. Because another window can use the same command to the OS.

Background working API (just for information)

Declare Function SetWindowPos Lib "user32.dll" ( _
                 ByVal hwnd As integer, _
                 ByVal hWndInsertAfter As integer, _
                 ByVal x As integer, _
                 ByVal y As integer, _
                 ByVal cx As integer, _
                 ByVal cy As integer, _
                 ByVal wFlags As integer) As integer

http://www.activevb.de/cgi-bin/apiwiki/SetWindowPos

Regards

Upvotes: 1

Related Questions