6

我想在系统托盘正上方的右下角显示一个winform,

我怎么做?这是我的代码:

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();
}
4

3 回答 3

8

我刚刚尝试过,它对我有用(注意:此代码必须在第一次显示表单出现- 例如,您可以将它放在表单的Load事件处理程序中,或者在任何调用之后简单地包含它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);

是使用WorkingArea还是Bounds取决于你所说的“over”:如果你的意思是“in front of”,那么使用Bounds,因为它包括覆盖整个屏幕的区域(包括系统托盘占用的空间);如果你的意思是“上面”,那么使用WorkingArea,它只包括用户的桌面。

另外让我澄清一下,您希望在下面显示您的实际表格,对吗?如果您想要通知区域中的图标,这就是该NotifyIcon组件的用途。

于 2010-09-04T23:13:23.300 回答
6

你忘了这个:

        fm.StartPosition = FormStartPosition.Manual;

接下来您需要做的是将任务栏放在屏幕左侧,然后在视频 DPI 设置为不同值(如 125)的机器上运行代码。您只能在其 Load 事件中准确定位表单。不要设置客户端大小。

于 2010-09-05T00:01:15.933 回答
5

如果您想将表单放置在任务栏上方/前面:

将表单 TopMost 属性设置为 true。您可以使用 Screen.PrimaryScreen.Bounds 获取屏幕分辨率,然后适当地设置表单位置。


如果您只想将表单放置在右下角任务栏的正上方,则可以执行以下操作:

在表单设计器中,转到 Properties->Events 并将 Load 事件添加到您的表单中。

添加以下内容:

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);
}
于 2010-09-04T23:09:41.887 回答