locked
Call Function with PaintEventArgs RRS feed

  • Question

  • I have the following function to Antialias my text:

    Public Function Form1_Paint(ByVal sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
            Dim myChosenFont As New Font("Segoe WP", 100, FontStyle.Regular, GraphicsUnit.Pixel)
            Dim myChosenFont2 As New Font("Segoe WP", 18, FontStyle.Regular, GraphicsUnit.Pixel)
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
            e.Graphics.DrawString(Date.Now.ToShortTimeString, myChosenFont, Brushes.White, 0, -5)
            e.Graphics.DrawString(Date.Now.ToLongDateString, myChosenFont2, Brushes.White, 15, 115)

    ...And I want to call this Function on a timer. Is that possible? How?
    Saturday, November 16, 2013 7:53 PM

Answers

  • Hi,

     You could use Me.Refresh in the timer tick event to make the form redraw its client area. This works in my test form. However, maybe you will want to compare a saved Date to Date.Now to see if the minutes have changed before Refreshing the form just so it is not redrawing everything on the form every second.

    Imports System.Drawing.Text
    
    Public Class Form1
        Dim WithEvents Tmr As New Timer With {.Interval = 1000, .Enabled = True}
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim myChosenFont As New Font("Segoe WP", 100, FontStyle.Regular, GraphicsUnit.Pixel)
            Dim myChosenFont2 As New Font("Segoe WP", 18, FontStyle.Regular, GraphicsUnit.Pixel)
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
            e.Graphics.DrawString(Date.Now.ToShortTimeString, myChosenFont, Brushes.White, 0, -5)
            e.Graphics.DrawString(Date.Now.ToLongDateString, myChosenFont2, Brushes.White, 15, 115)
        End Sub
    
        Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
            Me.Refresh()
        End Sub
    End Class


     I forgot to mention you will want to set the Form`s DoubleBuffered property to True so it does not flicker when it repaints the form.
    Saturday, November 16, 2013 8:31 PM
  • You can learn to do your own drawing

    'module level declares ' Buffer for maindrawing screen Public m_BufferBitmap As Bitmap Public m_BufferGraphics As Graphics 'in a sub routine do the drawing you do in a paint event 'then save it to the bitmap then copy the background 'bitmap to the screen

    'call the sub routines

    CopyDrawingBackground(FormMain.PictureBox1.CreateGraphics(), m_BufferBitmap) SaveDrawingBackground(grWidth, grHeight, m_BufferGraphics, m_BufferBitmap) Public Sub SaveDrawingBackground(ByVal w As Integer, ByVal h As Integer, ByRef thisBufferGraphics As Graphics, ByRef thisBufferBitmap As Bitmap) Dim new_bitmap As Bitmap ' Make a new bitmap that fits the form. new_bitmap = New Bitmap(w, h) thisBufferGraphics = Graphics.FromImage(new_bitmap) ' Clear the bitmap. thisBufferGraphics.Clear(Color.FromArgb(Cadrail.BackgroundColor)) ' Copy the existing bitmap's contents into ' the new bitmap. If Not (thisBufferBitmap Is Nothing) Then thisBufferGraphics.DrawImage(thisBufferBitmap, 0, 0) End If ' Save the new bitmap and graphics objects. thisBufferBitmap = new_bitmap End Sub Public Sub CopyDrawingBackground(ByVal gr As Graphics, ByVal the_BufferBitmap As Bitmap) If Not (the_BufferBitmap Is Nothing) Then 'tlh 7-20-10 must reset or background copies wrong in windows 7 gr.ResetTransform() gr.DrawImage(the_BufferBitmap, 0, 0) End If End Sub

    You can search for this type of thing with more instructions.

    There may be an easier way to call the paint event, maybe just add the timer.tick event to the paint handles? But I do lots of drawing so I just take care of it this way it is good to learn.

    I don't know why this forum show my code inserts green, what am I doing wrong?


    Saturday, November 16, 2013 8:35 PM

All replies

  • Hi,

     You could use Me.Refresh in the timer tick event to make the form redraw its client area. This works in my test form. However, maybe you will want to compare a saved Date to Date.Now to see if the minutes have changed before Refreshing the form just so it is not redrawing everything on the form every second.

    Imports System.Drawing.Text
    
    Public Class Form1
        Dim WithEvents Tmr As New Timer With {.Interval = 1000, .Enabled = True}
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim myChosenFont As New Font("Segoe WP", 100, FontStyle.Regular, GraphicsUnit.Pixel)
            Dim myChosenFont2 As New Font("Segoe WP", 18, FontStyle.Regular, GraphicsUnit.Pixel)
            e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
            e.Graphics.DrawString(Date.Now.ToShortTimeString, myChosenFont, Brushes.White, 0, -5)
            e.Graphics.DrawString(Date.Now.ToLongDateString, myChosenFont2, Brushes.White, 15, 115)
        End Sub
    
        Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
            Me.Refresh()
        End Sub
    End Class


     I forgot to mention you will want to set the Form`s DoubleBuffered property to True so it does not flicker when it repaints the form.
    Saturday, November 16, 2013 8:31 PM
  • You can learn to do your own drawing

    'module level declares ' Buffer for maindrawing screen Public m_BufferBitmap As Bitmap Public m_BufferGraphics As Graphics 'in a sub routine do the drawing you do in a paint event 'then save it to the bitmap then copy the background 'bitmap to the screen

    'call the sub routines

    CopyDrawingBackground(FormMain.PictureBox1.CreateGraphics(), m_BufferBitmap) SaveDrawingBackground(grWidth, grHeight, m_BufferGraphics, m_BufferBitmap) Public Sub SaveDrawingBackground(ByVal w As Integer, ByVal h As Integer, ByRef thisBufferGraphics As Graphics, ByRef thisBufferBitmap As Bitmap) Dim new_bitmap As Bitmap ' Make a new bitmap that fits the form. new_bitmap = New Bitmap(w, h) thisBufferGraphics = Graphics.FromImage(new_bitmap) ' Clear the bitmap. thisBufferGraphics.Clear(Color.FromArgb(Cadrail.BackgroundColor)) ' Copy the existing bitmap's contents into ' the new bitmap. If Not (thisBufferBitmap Is Nothing) Then thisBufferGraphics.DrawImage(thisBufferBitmap, 0, 0) End If ' Save the new bitmap and graphics objects. thisBufferBitmap = new_bitmap End Sub Public Sub CopyDrawingBackground(ByVal gr As Graphics, ByVal the_BufferBitmap As Bitmap) If Not (the_BufferBitmap Is Nothing) Then 'tlh 7-20-10 must reset or background copies wrong in windows 7 gr.ResetTransform() gr.DrawImage(the_BufferBitmap, 0, 0) End If End Sub

    You can search for this type of thing with more instructions.

    There may be an easier way to call the paint event, maybe just add the timer.tick event to the paint handles? But I do lots of drawing so I just take care of it this way it is good to learn.

    I don't know why this forum show my code inserts green, what am I doing wrong?


    Saturday, November 16, 2013 8:35 PM
  • I don't know why this forum show my code inserts green, what am I doing wrong.

    When you click the button to add code to your post you can choose from the Language combobox at the top "VB.Net". That will change the colors of the text in the code block.   :)
    Saturday, November 16, 2013 8:43 PM
  • You can learn to do your own drawing

    You will have to think to use code I posted as it is not complete. Here is a better explanation:

    http://www.vb-helper.com/howto_net_rubberband_line.html

    I have just shown the basics and you can search for the keywords to find many examples.

    Saturday, November 16, 2013 8:46 PM
  • I don't know why this forum show my code inserts green, what am I doing wrong.

    When you click the button to add code to your post you can choose from the Language combobox at the top "VB.Net". That will change the colors of the text in the code block.   :)

    Ahhhsooo. I always forget that.

    Thanks,

    Tom

    Saturday, November 16, 2013 9:02 PM
  • You can learn to do your own drawing

    So sorry, I  answer too fast to be the first one! Just in case anyone is listening.

    In my example, in a separate drawing subroutine, you draw to the m_BufferGraphics  like you do now with e.graphics in the paint event. Then you SaveDrawingBackground the graphics you drew to your background bitmap m_BufferBitmap. Then you CopyDrawingBackground the background bitmap to the graphics object like your picture box. Now you can call your drawing sub routine from the paint event, or any time you want to draw it. And you have a memory image of the drawing you can copy to another object anytime you want. Very handy.

    Saturday, November 16, 2013 9:45 PM