Example of dragging bitmaps

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hiya

Could anybody direct me to some examples of code to do the following:

Display a bitmap on a form which can then be moved about i.e. dragged, using
the left down button of the mouse.

Thanks in advance

Geoff
 
Hi Cor

Ah, not quite. What I was looking for was a way to move a bitmap image
around a form i.e. you can actually see it being moved.

An example of what I'm trying to accomplish would be moving a chess piece
i.e. if was writing a chess game, I would like to be able to move the pieces
on the screen.

Geoff
 
\\\
Private Dragging As Boolean = False
Private DragBitmap As Bitmap
Private DragBounds As Rectangle = New Rectangle(10, 10, 16, 16)
Private startpoint As Point = Point.Empty

Private Sub MainForm_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
e.Graphics.DrawImage(DragBitmap, DragBounds)
End Sub

Private Sub MainForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
DragBitmap = New Bitmap(16, 16)
Dim g As Graphics = Graphics.FromImage(DragBitmap)
g.FillEllipse(Brushes.Red, 0, 0, 16, 16)
g.Dispose()
End Sub

Private Sub MainForm_MouseDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
If e.Button = MouseButtons.Left = False Then Return
If DragBounds.Contains(e.X, e.Y) Then
Dragging = True
startpoint = New Point(e.X - DragBounds.X, e.Y - DragBounds.Y)
Dim CursorBounds As Rectangle = RectangleToScreen(ClientRectangle)
CursorBounds.Inflate(-startpoint.X, -startpoint.Y)
Cursor.Clip = CursorBounds
End If
End Sub

Private Sub MainForm_MouseUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseUp
Dragging = False
Cursor.Clip = Nothing
End Sub

Private Sub MainForm_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseMove
Dim DragPoint As Point = New Point(e.X, e.Y)
DragPoint.Offset(-startpoint.X, -startpoint.Y)
If Dragging Then DragBounds.Location = DragPoint
Invalidate()
End Sub
///
 
Geoff,

I forget to tell, did you know that buttons with a graph on it are very nice
for what you want to do.

Maybe you did know that already?

Cor
 
Thanks for the code Cor. Looks very impressive.

I'm afraid I don't understand what you mean by a button with a graph on it.
Could you give further details?

Geoff
 
Geoff,

A picture box does not have a click event.

So when you use for that a button, you have a click event, while a
chess-piece will be nicely displayed on that as well.

A label has as well a click as well. But no things as Button.performclick
and shadowing etc.

That is all, it was just an idea when you wrote chess-piece.

Cr
 
Cor,
A picture box does not have a click event.
Is a false statement!

PictureBox inherits from Control, Control has a Click event, ergo PictureBox
has a click event!

Add a PictureBox control to your favorite form, name it PictureBox1, add the
following code:

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles PictureBox1.Click
MessageBox.Show("PictureBox1_Click", Application.ProductName)
End Sub

Run the form, click the PictureBox, what do you see?

A label has as well a click as well. But no things as Button.performclick

To enable "PerformClick", you would need to inherit from PictureBox (or
Label) & implement the IButtonControl, something like:

Public Class PictureBoxEx
Inherits PictureBox
Implements IButtonControl

Private m_dialogResult As DialogResult

Public Property DialogResult() As DialogResult Implements
IButtonControl.DialogResult
Get
Return m_dialogResult
End Get
Set(ByVal value As DialogResult)
m_dialogResult = value
End Set
End Property

Public Sub NotifyDefault(ByVal value As Boolean) Implements
IButtonControl.NotifyDefault
' TODO: Handle the notification!
' Notifies a control that it is the default button
' so that its appearance and behavior is adjusted accordingly.
End Sub

Public Sub PerformClick() Implements IButtonControl.PerformClick
MyBase.OnClick(EventArgs.Empty)
End Sub

End Class

By implementing IButtonControl, a PictureBoxEx can be used as either
Form.AcceptButton or Form.CancelButton!
and shadowing etc.
You can use ControlPaint.DrawBorder or roll your own "shadowing" & border
effects...

