Solved

dragging a picturebox over another picturebox.

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

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 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