Tipp 0198 GDI Graphics vielseitig verwenden
Autor/Einsender:
Datum:
  Thomas Becker
16.02.2010
Entwicklungsumgebung:   VB.Net 2005
Framework:   2.0
Bitmaperstellung, Druck und Zeichnen auf Steuerelementen haben eines gemeinsam: 
Sie nutzen ihren jeweiligen Graphics-Kontext. Daher liegt bei Bedarf eine gemeinsame Nutzung 
der Zeichnung nahe. Dazu muss lediglich einer Methode der entsprechende Graphics-Parameter übergeben werden und die Zeichnung landet auf dem gewünschten Kontext.
 
Public Class Form1
  Private WithEvents DruckDoc As New Printing.PrintDocument
  Dim Rect As Rectangle = New Rectangle(0, 0, 120, 135)
  Dim GroupBoxPaint As Boolean

  Private Sub Malen(ByVal gr As Graphics)
    ' Irgendwelche "Grafik"
    gr.DrawEllipse(Pens.Blue, 20, 30, 80, 80)
    gr.DrawLine(Pens.Red, 20, 70, 100, 70)
    gr.DrawIcon(Me.Icon, 51, 45)

    Dim fnt As Font = New Font("Arial", 11, FontStyle.Bold, _
        GraphicsUnit.Pixel)
    gr.DrawString("Test", fnt, Brushes.Blue, 45, 80)
  End Sub

  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
     System.Windows.Forms.PaintEventArgs) Handles Me.Paint
     ' Selbstzeichnung auf Form
    Dim gr As Graphics = e.Graphics
    Call Malen(gr)
  End Sub

  Private Sub GroupBox1_Paint(ByVal sender As Object, ByVal e As_
     System.Windows.Forms.PaintEventArgs) Handles GroupBox1.Paint
     ' Selbstzeichnung auf GroupBox
    If GroupBoxPaint = True Then
      Dim gr As Graphics = e.Graphics
      Call Malen(gr)
    End If
  End Sub

  Private Sub Drucken(ByVal sender As Object, ByVal e As _
    Drawing.Printing.PrintPageEventArgs) Handles DruckDoc.PrintPage
    Dim gr As Graphics = e.Graphics
    Call Malen(e.Graphics)
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal _
     e As System.EventArgs) Handles Button1.Click
    ' auf Groupbox
    Dim gr As Graphics = GroupBox1.CreateGraphics
    Call Malen(gr)

    GroupBoxPaint = True
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal _
     e As System.EventArgs) Handles Button2.Click
     ' zur Bitmap
    Dim Bmp As Bitmap = New Bitmap(Rect.Width, Rect.Height)
    Dim gr As Graphics = Graphics.FromImage(Bmp)
    gr.FillRectangle(Brushes.White, Rect)
    Call Malen(gr)
    Clipboard.SetImage(Bmp)
    MsgBox("Die Bitmap ist in der Zwischenablage")
  End Sub

  Private Sub Button3_Click(ByVal sender As System.Object, ByVal _
     e As System.EventArgs) Handles Button3.Click
     ' zum Drucker
    Dim printpreviewDlg As New PrintPreviewDialog
    With printpreviewDlg
      .Document = DruckDoc
      .WindowState = FormWindowState.Maximized
      .ShowDialog(Me)
    End With
    printpreviewDlg = Nothing
  End Sub

  Private Sub Button4_Click(ByVal sender As System.Object, ByVal _
     e As System.EventArgs) Handles Button4.Click
     ' Beenden
     Me.Close()
   End Sub
End Class
 
Weitere Links zum Thema
Figuren zeichnen und rotieren
Zeichnen auf dem Desktop

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


Download  (13 kB) Downloads bisher: [ 356 ]

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: Dienstag, 17. Januar 2012