首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在winforms中将一个表单放在另一个窗体上?

如何在winforms中将一个表单放在另一个窗体上?
EN

Stack Overflow用户
提问于 2014-04-22 10:09:36
回答 1查看 848关注 0票数 0

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

到目前为止,我几乎没有什么成就。

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

代码语言:javascript
运行
复制
    Rectangle workingArea = Screen.GetWorkingArea(this);
    this.Location = new Point(workingArea.Right - Size.Width, workingArea.Bottom - Size.Height);

但是现在,在屏幕右下角打开名为“”的表单之后。当一个新的通知出现时,它只是与以前的表单重叠。有什么我能做的吗。我错过了什么很明显的东西吗?

EN

回答 1

Stack Overflow用户

发布于 2014-04-23 12:55:34

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

代码语言:javascript
运行
复制
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,用于创建新的通知。

这是通知表单示例:

代码语言:javascript
运行
复制
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();
    }
}

通知仅包括用于关闭通知表单的button1。确保通知表单使用StartPosition“手册”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23216511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档