拖动标签,使其背后留下黑色痕迹

时间:2014-05-06 18:42:02

标签: vb.net picturebox

我正在开发一个项目,我在一个图片框中有一个标签,我需要拖动它,而在拖动时它会在图片框上留下一条黑色的痕迹。我很难过如何实现这一目标。标签为15x15,使用鼠标和mousedown,mousemove和mouseup事件移动。在鼠标移动期间,标签需要在标签覆盖的任何地方绘制黑色。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

您需要覆盖Form的OnPaint()方法,然后跟踪鼠标位置与拖动origniation的偏移量。这基本上是实现动画效果,因此您需要在Label的拖动开始时触发重绘计时器,在前一个拖动坐标处绘制“尾部”,重复拖动操作将鼠标坐标移动到某个阈值或特定速度。这将需要一些游戏来使效果恰到好处,但它很容易实现。

从这里开始:http://msdn.microsoft.com/en-us/library/3e40ahaz(v=vs.110).aspx

答案 1 :(得分:0)

假设您在picturebox中有背景图片。创建一个picturebox尺寸的位图:

Private pctBoxImage As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    pctBoxImage = New Bitmap(PictureBox1.Width, PictureBox1.Height, Drawing.Imaging.PixelFormat.Format24bppRgb)

    pctBoxImage = CType(PictureBox1.BackgroundImage, Bitmap)
End Sub

如果你没有背景图片只有一些color

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    pctBoxImage = New Bitmap(PictureBox1.Width, PictureBox1.Height, Drawing.Imaging.PixelFormat.Format24bppRgb)

    Dim objGraphics As Graphics = Graphics.FromImage(pctBoxImage)
    objGraphics.Clear(PictureBox1.BackColor)
    objGraphics.Dispose()
End Sub

什么时候移动标签:

Dim pt as Point
'pt = PictureBox1.PointToClient(Cursor.Position)
pt = Label1.PointToScreen(New Point(0, 0))
pt = PictureBox1.PointToClient(pt)

Dim objGraphics As Graphics = Graphics.FromImage(pctBoxImage)
objGraphics.FillRectangle(Brushes.Black, pt.X, pt.Y, 15, 15)
objGraphics.Dispose()

PictureBox1.BackgroundImage = pctBoxImage

瓦尔特