Dat*_*ase 6 c# winforms notification-area
我想在系统托盘正上方的右下角显示一个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();
}
Run Code Online (Sandbox Code Playgroud)
我刚试过这个并且它对我有用(注意:这个代码必须在表单第一次显示之后出现- 例如,你可以把它放在表单的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);
Run Code Online (Sandbox Code Playgroud)
是否使用WorkingArea或Bounds取决于"over"的含义:如果您的意思是"在前面",则使用Bounds,因为它包括覆盖整个屏幕的区域(包括系统托盘占用的空间); 如果你的意思是"在上面",那么使用WorkingArea,它只包括用户的桌面.
另外,让我澄清一下,你希望你的实际表格显示在那里,对吧?如果您想在通知区域中有一个图标,那就是该NotifyIcon组件的用途.
如果您想将表单放置在任务栏上方/前方:
将表单 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);
}
Run Code Online (Sandbox Code Playgroud)
你忘了这个:
fm.StartPosition = FormStartPosition.Manual;
Run Code Online (Sandbox Code Playgroud)
接下来要做的就是将任务栏放在屏幕的左侧,并在视频DPI设置为不同值(如125)的计算机上运行代码.您只能在Load事件中准确定位表单.不要设置客户端大小.