1

I'm trying to create a form that's always on top that is transparent, click through-able, and is purely used to draw information on that never loses focus, so it is always displayed on top. The idea is to be able to draw information on my screen that will constantly be on top of every other window, most likely only simple text, but doesn't prevent me from interacting with the rest of my programs while still being visible.

The trouble I am having is to get the form to always be on top, despite other programs being focuses. I have tried using the TopMost property but that doesn't seem to work, and have played about with having the window re-focus on unfocus but that seemed a bit sloppy and didn't work anyhow. I am on Windows 8.1 should it matter.

Any replies greatly appreciated, thank you.

sergiogarciadev
  • 2,061
  • 1
  • 21
  • 35
user3599771
  • 65
  • 1
  • 6
  • Are you expecting it to be on top of other programs set to always be on top, or only on top of programs that aren't set to always be on top? The former you can't really do, the latter, sure. – Servy May 14 '14 at 19:04
  • Preferably on top of every program, but on top of programs that aren't set to always be on top is likely to work too. – user3599771 May 14 '14 at 19:05
  • And when you say setting "TopMost" doesn't work, in what way does it not work, because that should do exactly what you need? – Servy May 14 '14 at 19:05
  • The overlay that I have falls behind other programs the moment the focus of the window is lost. Perhaps this is a windows 8.1 issue? – user3599771 May 14 '14 at 19:08
  • Related: http://blogs.msdn.com/b/oldnewthing/archive/2011/03/10/10138969.aspx – Servy May 14 '14 at 19:52
  • Are you trying to stay on top of the Windows 8 native applications (Windows RT applications)? – sergiogarciadev May 14 '14 at 20:51

1 Answers1

-1

You must use extended windows styles, this code show how to do it.

This form pass the mouse interaction to the form behind it, and will show as TopMost even with other Top Most windows active.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    const int GWL_EXSTYLE = -20;
    const int WS_EX_TRANSPARENT = 0x20;

    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    extern static int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    extern static int SetWindowLong(IntPtr hWnd, int nIndex, int nStyle);

    private void Form1_Load(object sender, EventArgs e)
    {
        var style = GetWindowLong(this.Handle, GWL_EXSTYLE);
        var newStyle = style | WS_EX_TRANSPARENT;

        SetWindowLong(this.Handle, GWL_EXSTYLE, newStyle);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.BringToFront();
    }
}

It will be a little trick for the user to interact with the form, you must provide more details about what you're trying to do, so we can help.

To show your windows always on top, put a timer with a interval of 100ms and set the form property TopMost to true.

sergiogarciadev
  • 2,061
  • 1
  • 21
  • 35
  • [As Raymond Chen describes](http://blogs.msdn.com/b/oldnewthing/archive/2011/03/10/10138969.aspx), this is neither a reliable way of ensuring a window is always on top, nor is it productive to try to do this to begin with. This is one of those games where the only winning move is not to play. – Servy May 14 '14 at 20:26
  • Just play with this code. The **WS_EX_TRANSPARENT** make the mouse events for the window go to the window behind it (click-throughable as requested). It really works, please create a form and try it for yourself, then if it not work I'll remove the answer. – sergiogarciadev May 14 '14 at 20:30
  • The question states that the OP has *already* been able to make his form transparent. He is having problems making his form a topmost form. That is all that the question is about. – Servy May 14 '14 at 20:32
  • Just to make sure, I edited code in the window behind it and it stayed on top and my code windows didn't lost the focus, I also put task manager with "Always On Top" checked and my window still visible. (I'm using Windows 7 right now) – sergiogarciadev May 14 '14 at 20:34
  • 1
    Now ask yourself what will happen if another window does exactly what you're doing, and if your form will stay on top then. Or what if the other form hooks into your windows focus event and suppresses it, or any number of other options. Trying to fight the "super topmost" battle is a terrible idea, and you should just not do it to begin with. – Servy May 14 '14 at 20:36
  • Yes I know, but I suspect it isn't what the OP needs. He already pointed in the comments and he wants a form that can't be interacted with (click-throughable), as he pointed and it is what this solution is proposing. The top most timer was added later in my answer. – sergiogarciadev May 14 '14 at 20:41
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52707/discussion-between-sergio-garcia-and-servy) – sergiogarciadev May 14 '14 at 20:43