Show a winform over the notification area
Asked Answered
S

3

6

I want to show a winform in the very right down corner just above the system tray,

How do I do that? Here is my code:

public static void Notify()
{        
    Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
    Form fm = new Form();
    fm.ClientSize = new Size(200, 200);
    int left = workingArea.Width - fm.Width;
    int top = workingArea.Height - fm.Height;
    fm.Location = new Point(left, top);
    fm.ShowInTaskbar = false;
    fm.ShowIcon = false;
    fm.MinimizeBox = false;
    fm.MaximizeBox = false;
    fm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
    fm.Text = "Test";
    fm.TopMost = true;
    fm.Show();
}
Silvertongued answered 4/9, 2010 at 23:7 Comment(0)
L
8

I just tried this and it worked for me (note: this code must appear after the form has been displayed for the first time -- for example, you can put it in the form's Load event handler, or simply include it after any call to Show):

Rectangle workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
int left = workingArea.Width - this.Width;
int top = workingArea.Height - this.Height;

this.Location = new Point(left, top);

Whether to use WorkingArea or Bounds depends on what you mean by "over": if you mean "in front of," then use Bounds, as it includes the area covering the entire screen (including that space occupied by the system tray); if you mean "above," then use WorkingArea, which just includes the user's desktop.

Also let me just clarify that you want your actual form displayed down there, right? If you wanted an icon in the notification area, that's what the NotifyIcon component is for.

Lengthen answered 4/9, 2010 at 23:13 Comment(2)
@Data-Base: Move the lines that set the form's location to after you call Show. Windows Forms does a strange thing where it seems to ignore you when you set the Location property before the form's been shown for the first time.Lengthen
I just used fm.StartPosition = FormStartPosition.Manual; code from Hans Passant and it worked :-)Silvertongued
R
6

You forgot this one:

        fm.StartPosition = FormStartPosition.Manual;

What you'll have to work on next is putting the taskbar at, say, the left side of the screen and running the code on a machine that has the video DPI setting at a different value (like 125). You can only position the form accurately in its Load event. Don't set the client size.

Robertroberta answered 5/9, 2010 at 0:1 Comment(0)
A
5

If you wanted to position the form over/infront of the taskbar:

Set the forms TopMost property to true. You can use Screen.PrimaryScreen.Bounds to get the screen resolution then set your forms position appropriately.


If you just want to position the form just above the taskbar in the bottom right then you can do as follows:

In the form designer, goto Properties->Events and add the Load event to your form.

Add the following:

private void Form1_Load(object sender, EventArgs e)
{
    this.StartPosition = FormStartPosition.Manual;
    int x = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
    int y = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
    this.Bounds = new Rectangle(x, y, this.Width, this.Height);
}
Alary answered 4/9, 2010 at 23:9 Comment(2)
I think the OP meant "over" as in "above," rather than "on top of"; so TopMost would not be appropriate. I could be wrong, though.Lengthen
True that.. well now he has options :)Alary

© 2022 - 2024 — McMap. All rights reserved.