【问题标题】:How to place one form just above another in winforms?如何在winforms中将一个表格放在另一个表格上方?
【发布时间】:2014-06-06 15:30:14
【问题描述】:

我正在创建一个 winforms 应用程序,我会不时地在其中收到一些消息或事件的通知。 我期待的通知样式类似于 Gtalk,如果用户发送消息,它会在屏幕右下角显示通知,如果同时有来自另一个用户的消息,则会出现新的通知窗口将显示在前一个的上方。新窗口不会与旧窗口重叠或遮盖旧窗口。

到目前为止,我取得了一些成就

在构造函数中使用此代码获取屏幕右下角的窗口,这不是一项大任务

    Rectangle workingArea = Screen.GetWorkingArea(this);
    this.Location = new Point(workingArea.Right - Size.Width, workingArea.Bottom - Size.Height);

但是现在一旦在屏幕右下角打开名为“通知”的表单。当一个新的通知到来时,它只会与之前的表单重叠。有什么我可以为此做的吗?我错过了一些非常明显的东西吗?

【问题讨论】:

  • 或者在windows窗体中你也可以使用panel control而不是其他窗体
  • @LifeRunsOnCode 关于如何使用面板控件进行通知的任何建议
  • 只需发布您尝试添加panel的表单屏幕截图
  • 我认为您也可以使用notifyIcon control,这有助于在taskbar(屏幕右上角)中弹出通知
  • 我只需为正在显示的通知创建一个引用变量。每次需要新通知时,旧通知都会关闭。新的链接到参考,然后显示到屏幕上。我认为这是这种情况的基本方法。

标签: c# winforms notifications new-window


【解决方案1】:

这是带有按钮的父表单,它创建了一个新的通知表单:

public partial class Parent_Form : Form
{
    public static List<Form> activeNotifications = new List<Form>();

    public Parent_Form()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Notification notification = new Notification();
        activeNotifications.Add(notification);
        notification.Show();
    }

    public static void SortNotifications()
    {
        int additionalHeight = 0;
        foreach (Form notification in activeNotifications)
        {
            notification.Location = new Point(0, (0 + additionalHeight));
            additionalHeight += notification.Height;
        }
    }

    public static Point GetLocation()
    {
        int height = 0;
        foreach (Form notification in Parent_Form.activeNotifications) { height += notification.Height; }
        return new Point(0, height);
    }
}

父窗体包含一个button1,用于创建新的通知

这是通知表单示例:

public partial class Notification : Form
{
    public Notification()
    {
        InitializeComponent();
        this.Location = Parent_Form.GetLocation();
        this.FormClosing += Notification_FormClosing;
    }

    private void button1_Click(object sender, EventArgs e) { this.Close(); }

    private void Notification_FormClosing(object sender, FormClosingEventArgs e)
    {
        Parent_Form.activeNotifications.Remove(this);
        Parent_Form.SortNotifications();
    }
}

Notification 仅包含 button1,用于关闭通知表单。确保通知表单使用 StartPosition "Manual"。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2013-12-02
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多