Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Onpaint Dynamically

charchabilcharchabil Posts: 1Member
hey guys ,could you plz help me to do a FUNCTION1 using "Onpaint" as Event and taking "color","X_location","Y_location" as parameters.
This function1 might be called by a function2 and drawing different shape(ellipse or rectangle) with their specific color.

Comments

  • DrMartenDrMarten Posts: 748Member
    : hey guys ,could you plz help me to do a FUNCTION1 using "Onpaint" as Event and taking "color","X_location","Y_location" as parameters.
    : This function1 might be called by a function2 and drawing different shape(ellipse or rectangle) with their specific color.

    ______________________________________________________________________

    Hi,

    Take a look at.>>
    [code]
    '[Visual Basic]
    Private picture As Image
    Private pictureLocation As Point

    Public Sub New()
    ' Enable drag-and-drop operations.
    Me.AllowDrop = True
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    MyBase.OnPaint(e)

    ' If there is an image and it has a location,
    ' paint it when the Form is repainted.
    If Not (Me.picture Is Nothing) And _
    Not (Me.pictureLocation.Equals(Point.Empty)) Then
    e.Graphics.DrawImage(Me.picture, Me.pictureLocation)
    End If
    End Sub

    Private Sub Form1_DragDrop(ByVal sender As Object, _
    ByVal e As DragEventArgs) Handles MyBase.DragDrop
    ' Handle FileDrop data.
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    ' Assign the file names to a string array, in
    ' case the user has selected multiple files.
    Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
    Try
    ' Assign the first image to the 'picture' variable.
    Me.picture = Image.FromFile(files(0))
    ' Set the picture location equal to the drop point.
    Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    Return
    End Try
    End If

    ' Handle Bitmap data.
    If e.Data.GetDataPresent(DataFormats.Bitmap) Then
    Try
    ' Create an Image and assign it to the picture variable.
    Me.picture = CType(e.Data.GetData(DataFormats.Bitmap), Image)
    ' Set the picture location equal to the drop point.
    Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    Return
    End Try
    End If

    ' Force the form to be redrawn with the image.
    Me.Invalidate()
    End Sub

    Private Sub Form1_DragEnter(ByVal sender As Object, _
    ByVal e As DragEventArgs) Handles MyBase.DragEnter
    ' If the data is a file or a bitmap, display the copy cursor.
    If e.Data.GetDataPresent(DataFormats.Bitmap) _
    Or e.Data.GetDataPresent(DataFormats.FileDrop) Then
    e.Effect = DragDropEffects.Copy
    Else
    e.Effect = DragDropEffects.None
    End If
    End Sub

    [/code]

    You could get a Function or another Sub to call the over-ridden
    2nd Sub in the code above with

    OnPaint()

    You may need to pass it PaintEventArguments (PaintEventArgs)
    as the ByVal section in the Sub is NOT optional, i wouldn't know how though.

    You may not need to as i've not tried to run this code and the OnPaint event is over-ridden.

    It looks like it is calling OnPaint whenever there is something to actually paint.

    Hope this helps? :-)



    Regards,

    Dr M.

Sign In or Register to comment.