Solved

dragging a picturebox over another picturebox.

Posted on 2009-04-06
2
761 Views
Last Modified: 2013-11-26
Hi,

I am developing a VC++ application and I am trying to make a circle image over a map image to be draggable by mouse. I did this by first creating a picturebox which has its image set to a map and the circle as anothe picturebox  (PNG) which has transparency.

I am setting the first picturebox as the parent of the second picturebox.

I am making the image draggable by using the mousedown events and mouse move events to relocate the circle picturebox to the mouse pointer position.

The problem is, when I do this, the transparent areas of the circle image look messy (flickering) and the circle image is looking strange and not moving smoothly. Am I doing anything wrong? is there a better way to achieve this in VC++ dot net?

Regards,
0
Comment
Question by:gghaleb
2 Comments
 
LVL 12

Accepted Solution

by:
omegaomega earned 500 total points
ID: 24089507
Hello, gghaleb,

You might find this smoother if you don't bother with the second PictureBox but instead just draw the circle during the map PictureBox's Paint event.  The snippet shows a simple example.  (It's in VB, but you probably won't have any trouble converting it.)   The MouseMove and MouseUp events simply request repainting of the PictureBox.  The Paint event is then used to draw the circle.

Cheers,
Randy

Public Class Form1
 
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If (e.Button = Windows.Forms.MouseButtons.Left) Then
            PictureBox1.Invalidate()
        End If
    End Sub
 
    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        PictureBox1.Invalidate()
    End Sub
 
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If (Control.MouseButtons = Windows.Forms.MouseButtons.Left) Then
            Const Radius As Integer = 30
            Dim Centre As Point = PictureBox1.PointToClient(Control.MousePosition)
            Dim CircleFrame As New Rectangle(Centre.X - Radius, Centre.Y - Radius, 2 * Radius, 2 * Radius)
            e.Graphics.DrawEllipse(Pens.Black, CircleFrame)
        End If
    End Sub
 
End Class

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question