Results 1 to 2 of 2

Thread: GDI+ and PictureBox AutoRedraw, CLS, Refresh Issues

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    658

    GDI+ and PictureBox AutoRedraw, CLS, Refresh Issues

    What am I not understanding about a Picture Box's Cls method and Refresh method?

    A simple program to draw a line using GDI+ below.

    I would like to clear the Picture Box each time the Command Button is clicked,
    but if I issue a "Clear Screen" instruction (no matter where) the line does not draw.

    If a clear screen is not done, the red line gets darker and darker each time the button is clicked.
    I don't want this.

    Can someone help me to understand the problem and solution?

    Code:
    Option Explicit
    
    
    Dim gdiplusToken As Long
    
    Dim stat As Long
    
    Dim graphics As Long
    Dim penRed As Long
    
    
    Private Sub Form_Load()
    
        Form1.Caption = "GDI+"
        Form1.Width = Screen.TwipsPerPixelX * 600
        Form1.Height = Screen.TwipsPerPixelY * 465
        Form1.BackColor = &H8000000F
        Form1.ScaleMode = vbPixels
        
        Picture1.Appearance = 0
        Picture1.Left = 16
        Picture1.Top = 16
        Picture1.Height = 366
        Picture1.Width = 552
        Picture1.AutoRedraw = True
        Picture1.ScaleMode = vbPixels
        
        Command1.Width = Picture1.Width
        Command1.Height = 25
        Command1.Left = Picture1.Left
        Command1.Top = Picture1.Top + Picture1.Height + 10
        Command1.Caption = "Draw"
     
        ' Initialize Windows GDI+
        Dim GdiplusStartupInput As GdiplusStartupInput
        GdiplusStartupInput.GdiplusVersion = 1
        GdiplusStartupInput.DebugEventCallback = 0
        GdiplusStartupInput.SuppressBackgroundThread = False
        GdiplusStartupInput.SuppressExternalCodecs = False
        Dim status As GpStatus
        status = GdiplusStartup(gdiplusToken, GdiplusStartupInput, 0)
        If status <> Ok Then
            MsgBox "Error loading GDI+!", vbCritical
            Call GdiplusShutdown(gdiplusToken)
        End If
        
        Call GraphicsInitialize
    
    End Sub
    
    
    Private Sub Form_Unload(Cancel As Integer)
    
        Call GraphicsUninitialize
        
        ' Clean up resources used by Windows GDI+
        Call GdiplusShutdown(gdiplusToken)
        
    End Sub
    
    
    Private Sub Command1_Click()
        
        'Picture1.Cls
        
        Call GdipSample
        
        Picture1.Refresh
        
    End Sub
    
    
    Private Sub GdipSample()
    
        'Picture1.Cls
    
        Call DrawSomeLine(graphics)
    
        'Picture1.Refresh
    
    End Sub
    
    
    Public Sub DrawSomeLine(graphics As Long)
        
        'Picture1.Cls
        
        stat = GdipDrawLine(graphics, penRed, 10, 10, 50, 30)
    
        'Picture1.Refresh
        
    End Sub
    
    
    Public Sub GraphicsInitialize()
    
        stat = GdipCreateFromHDC(Picture1.hdc, graphics)
        stat = GdipSetSmoothingMode(graphics, SmoothingModeAntiAlias)
        stat = GdipCreatePen1(&HFFFF0000, 1, UnitPixel, penRed)
        
    End Sub
    
    
    Public Sub GraphicsUninitialize()
    
        stat = GdipDeletePen(penRed)
        stat = GdipDeleteGraphics(graphics)
    
    End Sub
    Last edited by mms_; Apr 19th, 2024 at 11:29 AM.

  2. #2
    Addicted Member
    Join Date
    Feb 2015
    Posts
    176

    Re: GDI+ and PictureBox AutoRedraw, CLS, Refresh Issues

    Colors must be in ARGB format for use with GDI+: i.e. color, opacity



    Code:
    Public Sub GraphicsInitialize()
    
        Dim PenCol as Long
    
        PenCol = ColorToARGB(&HFFFF0000, 100) 
    
        stat = GdipCreateFromHDC(Picture1.hdc, graphics)
        stat = GdipSetSmoothingMode(graphics, SmoothingModeAntiAlias)
        stat = GdipCreatePen1(PenCol, 1, UnitPixel, penRed)
        
    End Sub
    
    
    
    Private Function ColorToARGB(Color As Long, ByVal Opacity As Long) As Long
        
        '- Helper function.
        '- Converts long colors to ARGB for use with GDI+
        If Opacity < 1 Then
            Exit Function
        ElseIf Opacity > 100 Then
            Opacity = 100
        End If
        '- Convert opacity to a 0 to 100 setting
        Opacity = (255 * Opacity) \ 100
        
        Color = ((Color And &HFF) * &H10000) Or _
        (((Color \ &H100) And &HFF) * &H100) Or ((Color \ &H10000) And &HFF)
        If Opacity > 127 Then
            Color = Color Or (&H80000000 Or ((Opacity - 128) * &H1000000))
        Else
            Color = (Opacity * &H1000000) Or Color
        End If
        ColorToARGB = Color
        
    End Function

    This is correct regarding clearing the drawing surface and should be all that is needed:

    Code:
    Public Sub DrawSomeLine(graphics As Long)
        
        Picture1.Cls
        
        stat = GdipDrawLine(graphics, penRed, 10, 10, 50, 30)
    
        Picture1.Refresh
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width