How to update a region using ExcludeClip

23 views Asked by At

I'm trying to implement a solution that @RezaAghaei provided in this post Creating Custom Picturebox with Draggable and Resizable Selection Window.

The process for creating the control has already been done and everything works as Reza Aghaei puts it, however my difficulty is in being able to update the region that is excluded from the picturebox, exactly in this part of the code:

Private Sub pb_screen_Paint(sender As Object, e As PaintEventArgs) Handles pb_screen.Paint
    e.Graphics.ExcludeClip(pb_screen.Controls(0).Bounds)
    Using b = New SolidBrush(Color.FromArgb(100, Color.Black))
        e.Graphics.FillRectangle(b, pb_screen.ClientRectangle)
    End Using

    If freehand Then
        DrawShapes(e.Graphics)
    End If
End Sub

and here is the part of my code where I apply MouseMove to the picturebox to add the control:

Private Sub pb_screen_MouseMove(sender As Object, e As MouseEventArgs) Handles pb_screen.MouseMove
    If IsMouseDown = True Then
        EndLocation = e.Location
        GetRectangle()
        Application.DoEvents()
        pb_screen.Controls(0).Size = New Size(rect.Size)
        pb_screen.Controls(0)..Location = New Point((StartLocation.X), (StartLocation.Y))
        lbl_position.Text = pb_screen.Controls(0).Width & "x" & pb_screen.Controls(0).Height
        pnl_menu.Location = New Point(EndLocation.X + 5, EndLocation.Y - pnl_menu.Height)
        Application.DoEvents()

        If freehand Then
            linefreehand.AddLine(e.X, e.Y, e.X, e.Y)
        End If
        pb_screen.Controls(0).Invalidate()
        pb_screen.Invalidate()
    End If
End Sub

The question is how can I draw in picturebox and update the control at the same time? If I remove the ExcludeClip from the Paint event, the control is updated whenever the picturebox is updated, but the "fancy effect of filling outside the frame with semi-transparent color" is not applied.

These are some example screenshots of my problem

Screenshot with ExcludClip: screenshot with ExcludClip

screenshot without ExcludClip: screenshot without ExcludClip

Any suggestions to solve it????

0

There are 0 answers