Something like:
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
ControlPaint.DrawBorder3D(e.Graphics, Me.ClientRectangle)
End Sub


Hope this helps
Jay
 
Jay,

You are right it has a click event, in my thought that was the reason that
the button was better to use than the picturebox for this kind of solutions.

However it has to do with the click event. The picturebox cannot get focus
(in a simple way) and therefore it is in my opinion a little bit less usable
than a picturebox when you are using it for things as games (you cannot use
the enter key).

While when you use a button for that, it is easy to use.

That you inherit from control does not say everything, you can shadow
methods/properties and with that even when it is inherited is it possible
that a method/property is not functioning. In my opinion is that by instance
the text property from a picturebox.

I hope this helps,

Cor
 
Geoff,

Because of message from Jay about my mistake about the click event I know
what it was again.

With a pictureboxes you cannot use the keys to navigate, while that is
posible with a button, something that you would normally do playing by a
game.

Cor
 
Cor,
I agree if you want a button then start with a button. On the same token if
you want a picture box start with a picture box. If you want something that
resemples both, pick the one that is closer to what you really need, then
add the code to get the features you need. Of course starting with Control,
UserControl, ContainerControl, ScrollableControl, or another control instead
might be better still...

Remember you can use Control.SetStyle(ControlStyles.Selectable, True)
followed by Control.UpdateStyles will enable PictureBoxEx to get the
keyboard focus, you can then use the normal & advanced keyboard* overrides &
events to handle the keyboard while the PictureBox has the focus...

Something like:

Imports System.ComponentModel

Public Class PictureBoxEx
Inherits PictureBox
Implements IButtonControl

Private m_dialogResult As DialogResult

Public Sub New()
SetStyle(ControlStyles.Selectable, True)
UpdateStyles()
TabStop = True
End Sub

Public Property DialogResult() As DialogResult Implements
IButtonControl.DialogResult
Get
Return m_dialogResult
End Get
Set(ByVal value As DialogResult)
m_dialogResult = value
End Set
End Property

Public Sub NotifyDefault(ByVal value As Boolean) Implements
IButtonControl.NotifyDefault
' Notifies a control that it is the default button
' so that its appearance and behavior is adjusted accordingly.
End Sub

Public Sub PerformClick() Implements IButtonControl.PerformClick
MyBase.OnClick(EventArgs.Empty)
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
ControlPaint.DrawBorder3D(e.Graphics, Me.ClientRectangle)
End Sub

Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
MyBase.OnGotFocus(e)
Me.BackColor = SystemColors.Info
End Sub

Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
MyBase.OnLostFocus(e)
Me.BackColor = SystemColors.Control
End Sub

<Browsable(True), DefaultValue(True)> _
Public Shadows Property TabStop() As Boolean
Get
Return MyBase.TabStop
End Get
Set(ByVal value As Boolean)
MyBase.TabStop = value
End Set
End Property

<Browsable(True), DefaultValue(0)> _
Public Shadows Property TabIndex() As Integer
Get
Return MyBase.TabIndex
End Get
Set(ByVal value As Integer)
MyBase.TabIndex = value
End Set
End Property

End Class

Again put the PictureBoxEx on your form, with a Click event handler, tab to
it, press Enter, what happens? Note PictureBoxEx does not understand the
Space bar, which is normally the same as Enter on a button!

*advanced keyboard overrides include: IsInputChar, IsInputKey,
PreProcessMessage, ProcessCmdKey, ProcessDialogChar, ProcessDialogKey,
ProcesKeyEventArgs, ProcessKeyMessage, ProcessKeyPreview, and
ProcessMnemonic. Unfortunately I don't have any real good examples of using
these, other then they allow you to intercept the key significantly earlier
then the OnKey* methods...

Hope this helps
Jay
 
Jay,

Nice sample to use as usercontrol.

I saved it in my snippets with your name

Thanks,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Treeview Drag n Drop 1
Q: Transparent image on a form 40
Icon vs BitMap 2
WinForms Drag Drop with Custom Image/Cursor 0
Drag and drop problem 2
Merging Bitmaps 6
saving bitmap to file 1
Memorystream bytearray bitmap 4

Back
Top