473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Example of dragging bitmaps

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
Nov 21 '05 #1
16 1643
Geoff,

Is this sample on MSDN not what you mean?

See Dragging a picture

http://msdn.microsoft.com/library/de...mpdragdrop.asp

I hope this helps?

Cor
Nov 21 '05 #2
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:ug**************@TK2MSFTNGP14.phx.gbl...
Geoff,

Is this sample on MSDN not what you mean?

See Dragging a picture

http://msdn.microsoft.com/library/de...mpdragdrop.asp

I hope this helps?

Cor

Nov 21 '05 #3
Geoff,

I have made once a sample of what is in my opinion what you ask (just to
show how it can be done you have to prevent moving from the window and
things like that)

http://groups-beta.google.com/group/...486e1e60f1956e

I hope this helps?

Cor
Nov 21 '05 #4
"Geoff Jones" <no********@email.com> schrieb:
Display a bitmap on a form which can then be moved about i.e. dragged,
using the left down button of the mouse.


Something like this is implemented in this sample:

<URL:http://dotnet.mvps.org/dotnet/samples/misc/XmlSerialization.zip>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
\\\
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
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Geoff Jones" <no********@email.com> wrote in message
news:41*********************@news.dial.pipex.com.. .
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

Nov 21 '05 #6
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
Nov 21 '05 #7
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:eR**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #8
Many thanks Mick and Herfried

Geoff

"Mick Doherty"
<EX***********@AND.REMOVE.SQUAREBRACKETS.[mdaudi100#ntlworld.com]> wrote in
message news:OW**************@TK2MSFTNGP12.phx.gbl...
\\\
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
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Geoff Jones" <no********@email.com> wrote in message
news:41*********************@news.dial.pipex.com.. .
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


Nov 21 '05 #9
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
Nov 21 '05 #10
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:OR****************@TK2MSFTNGP09.phx.gbl... 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

Nov 21 '05 #11
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
Nov 21 '05 #12
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
Nov 21 '05 #13
I see! Good idea

Geoff

"Cor Ligthert" <no************@planet.nl> wrote in message
news:OR****************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #14
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:eY**************@TK2MSFTNGP11.phx.gbl...
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

Nov 21 '05 #15
Jay,

Nice sample to use as usercontrol.

I saved it in my snippets with your name

Thanks,

Cor
Nov 21 '05 #16
Jay,

Nice sample to use as usercontrol.

I saved it in my snippets with your name

Thanks,

Cor
Nov 21 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Brad Smalling | last post by:
Does anyone know if there is a reason to prefer bitmaps over icons (or vice-versa) when populating image lists? I've always favored icons because of their built-in transparency (although I've...
1
by: Mark Evans | last post by:
I have a dialog box and on it I want to display a bitmap, which will change at various times during the program. My problem is that the bitmaps will not be the same each time. I want the user to...
0
by: Sin Jeong-hun | last post by:
If FlowLayoutPanel's AutoScroll is set to true, there appears a scroll bar on the right. Normally, users would expect the contents is scrolled while they are dragging the scroll bar. Well, it does,...
1
by: John | last post by:
I have 76 bitmaps (640 x 480) and need to combine them together to form a big one (about 3840 x 2880). Those 76 bitmaps overlap each other with a small portion. Is there any way to do it? Thanks.
1
by: Peter Stojkovic | last post by:
What is the correct way to move BITMAPS and drawings inside a windows-forms from one project to another project. The problem is the following row: Dim resources As...
2
by: Mike | last post by:
Hello everybody. I am drawing a country map that consists of 149 municipality bitmaps, each around 25 Kb. I draw it onto the in-memory bitmap, then draw it on the picture box. I use C++, but...
2
by: Tull Clancey | last post by:
Does anyone have, or can anyone suggest a URL for code to drag a control around a form at run time? VB.Net 2003. I have written stuff in VB6 to do this before, but a long time ago and I don't...
1
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi, I need to combine several bitmaps together to form a single bitmap. Can anyone show me some similar sample code? Thanks. AJ
1
by: nkumarin001 | last post by:
Hi, Can anyone help me in this matter:- When i was studying locally managed tablespaces i came across bitmaps that are used in locally managed tablespaces it stated that:- "Locally...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.