Question How can i paint on the controls when the mouse is clicked on them?

NKN

Member
Joined
Dec 9, 2011
Messages
19
Programming Experience
Beginner
How can i paint on the controls when the mouse is clicked on them? I tried creating a handler for Ctrl.paint but it didn't work. I want the image to be drawn over the control instead of under, like bringing it to the front..


Private Sub AllCtrls()
        Dim ExceptedCtrl = {StartLabel}
        For Each Ctrl As Control In Me.Controls.OfType(Of Control).Except(ExceptedCtrl)
            AddHandler Ctrl.Click, AddressOf Ctrl_Click
        Next
    End Sub

 Private Sub Ctrl_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
       
            Start()


    End Sub


Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint        Static myImagePoints As New List(Of Point)
        Dim Intimg As New Bitmap(My.Resources.Image1, 42, 39)


        If DoPaint Then
            Dim CurrentPoint As New Point(CInt(Me.PointToClient(Cursor.Position).X - Intimg.Width / 2), CInt(Me.PointToClient(Cursor.Position).Y - Intimg.Height / 2))
            myImagePoints.Add(CurrentPoint)
            DoPaint = False
        End If


        For Each EnteredPoint As Point In myImagePoints
          
                    e.Graphics.DrawImage(Intimg, EnteredPoint)


        Next
        Intimg.Dispose()
        My.Resources.Image1.Dispose()
    End Sub


End Class
 
Last edited by a moderator:
The code I provided was just an example. Just like all examples, you need to take the principle it demonstrates and apply it to your own set of circumstances. My example handles the Paint event of the form so it paints on the form. If you want to paint on a different control then handle the Paint event of a different control.
 
My question is how can i do a paint event for AllCtrls?

VB.NET:
    Private Sub AllCtrls()
        Dim ExceptedCtrl = {StartLabel}
        For Each Ctrl As Control In Me.Controls.OfType(Of Control).Except(ExceptedCtrl)
            AddHandler Ctrl.Click, AddressOf Ctrl_Click
        Next
    End Sub
 
Here's a modification of my original code:
Private picture As Image = My.Resources.MyImage
Private imageLocations As New List(Of Point)
 
Private Sub Form1_MouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
    'Get the point at which the user clicked.
    'Dim p = e.Location
    Dim p = Control.MousePosition
 
    imageLocations.Add(p)
 
    'Repaint the area that the new picture will be drawn.
    'Invalidate(New Rectangle(p, picture.Size))
    'Update()
    Refresh()
End Sub
 
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    Dim sendingControl = DirectCast(sender, Control)

    'Draw the picture at each clicked point.
    For Each imageLocation In imageLocations
        'e.Graphics.DrawImage(picture, imageLocation)
        e.Graphics.DrawImage(picture, sendingControl.PointToClient(imageLocation))
    Next
End Sub
If you attach those MouseClick and Paint handlers to the events for each control you want to draw on then you'll get the desired behaviour. It will draw the whole list of Images onto every control. The locations are stored in screen coordinates and then translated separately for each control. Each set of drawings will line up correctly.
 
What i'm trying to do is when i click on the form the image is painted at this location (X- that image's width, Y- that image's height) and when i click on a control this image is painted on that control. The problem is when i click on the border of a control the image is only painted on the control and part of it isn't shown on the form. and when i click on the form near a control i want the part of the image beneath that control to overlap that control.
 
Back
Top