在通知区域显示winform

我想在系统托盘正上方的右下角显示一个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(); } 

我刚试过这个并且它对我有用( 注意 :这个代码必须表单第一次显示之后出现 – 例如,你可以把它放在表单的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); 

是否使用WorkingAreaBounds取决于“over”的含义:如果您的意思是“在前面”,则使用Bounds ,因为它包括覆盖整个屏幕的区域(包括系统托盘占用的空间); 如果你的意思是“上面”,那么使用WorkingAreaWorkingArea包括用户的桌面。

另外,让我澄清一下,你希望你的实际表格显示在那里,对吧? 如果您想在通知区域中有一个图标,那就是NotifyIcon组件的用途。

你忘了这个:

  fm.StartPosition = FormStartPosition.Manual; 

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

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

将窗体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); }