tags:

views:

204

answers:

3

I've tried several things, but none of them work...

I have Form that should come in front of all Windows on clicking NotifyIcon. So here is what I tried:

private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        this.TopMost = true;
        this.BringToFront();
        this.Focus();
        this.TopMost = false;
    }
}

Then I tried to use SetForegroundWindow:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);

by adding

        SetForegroundWindow(this.Handle);

at the end of the if block.

Finally, I saw that when this doesn't work if I click right mouse button on NotifyIcon and context menu gets opened, I can then left click NotifyIcon and it brings it to the front.

I've tried to add this code at the beginning:

        cmsNotifyIcon.Show();
        cmsNotifyIcon.Close();

So that it shows and closes notifyIcon context menu, as a possible idea for workaround, but it doesn't help.

Any ideas on how to do this, or work around this?

+1  A: 

Here's how I've done it. Note that StartupWindowState and HideWhenMinimized are a private members of my form.

private void OnOpenTrayMenuItemClicked(object sender, EventArgs e) {
    if (this.WindowState == FormWindowState.Minimized) {
        this.WindowState = this.StartupWindowState;
        this.ShowInTaskbar =
            (this.HideWhenMinimized && (this.WindowState == FormWindowState.Minimized)) ? false : true;
        this.Show();
    }

    this.Activate();
}
Matt Davis
As I answered below, I don't think this resolves my problem - this is a code to bring form in front of all forms in your application, but not in front of all windows of all applications if I am right?
Ivan
Perhaps I'm missing something in your requirements. It won't bring the window to the front of any topmost windows that are open (because I avoid using `TopMost` in my applications). However, when I click on the notify icon, this *seems* to make my application the active one, and the code above brings the window to the front of all non-topmost windows, even ones that are not part of my application, without fail.
Matt Davis
I was wrong, I explained that to another answer similar to yours below, anzway you are right that this should work, but the reason why I had a problem was that I was using MouseDown event and not MouseUp or MouseClick as you did.
Ivan
A: 

what if you do it on MouseUp ?

Catalin DICU
Good idea, Activate() can't fix it.
Hans Passant
I can try, but I don't have high hopes in this, because MouseDown event is triggered, I can see that if I insert break point in code.Anyway, as this doesn't happen constantly, I need some time to test.
Ivan
I think that I had enough observations to claim that this should be solution to my problem. If somehow problem reappears, I'll report it.
Ivan
A: 

Use Activate() instead of Show(). Also if your form is minimized you have to set it's WindowState to WindowState.Normal (or whatever state it was before minimizing).


        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            Activate();

            // this is needed for minimized form to show
            WindowState = FormWindowState.Normal;
        }

Petr Havlicek
I've tried to minimize all windows with start+d, click notifyicon and it seems to work (with or without bringing another window to front first), so I guess it is not the problemThis code you provided should work if you don't have any other application running than yours, but I don't think that it can bring in front of all windows of all applications.
Ivan
It will bring your application's window in front of al windows of all applicatiion. I've tested it (Windows Vista).
Petr Havlicek
It seems that MouseUp instead of MouseDown solved my problem, but I guess that yours code is simpler, so I should also use it. I am not sure whether it would work on MouseDown, I'll try to test it.
Ivan
I suggest using MouseClick event.
Petr Havlicek
Hi Ivan, please note that my code is not handling the case when your window was maximized before minimizing. You should set WindowState to Maximized insted of Normal then. If you find my answer usefull please mark it as useful or accepted answer, thanks.
Petr Havlicek