使用背景图像在面板周围移动面板

时间:2012-04-01 15:04:03

标签: windows vb.net

我正在尝试在VB.Net中构建一个Windows应用程序作为应用程序的一部分,我需要能够将标记放在图像上,但是我遇到了大量问题。

我有一个面板,我已将我想要放置标记的图像作为背景分配,然后我将自定义面板添加到面板中。

包含图像(标记)和标签(标记名称)的自定义面板,因为我只移动面板比移动标记图像和名称标签更容易。

我已尝试将事件处理程序分配给MouseDown,MouseUp和MouseMove到每个自定义面板,但每次我尝试移动面板时,它都遍布整个地方,并在任何地方留下黑盒子。

Dim image As New System.Drawing.Bitmap(/* Path */)
pnlPreviewPanel.BackgroundImageLayout = ImageLayout.Stretch
pnlPreviewPanel.BackgroundImage = image

pnlPreviewPanel.Controls.Clear()
For Each item As Marker In mMarkers
    Dim panel as New CustomPanel

    AddHandler panel, AddressOf DeviceMouseDown
    AddHandler panel.Controls(0).MouseDown, AddressOf DeviceMouseDown
    AddHandler panel.Controls(1).MouseDown, AddressOf DeviceMouseDown

    AddHandler panel.MouseUp, AddressOf DeviceMouseUp
    AddHandler panel.Controls(0).MouseUp, AddressOf DeviceMouseUp
    AddHandler panel.Controls(1).MouseUp, AddressOf DeviceMouseUp

    AddHandler panel.MouseMove, AddressOf DeviceMouseMove
    AddHandler panel.Controls(0).MouseMove, AddressOf DeviceMouseMove
    AddHandler panel.Controls(1).MouseMove, AddressOf DeviceMouseMove

    pnlPreviewPanel.Controls.Add(panel)
Next

Private Sub DeviceMouseDown(ByVal pSender As Object, ByVal pEventArgs As MouseEventArgs)
    Dim control As Control = pSender
    If Not control.GetType() = GetType(CustomPanel) Then
        control = control.Parent
    End If

    mSelectedPanel = CType(control, CustomPanel)
End Sub

Private Sub DeviceMouseUp(ByVal pSender As Object, ByVal epEventArgs As MouseEventArgs)
    If Not mSelectedDevicePanel Is Nothing Then
        mSelectedDevicePanel = Nothing
    End If
End Sub

Private Sub DeviceMouseMove(ByVal pSender As Object, ByVal pEventArgs As MouseEventArgs)
    If Not mSelectedDevicePanel Is Nothing Then
        Dim x As Integer = pEventArgs.Location.X - pnlPreviewPanel.Location.X
        Dim y As Integer = pEventArgs.Location.Y - pnlPreviewPanel.Location.Y

        mSelectedDevicePanel.Location = New Point(x, y)
    End If
End Sub

0 个答案:

没有答案