AutoRedraw = True?

  • Thread starter Thread starter makai
  • Start date Start date
I have spent a whole day trying to do exactly that with no progress

the concept seems simple - create a bitmap of the form whenever anything is written to it and then simply copy the bitmap to the form in the Paint event

I am not even anywhere close to getting it
 
Paste your forms paint event here in this thread so we can have a look. It would help a lot more if we could see the code ourselves. :)
 
if you minimize and then restore the text is gone - trying the text generated in form activate and the button event

Code:
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private b1 As Bitmap

    Protected Overrides Sub OnActivated(ByVal e As System.EventArgs)
        Me.Text = "AutoRedraw Test"
        Me.BackColor = System.Drawing.Color.White

        FormPrint(".NET Printing", 10, 10)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FormPrint("2nd Printing", 100, 100)
    End Sub

    Private Sub FormPrint(ByVal t$, ByVal x1%, ByVal y1%)
        Dim g1 As Graphics = Me.CreateGraphics()

        System.Windows.Forms.Application.DoEvents()

        g1.DrawString(t, New Font("Verdana", 12), _
            Brushes.Black, x1, y1)

        System.Windows.Forms.Application.DoEvents()

        b1 = New Bitmap(Me.Size.Width, Me.Size.Height, g1)
    End Sub

    ' Redraw the form.
    Private Sub Form1_Paint( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
            Handles MyBase.Paint

        If Not (b1 Is Nothing) Then e.Graphics.DrawImage(b1, 0, 0)
    End Sub
End Class
 
no then I have to print everything that I already printed (something that I would not be able to do without restarting the whole program) - I just want to refresh what has already BEEN printed

the trick is knowing the graphics methods work in order to copy the form to a bitmap and then copying it back to the form in the paint event

apparently there is something to the graphics that does not work in my code
 
I see what you're trying to do (I think).

You have to call the Refresh method to trigger the Paint Event... so in your FormPrint method, add this as the last line;

Me.Refresh()
 
after 2 days I finally arrived at the solution - as I suspected it is fairly simple application of the Graphics Object

Code:
Option Strict On

Public Class Form1
    Inherits System.Windows.Forms.Form

    'Bitmap holds picture of the Form
    Private b1 As Bitmap
    'Graphics object (buffer)
    Private g1 As Graphics

    Private Sub Form1_Activated(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Activated

        Static done As Boolean = False

        If Not done Then
            Text = "AutoRedraw Test"
            BackColor = System.Drawing.Color.White

            'Create the initial bitmap from Form
            b1 = New Bitmap(Me.Size.Width, _
                Me.Size.Height, Me.CreateGraphics())

            'Create the Graphics Object buffer
            ' which ties the bitmap to it so that
            ' when you draw something on the object
            ' the bitmap is updated
            g1 = Graphics.FromImage(b1)

            'Print something
            FormPrint(".NET Printing", 10, 10)

            'Prevent reentry to initialization
            done = True
        End If
    End Sub

    'Print something else
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        FormPrint("2nd Printing", 100, 100)
    End Sub

    Private Sub FormPrint(ByVal t$, ByVal x1%, ByVal y1%)
        'Printing to the graphics buffer (not form)
        ' updates the associated bitmap b1
        g1.DrawString(t, New Font("Verdana", 12), _
            Brushes.Black, x1, y1)

        'Copy the bitmap to the form
        Me.CreateGraphics.DrawImage(b1, 0, 0)
    End Sub

    Private Sub Form1_Paint( _
        ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
            Handles MyBase.Paint

        'Copy the bitmap to the form
        e.Graphics.DrawImage(b1, 0, 0)
    End Sub

End Class
 
Back
Top