[VB] Toast in .NET

Chrono

Grünschnabel
Hallo Gemeinde,

hört sich vielleicht etwas komisch an, aber weiß jemand, wie ich ein Toast in .NET machen kann?

Ich weiß nicht ob es wirklich Toast heißt, aber ihr kennt es sicherlich von den Instant Messagern (ICQ, MSN). Es springt quasi wie ein Toast rechts unten über den Tray Icons ein Fenster mit einer Benachrichtigung hoch.

Ich suche keine Baloon- oder NotifyMessage, sondern wirklich so ein Toast Fenster. :)

Wäre nett wenn ihr mir helfen könntet, ihr braucht keine ganzen ausgeschmückten Codes zu schicken, ein paar Tipps, Gedankenanstöße würden reichen. ;)

Chrono
 
Kannst eigentlich ein ganz normales Form machen. Dieses musst du nur an der richtigen Stelle einblenden und hochfahren lassen, oder wie du es auch immer visualisieren willst. Da ist im Grunde nicht viel dahinter.
 
So! Habs schon ein bisschen geschafft....!

Auf der Seite von Microsoft gibt es eine tolle Hilfe. Zwar in Englisch aber egal!

Mein Code sieht folgender maßen aus:

Toast.vb
Code:
Imports System.Drawing.Drawing2D
Imports System.Drawing
Imports System.Windows.Forms

Public Class Toast

    Dim colorBackground1 As Color = Color.FromArgb(87, 140, 188)
    Dim colorBackground2 As Color = Color.FromArgb(181, 220, 237)
    Dim colorRahmen As Color = Color.FromArgb(66, 118, 158)
    Dim penRahmen As New Pen(colorRahmen)
    Dim imageXButtonInAktiv As Image = Image.FromHbitmap(My.Resources.X_Button_InAktiv.GetHbitmap)


    Public Shared Function CreateRoundedRectPath(ByVal rect As Rectangle, ByVal radius As Integer) As GraphicsPath

        Dim roundRect As New GraphicsPath

        ' top line
        roundRect.AddLine(rect.Left + radius, rect.Top, _
            rect.Right - radius, rect.Top)

        ' Upper-right corner
        roundRect.AddArc(rect.Right - 2 * radius, _
            rect.Top, radius * 2, radius * 2, 270, 90)

        ' right edge
        roundRect.AddLine(rect.Right, rect.Top + radius, _
            rect.Right, rect.Bottom - 10)

        ' bottom-right corner
        roundRect.AddArc(rect.Right - radius * 2, _
            rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90)

        ' bottom
        roundRect.AddLine(rect.Right - 2 * radius, rect.Bottom, _
            rect.Left + radius, rect.Bottom)

        ' bottom-left edge
        roundRect.AddArc(rect.Left, rect.Bottom - 2 * radius, _
            2 * radius, 2 * radius, 90, 90)

        ' left side
        roundRect.AddLine(rect.Left, rect.Bottom - radius, _
            rect.Left, rect.Top + radius)

        ' upper-left corner 
        roundRect.AddArc(rect.Left, rect.Top, 2 * radius, _
            2 * radius, 180, 90)

        Return roundRect
    End Function
    Dim fontbla As Font

    Private Sub Toast_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim myRecTangle As GraphicsPath = CreateRoundedRectPath(New Rectangle(0, 0, 190, 116), 12)

        Dim blasds As New LinearGradientBrush(New Rectangle(0, 0, 190, 116), colorBackground1, colorBackground2, LinearGradientMode.Vertical)

        e.Graphics.FillPath(blasds, myRecTangle)
        e.Graphics.DrawPath(penRahmen, myRecTangle)
        e.Graphics.DrawImage(imageXButtonInAktiv, 170, 10)

        e.Graphics.DrawString("dsd", fontbla, Brushes.Black, 10, 10)

    End Sub


    Private Sub Toast_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        fontbla = New Font("Arial", 10)
        Me.SetDesktopLocation((Screen.PrimaryScreen.Bounds.Width - Me.Size.Width), (Screen.PrimaryScreen.Bounds.Height - Me.Size.Height - 27))
    End Sub

    Private Sub Toast_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
        Timer2.Stop()
        Timer1.Start()
    End Sub


    Private Sub Toast_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
        Timer1.Stop()
        Timer2.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.Opacity = Me.Opacity * 1.25
        If Me.Opacity > 0.8 Then
            Timer1.Stop()
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Me.Opacity = Me.Opacity * 0.75
        If Me.Opacity < 0.4 Then
            Timer2.Stop()
        End If
    End Sub
End Class

Toast.Designer.vb
Code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Toast
    Inherits System.Windows.Forms.Form

    'Das Formular überschreibt den Löschvorgang, um die Komponentenliste zu bereinigen.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Wird vom Windows Form-Designer benötigt.
    Private components As System.ComponentModel.IContainer

    'Hinweis: Die folgende Prozedur ist für den Windows Form-Designer erforderlich.
    'Das Bearbeiten ist mit dem Windows Form-Designer möglich.  
    'Das Bearbeiten mit dem Code-Editor ist nicht möglich.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.Timer2 = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'Timer1
        '
        '
        'Timer2
        '
        '
        'Toast
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.BackColor = System.Drawing.Color.Red
        Me.ClientSize = New System.Drawing.Size(195, 120)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "Toast"
        Me.Opacity = 0.5
        Me.ShowInTaskbar = False
        Me.Text = "Toast"
        Me.TransparencyKey = System.Drawing.Color.Red
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    Friend WithEvents Timer2 As System.Windows.Forms.Timer
End Class

Vielleicht kann ja jemand etwas mit anfangen =)

Guckt euch das Screenshot an!

Chrono
 

Anhänge

  • 26741attachment.png
    26741attachment.png
    7,5 KB · Aufrufe: 37

Neue Beiträge

Zurück