Solved

dragging a picturebox over another picturebox.

Posted on 2009-04-06
2
759 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now