Question:
How would I create an Image at the mouse position when I click? (Visual Basic)?
GaminMagicMan
2009-09-01 03:05:44 UTC
Im making a very basic game where a crosshair follows the mouse (Which i've already accomplished) and to place a bullethole when I click. How do I do this?
Three answers:
?
2009-09-01 09:02:27 UTC
CODE DEMO:

To use this demo copt and past the following code into form1's code

place a picture box on the form (picturebox1) . Set the properties on this picture box



Visible = False

SizeMode = Autosize

Image = browse to your image (bullet hole)



Using only the form mouse down event would only place an image if you can click the form itself. But to plcae one bullet hole image ontop of another you need to use a second event handler. the mouse down for the image.



The problem here is the XY positions reported for these events are relative to their base objects. XY on the form vs XY of the image

If you look at the code you will see how to convert the image XY to a form XY



Also note the AddHandler function. This assigns the existing event handler for the hidden picture box to the new picture that is added to the form. this is how you can get the new image to respond when you click it.



Final point. You will need to make your bullet hole with a transparent backround. This is a detail that will polish your bullet hole effect but its not required to do for the purpose of this question.





Public Class Form1



Private Sub Form1_MouseDown(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.MouseEventArgs) _

Handles MyBase.MouseDown



Dim x, y As Integer

'Get XY position on the form

x = e.X

y = e.Y



PlaceNewImage(x, y)



End Sub





Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.MouseEventArgs) _

Handles PictureBox1.MouseDown



Dim x, y As Integer

Dim clickedImage As PictureBox



'Get XY position on the Image

x = e.X

y = e.Y

'Convert the Image XY position to a

'Form XY position

'

'expose methods and propeties of clicked image

'by copying the sender to a picturebox variable

'

clickedImage = sender

'extract the position of the image and modify the XY positions

'to crrespond to positions on the form

'

x = x + clickedImage.Left

y = y + clickedImage.Top

'Place a new image on the form using the modified XY position

PlaceNewImage(x, y)



End Sub



Private Sub PlaceNewImage(ByVal x As Integer, ByVal y As Integer)

'

Dim pb As New PictureBox



'get current mouse position



'Center picture about mouse position

pb.Top = y - (Me.PictureBox1.Height / 2)

pb.Left = x - (Me.PictureBox1.Width / 2)

'

'Copy the image from a hidden picture box

'or source file. Copying from a hidden

'picture box embeds the image in the

'program and prevents it being used by

'others or being changed.

'

pb.Image = Me.PictureBox1.Image

'Size the new picture box to the exact size of the picture

pb.SizeMode = PictureBoxSizeMode.AutoSize

'

'Assign the mouse down event handler for the new PB

AddHandler pb.MouseDown, AddressOf PictureBox1_MouseDown

'place the new picture box on the form by

'adding it to the controls collection of the form

Me.Controls.Add(pb)

pb.BringToFront() 'place new image on top of all others

'

'show the new picture

pb.Visible = True



End Sub



End Class
lockwood
2016-10-07 08:09:00 UTC
Any administration - like a textual content textile field, a button, or a picture - has a sources that as quickly as set to real is comparable to if the person had clicked on that administration along with his mouse. Command1.fee = real place this on your timer or notwithstanding different journey will set off the "fake mouse click".
RJ
2009-09-01 07:07:40 UTC
it is easy...just make a PictureBox and set it's picture property to what you want, then you can make it hidden!(set it's visible to false) now use this code in the MouseDown event of ur form:



Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Me.PaintPicture Picture1.Picture, X, Y

End Sub


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...