Tipp 0046 MessageBox individuell gestalten
Autor/Einsender:
Datum:
  Jürgen Hardt
18.06.2004
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Der MessageBox in VB.Net wird nur die Show-Methode zur Verfügung gestellt. Eine individuelle Gestaltung und Positionierung auf dem Bildschirm ist auf normalen Wege nicht möglich. Abhilfe schafft eine Klasse, in der mit GDI+-Methoden das Aussehen der MessageBoxen bestimmt wird.
Code in frmMain
 
Public Class frmMain
  Inherits System.Windows.Forms.Form
  Dim xIB As InfoBox
Vom Windows Form Designer generierter Code
  Public Shared Sub Main()
    Application.EnableVisualStyles()
    Application.Run(New frmMain)
  End Sub

  Private Sub btnSample1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnSample1.Click
    With xIB
      .Location = New Point(100, 100)
      .Picture = .InfoBoxPicture.InfoPicture
      .Show("Der Vorgang wurde erfolgreich ausgeführt.", _
                          InfoBox.InfoBoxButtons.YesNo)
    End With
  End Sub

  Private Sub btnSample2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnSample2.Click

    Dim eResult As System.Windows.Forms.DialogResult
    With xIB
      .BackColor = Color.LightGreen
      .Location = New Point(200, 0)
      .Font = New Font("Comic Sans MS", 8)
      .Picture = .InfoBoxPicture.QuestionPicture
      eResult = .Show( _
            "Sollen alle Dateien wirklich gelöscht werden ?", _
            InfoBox.InfoBoxButtons.YesNo)
    End With

    Select Case eResult
      Case DialogResult.Yes
        '
      Case DialogResult.No
        '
    End Select
  End Sub

  Private Sub btnSample3_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles btnSample3.Click
    With xIB
      .GradientBackground = False
      .BackColor = Color.Aqua
      .ForeColor = Color.Blue
      .Location = New Point(0, 100)
      .Font = New Font("Tahoma", 8, FontStyle.Bold)
      .Picture = .InfoBoxPicture.ErrorPicture

      .Show("Die Grafik-Karte ist defekt. " & _
            "Tauschen Sie die Hardware umgehend aus.", _
            InfoBox.InfoBoxButtons.Abort)
    End With
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
  End Sub
End Class
 
Code in InfoBox
 
Imports System.Drawing.Drawing2D

Public NotInheritable Class InfoBox
  Inherits System.Windows.Forms.Form

  Private Shared tLocation As System.Drawing.Point
  Private Shared xFont As System.Drawing.Font
  Private Shared tSize As System.Drawing.Size
  Private Shared iBackColor As System.Drawing.Color = _
            System.Drawing.SystemColors.Control
  Private Shared iForeColor As System.Drawing.Color = _
            System.Drawing.SystemColors.ControlText
  Private Shared ePicture As InfoBoxPicture = _
            InfoBoxPicture.InfoPicture
  Private Shared bGradient As Boolean = True

  Private bExit As Boolean

  Public Enum InfoBoxPicture
    InfoPicture = 0
    QuestionPicture = 1
    ErrorPicture = 2
  End Enum

  Public Enum InfoBoxButtons
    OK = 0
    YesNo = 1
    OKCancel = 2
    RetryCancel = 3
    Abort = 4
  End Enum
Vom Windows Form Designer generierter Code
  Private Sub InfoBox_Paint(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.PaintEventArgs) _
          Handles MyBase.Paint

    If bGradient Then
      Dim xGraphics As Graphics = e.Graphics
      Dim xBrush As New LinearGradientBrush(New Point(0, 0), _
                                     New Point(Me.Width, 0), _
                                     Color.White, BackColor)

       xGraphics.FillRectangle(xBrush, Me.ClientRectangle)
     End If
  End Sub

  Protected Overrides Sub WndProc(ByRef m As _
          System.Windows.Forms.Message)
    If m.Msg = &H10 Then
      If Not bExit Then
        Return
      End If
    End If
    MyBase.WndProc(m)
  End Sub
End Class
 
Weitere Links zum Thema
Benutzerdefinierte MessageBox

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (16,6 kB) Downloads bisher: [ 1177 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Sonntag, 22. Januar 2012