Announcement

Collapse
No announcement yet.

NotifyIcon - BalloonTip - System-Icon verschwindet nicht.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • NotifyIcon - BalloonTip - System-Icon verschwindet nicht.

    Hallo zusammen,

    ich lasse mir einen BalloonTip anzeigen, allerdings habe ich Schwierigkeiten, das System-Icon (TryIcon) unten rechts verschwinden zu lassen, sobald ich die Anzeige mehrmals hintereinander aufrufe.

    IDE:: VS 2012; .NET:: 4.5; Example-Tools im Anhang

    Ich habe mir ein Projekt erstellt und über einen Button wird der BalloonTip angezeigt. Wenn ich nichts unternehme, verschwinden auch die TryIcons. Wird aber der Button mehrmals gedrückt oder ich klicke auf den BalloonTip, erscheinen mehrere TryIcons des BalloonTips. Selbst, wenn ich das Programm schließe.

    Im Anhang das Example Tool

    Hier mein Code für das Ausführen des Buttons und des Schließen des Fensters.
    [highlight=csharp]
    private void BtnNotifyIcon_Click(object sender, EventArgs e)
    {
    _windowMessage.ShowBalloonTip(
    " Ruft den Rückgabewert des asynchronen Vorgangs ab, der durch das übergebene IAsyncResult dargestellt wird." +
    " (Von Control geerbt.)","Titel",EnumToolTipIcon.Info,EnumSysIcon s.Question,2);
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    _windowMessage.DisposeBalloonTip();
    }
    [/highlight]

    Hier der Code in dem Projekt
    [highlight=csharp]
    public void ShowBalloonTip(string text, string caption = "", NICON notifyIcon = NICON.None,
    SYSICON sysIcons = SYSICON.Information, int seconds = 3)
    {
    _balloontipSeconds = seconds * 1000;
    notify = new NotifyIcon();
    notify.BalloonTipText = text;
    notify.BalloonTipTitle = caption;
    notify.BalloonTipIcon = (ToolTipIcon) notifyIcon;
    notify.Icon = GetSystemIcon(sysIcons);
    notify.Visible = true;
    notify.ShowBalloonTip(_balloontipSeconds);
    _backgroundworker1 = new BackgroundWorker();
    _backgroundworker1.RunWorkerAsync();
    _backgroundworker1.DoWork += Backgroundworker_DoWork;
    _backgroundworker1.RunWorkerCompleted += Backgroundworker1_RunWorkerCompleted;
    }

    private void Backgroundworker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    _backgroundworker1.Dispose();
    }

    private void Backgroundworker_DoWork(object sender, DoWorkEventArgs e)
    {
    var worker = sender as BackgroundWorker;

    if (worker != null && worker.CancellationPending)
    {
    e.Cancel = true;
    }
    else
    {
    Thread.Sleep((_balloontipSeconds*4));
    DisposeBalloonTip();
    }
    }

    private Icon GetSystemIcon(SYSICON sysIcons)
    {
    switch (sysIcons)
    {
    case SYSICON.Application:
    return SystemIcons.Application;
    case SYSICON.Error:
    return SystemIcons.Error;
    case SYSICON.Information:
    return SystemIcons.Information;
    case SYSICON.Question:
    return SystemIcons.Question;
    case SYSICON.Shield:
    return SystemIcons.Shield;
    case SYSICON.Warning:
    return SystemIcons.Warning;
    case SYSICON.WinLogo:
    return SystemIcons.WinLogo;
    default:
    throw new ArgumentOutOfRangeException("sysIcons", "Cannot resulve the Icon for the BalloonToolTip");
    }
    }

    public void DisposeBalloonTip()
    {
    if (notify != null) notify.Dispose();
    }
    [/highlight]

    Grüße Lerando
    Attached Files

  • #2
    Probier mal folgendes, vielleicht bringt dich das deiner lösung näher

    [highlight=csharp]
    private void BtnTest_Click(object sender, EventArgs e)
    {
    if (notifyIcon != null)
    notifyIcon.Visible = !notifyIcon.Visible ;
    }
    [/highlight]

    Ansonsten hier meine Lösung um nur max. eine Ballon anzuzeigen:

    File: Form1.cs
    [highlight=csharp]
    private void BtnNotifyIcon_Click(object sender, EventArgs e)
    {
    if(!_windowMessage.BalloonTipActive) // <--- neu
    _windowMessage.ShowBalloonTip(
    " Ruft den Rückgabewert des asynchronen Vorgangs ab, der durch das übergebene IAsyncResult dargestellt wird." +
    " (Von Control geerbt.)","Titel",EnumToolTipIcon.Info,EnumSysIcon s.Question,2);
    else // <--- neu
    _windowMessage.ShowMessage("schon aktiv"); // <--- neu
    }
    [/highlight]

    File: WindowMessage.cs
    [highlight=csharp]
    /// <summary>
    /// Lässt einen Balloontip in der TaskIcon-Leiste erscheinen.
    /// <para>WICHTIG!!! Beim Schließen des Programmes muss die Methode DisposeBalloonTip() aufgerufen werden</para>
    /// <para>Es stehen die Enumationen EnumSysIcons und EnumNotifyIcon zur Verfügung </para>
    /// </summary>
    /// <param name="text">Der Text der angezeigt werden soll</param>
    /// <param name="caption">Der Titel</param>
    /// <param name="notifyIcon">Das Icon, das neben dem Text erscheinen soll</param>
    /// <param name="sysIcons">Das Icon, das in der Tooltip-Leiste erscheinen soll</param>
    /// <param name="seconds">Sekunden, die der BalloonTip angezeigt werden soll</param>
    public void ShowBalloonTip(string text, string caption = "", NICON notifyIcon = NICON.None,
    SYSICON sysIcons = SYSICON.Information, int seconds = 3)
    {
    _balloontipSeconds = seconds * 1000;

    notify = new NotifyIcon();

    notify.BalloonTipText = text;
    notify.BalloonTipTitle = caption;
    notify.BalloonTipIcon = (ToolTipIcon) notifyIcon;
    notify.Icon = GetSystemIcon(sysIcons);

    notify.Visible = true;
    _balloonTipActive= true; // <--- neu
    notify.ShowBalloonTip(_balloontipSeconds);

    _backgroundworker1 = new BackgroundWorker();
    _backgroundworker1.RunWorkerAsync();
    _backgroundworker1.DoWork += Backgroundworker_DoWork;
    _backgroundworker1.RunWorkerCompleted += Backgroundworker1_RunWorkerCompleted;
    }
    [/highlight]

    [highlight=csharp]
    private bool _balloonTipActive; // <--- neu
    public bool BalloonTipActive { // <--- neu
    get { return _balloonTipActive; } // <--- neu
    } // <--- neu
    public void DisposeBalloonTip()
    {
    if (notify != null) notify.Dispose();
    _balloonTipActive= false; // <--- neu
    }
    [/highlight]
    Zuletzt editiert von Nooa; 19.07.2013, 12:53.

    Comment


    • #3
      @Nooa:::
      Hat wunderbar funktioniert.

      Vielen Dank.

      Gruß Lerando

      Comment

      Working...
      X