1

I have a simple app which is small and is supposed to float on top of all my other windows. After a few hours, I'll notice it is no longer on top of all my other windows and I was wondering if anyone knew why this happens.

During this time I have opened new applications (mostly MS 2010 products), locked/unlocked my PC a few times, hit the Desktop shortcut, and moved the app around on the screen (sometimes between screens since I have two monitors).

I have not been able to duplicate the behavior on demand, however it happens at least once a day. When it stops staying on top, it hides itself when I hit the desktop shortcut, so I think somehow the TopMost property is getting set to false.

<Window AllowsTransparency="True" 
        WindowStyle="None"
        Topmost="True"
        SizeToContent="WidthAndHeight"
        MouseDown="Window_MouseDown"
        ShowInTaskbar="False"
        Background="Transparent" 
        SnapsToDevicePixels="True">

The app is really simple... it just works like a virtual chess clock and lets me track time spent handling help desk calls vs actual development. The MouseDown event simply triggers the drag/drop behavior of the application since WindowStyle is set to None.

I am running WindowsXP and the app was built in .Net 4.0.

Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Any luck getting this to work? I've been having the same trouble, and have not found a solid solution yet. – Joseph Gabriel Nov 30 '11 at 22:23
  • @joeg Sadly, no. My window still disappears intermittently – Rachel Dec 01 '11 at 04:19
  • In my app, I'm reproducing the loss of Topmost consistently. First, I have a WindowA that is Topmost. In another window WindowB, I do the following: (1) set WindowB.Topmost = true, then (2) call MessageBox.Show, then (3) set WindowB.Topmost = false again. After this, WindowA will have lost top-most status. It's rather frustrating. – Grant Birchmeier Apr 10 '12 at 01:23

3 Answers3

3

Perhaps you could capture the Deactivated event, and force the Window back ontop again?

Just a guess really, but it'd be worth investigating if setting Topmost = False then Topmost = True at the right time fixes it.

Tom
  • 3,354
  • 1
  • 22
  • 25
  • Thanks, I'm giving that a shot but won't know for sure if it works until it stops staying on top of other windows again. For now I added a MessageBox alert and reset Topmost=True when window gets deactivated and Topmost=False – Rachel Feb 21 '11 at 16:00
  • Still have not been able to reproduce the problem... no idea why since it was happing daily before I posted this. Your idea is a good one though and I'm accepting it as the answer – Rachel Feb 24 '11 at 15:00
  • This doesn't work unfortunately... I am watching the `Deactivated` event and throwing a popup if `TopMost = false` and it never triggers. – Rachel Mar 22 '11 at 12:59
1

Alternative solution

in your WPF MainWindow

namespace YourNameSpace
{  
     public partial class MainWindow : Window
     {
    public MainWindow()
    {
        InitializeComponent();
    }

    private delegate void MessageBoxDelegate(DelegatePara para);

    public void ShowAwMessageBox(DelegatePara para)
    {            
        this.Dispatcher.BeginInvoke(new MessageBoxDelegate(ShowMessageBox), para);
    }

    private void ShowMessageBox(DelegatePara para)
    {
        this.Topmost = true;
        int typ = para.count;
        string msg = para.Msg;
        switch (typ)
        {
            case 0:
                MessageBox.Show(this, msg, "Auswertung", MessageBoxButton.OK, MessageBoxImage.Information);
                break;
            case 1:
                MessageBox.Show(this, msg, "Auswertung", MessageBoxButton.OK, MessageBoxImage.Error);
                break;
            case 2:
                MessageBox.Show(this, msg, "Auswertung", MessageBoxButton.OK, MessageBoxImage.Question);
                break;
            default:
                MessageBox.Show(this, msg, "Auswertung", MessageBoxButton.OK, MessageBoxImage.Information);
                break;
        }
    }
}

 public class DelegatePara
 {
    public int count {get;set; }
    public string Msg {get;set; }
 }
}

Then you call it with MainWind handle from anywhere you want, even in a thread

this.MyWind.ShowRlvAwMessageBox(new DelegatePara() { count = 0, Msg = "Hallo World!" });
Jack
  • 10,943
  • 13
  • 50
  • 65
jansen208
  • 11
  • 1
0

This question is pretty old but I haven't found any real solutions yet so here's my idea for anyone else that runs across the issue:

Try Tom's advice, but instead of just checking to see if TopMost becomes use ShowWindowAsync to force it to show again.

Only thing that might be an issue about that that is that it can move the focus away from the active application, but I know there's a way to make it a "focus-less" form if that's an option for you.

About to try this for myself, so I will update this answer if it works with details.

BVernon
  • 3,205
  • 5
  • 28
  • 64
  • "About to try this for myself, so I will update this answer if it works with details." - so it didn't work? – dontbyteme Dec 09 '16 at 10:40
  • @dontbyteme Oh geez... sorry I forgot to come back and update this. I can't say I remember for sure now but I don't think this ended up being my final solution. I think I had to incorporate the answer on this one: http://stackoverflow.com/questions/156046/show-a-form-without-stealing-focus – BVernon Dec 12 '16 at 21:36
  • @dontbyteme Although now that I'm looking again maybe you're not after making it a focus-less window so that answer might not help. If all else fails, just set a timer to put it back on top. Of course, you want to make sure when you do that it doesn't steal the focus of whatever you're working on (or quickly returns it) so that might be another challenge in itself. Hope something I said helps, and good luck! – BVernon Dec 12 '16 at 21:40