0

I have an background WinForms application, which doesn't show any windows usually. But it shows a form on top most without activation, when occurred some app event. And I have a problem that when users clicks on the form and after this application closes this form and showes another instance of form, then last one shows without top most option (it shows under all opened windows). And I really don't understand why.

I noticed, that problem disappears when I run application with overloaded method:

Application.Run(new TestForm());

But my application is background and so I need to use follow overloaded method:

Application.Run();

And a problem appears again...

I maximally simplified my code to make my problem more clear. Here is code of my form:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace winscr
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
            this.Load += Form_Load;
        }

        void Form_Load(object sender, EventArgs e)
        {
            //-1    HWND_BOTTOM     Places the window at the bottom of the Z order  
            //0x0010    SWP_NOACTIVATE  Does not activate the window.
            //0x0002    SWP_NOMOVE      Retains the current position (ignores X and Y parameters).
            //0x0001    SWP_NOSIZE      Retains the current size (ignores the cx and cy parameters).
            //always returns true
            bool result = SetWindowPos(this.Handle, new IntPtr(-1), 0, 0, 0, 0, 0x0010 | 0x0002 | 0x0001);
            this.Text = result + " " + DateTime.Now.ToString();
        }

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        protected static extern bool SetWindowPos(
             IntPtr hWnd,           // window handle
             IntPtr hWndInsertAfter,    // placement-order handle
             int X,          // horizontal position
             int Y,          // vertical position
             int cx,         // width
             int cy,         // height
             uint uFlags);   // window positioning flags

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

And code of Program class:

using System;
using System.Windows.Forms;

namespace winscr
{
    static partial class Program
    {
        static TestForm frm;

        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            //I use timer only for example on stackoverflow. In my real program form appears after particular events
            Timer tmr = new System.Windows.Forms.Timer();
            tmr.Interval = 2000;
            tmr.Tick += new EventHandler(tmr_Tick);
            tmr.Enabled = true;

            Application.Run();
            //Application.Run(new TestForm()); //if I use this overloading, then all works without any bugs
        }


        static void tmr_Tick(object sender, EventArgs e)
        {
            if (frm != null)
            {
                //close previous instance of form
                frm.Close();
            }
            //construct and show new form
            frm = new TestForm();
            frm.Show();
        }
    }
}

Here is direct link to the project with this code (you can just run project and try to reproduce problem): http://hidescreener.com/downloads/?r=form_top_most_tests.zip

And I recorded the video, in which I show this problem: http://youtu.be/mxJYZE-oMDI

Please, help me to solve this problem. I have trying to solve it for 2 days.

P.S. I found like problem Really annoying bug with TopMost property in Windows Forms but there is no answer...

Updated: Thanks for the Hans Passant's and King King's comments. It was found that this problem only occurs in Windows XP.

Community
  • 1
  • 1
Anatolii Humennyi
  • 1,807
  • 3
  • 26
  • 37
  • without diving into the design you are making,and not fully understanding what you are after,please paste this in your ctor TopMost = true; – terrybozzio Nov 28 '13 at 16:53
  • TopMost is only for your own application. You can't be TopMost of every window, or else, what if I wrote the same program? Who would be on top? See [How do I create a topmost window that is never covered by other topmost windows?](http://blogs.msdn.com/b/oldnewthing/archive/2011/03/10/10138969.aspx) – LarsTech Nov 28 '13 at 16:57
  • If using a concrete MainForm helps than as a workaround simply use a hidden Form for that(Setting Opaque to 0 for it as example). – Ralf Nov 28 '13 at 16:58
  • @terrybozzio calling SetWindowPos with -1 as he does is absolutely the same as what setting the TopMost Property does. Just with the NOACTIVATE Flag. So setting TopMost before that will be overwritten and calling TopMost after that will erase The NOACTIVATE Flag. – Ralf Nov 28 '13 at 17:04
  • No repro at all. Something environmental, I guess. – Hans Passant Nov 28 '13 at 18:34
  • I also can't reproduce it (using your test demo). Remember that if you are using another **Topmost** window (such as a TaskManager), that window can also overlap your Topmost window when receiving focus. Your problem may be caused by the window you used (in the video). – King King Nov 29 '13 at 00:44
  • @terrybozzio see the Ralf's comment. He is right. I need a form without activating, but when I try to use TopMost=true, then my form will be activated. – Anatolii Humennyi Nov 29 '13 at 08:23
  • @Ralf thank you for your answer on terrybozzio's comment. You are right. But hidden Form - is a very bad solution for my program. I would be glad to overcome the root of problem... – Anatolii Humennyi Nov 29 '13 at 08:26
  • @HansPassant yes, you are right... After your answer I tested this test program on Windows 7 x86, Windows 8 x64 and Windows 8.1 x64, and I noticed, that this problem only exists on Windows XP. I updated my question. Thank you. – Anatolii Humennyi Nov 29 '13 at 08:29
  • @KingKing thank you for the comment. I noticed, that problem occurs on Windows XP only. My notepad++ window (which I used in the demo video) is not topmost. – Anatolii Humennyi Nov 29 '13 at 08:34

0 Answers0