Solved

dragging a picturebox over another picturebox.

Posted on 2009-04-06
2
758 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

9 Experts available now in Live!

Get 1:1 Help Now