如何使通知窗口更像吐司?

时间:2018-12-23 19:03:44

标签: c# notifications

我正在为我的Package Tracker应用程序创建一个通知系统...我需要知道如何使这种吐司风格比现在更高效...

我想要完成的事情:

  1. 当显示多个通知时,将其从顶部停靠到 屏幕右下方(寻找更好的解决方案)<<<完成!
  2. 当1条通知关闭时,将其下方的所有通知上移... <<<完成!

  3. 粘滞通知当计算机空闲时启用。 <<<完成!

  4. 如果特定项目存在通知,它将被替换而不是 创建一个新的通知。 <<<完成!

要显示通知,我使用:

#region Check if Notification Exists

foreach (Form f in ActiveNotifications)
{
    string[] windowID = f.Text.Split('|');

    if (windowID[1] == NotificationTrackingNumber)
    {
        NotificationFound = true;
        f.Text = "Description=" + NotificationDescription;
        f.Text = "Status=" + NotificationStatus;
    }
}

#endregion Check if Notification Exists

if (NotificationFound)
{
    NotificationFound = false;
}
else if (!NotificationFound)
{
    notification = new Notification(NotificationDescription, NotificationStatus, NotificationTrackingNumber, min, sec, WindowID);
    notification.Closed += new EventHandler(Notification_Closed);
    notification.Show();
    ActiveNotifications.Add(notification);
    WindowID++;
}

要在屏幕的右侧上下放置它们,请使用:

Rectangle res = Screen.PrimaryScreen.Bounds;           
this.Location = new Point(res.Width - 5 - Size.Width, (this.Height + 16) * multiplier + 6);

要重新定位已关闭通知下方的所有活动通知

public void Notification_Closed(object sender, EventArgs e)
{
    WindowID--;
    List<Form> TempNotificationList1 = new List<Form>();
    List<Form> TempNotificationList2 = new List<Form>();
    Point newFormPoint = NotificationSettings.notificaionLocation;
    Point oldFormPoint;
    int delIndex = -1;
    int newWindowID = 0;
    int winHandleIndex = 0;
    int ClosedNotificationWindowID = NotificationSettings.WindowMultiplier;

    #region Deletes Closed Notification Window from ActivateNotifications

    while (winHandleIndex < ActiveNotifications.Count)
    {
        if (ActiveNotifications[winHandleIndex].Handle == NotificationSettings.notificationClosedID)
        {
            delIndex = winHandleIndex;
            break;
        }

        winHandleIndex++;
    }

    if (delIndex > -1)
    {
        ActiveNotifications.RemoveAt(delIndex);
    }

    #endregion Deletes Closed Notification Window from ActivateNotifications

    #region Changes the Location Active Notifications

    while (ClosedNotificationWindowID < ActiveNotifications.Count)
    {
        string[] windowID = ActiveNotifications[ClosedNotificationWindowID].Text.Split('|');
        oldFormPoint = ActiveNotifications[ClosedNotificationWindowID].Location;
        ActiveNotifications[ClosedNotificationWindowID].Location = newFormPoint;
        newFormPoint = oldFormPoint;
        TempNotificationList1.Add(ActiveNotifications[ClosedNotificationWindowID]);
        ClosedNotificationWindowID++;
    }

    #endregion Changes the Location Active Notifications

    #region Merges ActiveNotifications &  TempNotificationList1

    foreach (Form f in ActiveNotifications)
    {
        if (!TempNotificationList1.Contains(f))
        {
            TempNotificationList2.Add(f);
        }
    }

    foreach (Form f in TempNotificationList1)
    {
        if (!TempNotificationList2.Contains(f))
        {
            TempNotificationList2.Add(f);
        }
    }

    #endregion Merges ActiveNotifications &  TempNotificationList1

    #region Rebuilds Active Notifications List

    ActiveNotifications.Clear();
    foreach (Form f in TempNotificationList2)
    {
        string[] windowID = f.Text.Split('|');
        f.Text = newWindowID.ToString() + "|" + windowID[1];
        ActiveNotifications.Add(f);
        newWindowID++;
    }

    TempNotificationList1 = null;

    #endregion Rebuilds Active Notifications List
}

好吧,代码可以工作,但是有没有更有效的方法?乘数实际上只是窗口计数...这是到目前为止的工作方式的图片...

my notification Window as of now

0 个答案:

没有答案