Place circle at mouse position in picture box - vb.net

I have done a lot of searching and cannot find the help that I need.
I need to be able place some circles with diameter of 140 pixels over some sections of an image in a picturebox. The circles should appear when the mouse is clicked on the image. I need the centre of the circle to be placed at the position of the mouse when it is clicked.The user should also be able to drag the circle to a different position once it is placed on the image.
Can anyone give me some guidance on how this is done?
Thanks in advance!

This might start you off. Draws a simple red circle on PictureBox.Click
Private Sub PictureBox1_Click(sender As System.Object, e As MouseEventArgs) Handles PictureBox1.Click
Dim myBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = sender.CreateGraphics()
formGraphics.FillEllipse(myBrush, New Rectangle(e.X / 2, e.Y / 2, 70 , 70))
myBrush.Dispose()
formGraphics.Dispose()
End Sub
EDIT
Looks exaclty like what you are looking for
http://www.dreamincode.net/forums/topic/59049-simple-drawing-selection-shape-or-rubberband-shape/

My solution: `
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
'start creating circle when the mouse is clicked
PictureBox1.Refresh() 'erases previous rectangle
Xstart = e.X
Ystart = e.Y
bRB = True
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
'drag the circle over the seclected area
If bRB Then
PictureBox1.Refresh() 'erases previous rectangle
Select Case e.X
Case Is < 0
RBRectangle.X = 0
' RBRectangle.Width = Xstart
RBRectangle.Width = 140
Case 0 To Xstart
RBRectangle.X = e.X
'RBRectangle.Width = Xstart - e.X
RBRectangle.Width = 140
Case Xstart To PBWidth
RBRectangle.X = Xstart
'RBRectangle.Width = e.X - Xstart
RBRectangle.Width = 140
Case Is > PBWidth
RBRectangle.X = Xstart
'RBRectangle.Width = PBWidth - Xstart
RBRectangle.Width = 140
End Select
Select Case e.Y
Case Is < 0
RBRectangle.Y = 0
'RBRectangle.Height = Ystart
RBRectangle.Height = 140
Case 0 To Ystart
RBRectangle.Y = e.Y
'RBRectangle.Height = Ystart - e.Y
RBRectangle.Height = 140
Case Ystart To PBHeight
RBRectangle.Y = Ystart
'RBRectangle.Height = e.Y - Ystart
RBRectangle.Height = 140
Case Is > PBHeight
RBRectangle.Y = Ystart
'RBRectangle.Height = PBHeight - Ystart
RBRectangle.Height = 140
End Select
PictureBox1.CreateGraphics.DrawEllipse(RBPen, RBRectangle)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
'show the area in the rectangle as a new image
Dim CroppedBm As Bitmap
If bRB Then
bRB = False
CroppedBm = New Bitmap(RBRectangle.Width, _
RBRectangle.Height)
End If
End Sub

Try this:
Dim g As Graphics
Dim md As Boolean
Private Sub Form1_Load() Handles MyBase.Load
g = PictureBox1.CreateGraphics
End Sub
Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
PictureBox1.Refresh()
g.DrawEllipse(Pens.Red, New Rectangle(e.X - 35, e.Y - 35, 70, 70))
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
md = True
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If md Then
PictureBox1.Refresh()
g.DrawEllipse(Pens.Red, New Rectangle(e.X - 35, e.Y - 35, 70, 70))
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
md = False
End Sub

Related

vb.net draw brush picturebox location uncorrect place of mouse cursor

I draw brush in picturebox but the location of the drawing appears away from the place of mouse cursor
and it always appear on top of the picture
Note This happen when sizemode in picture is set to StretchImage
Sub drawBrush(ByVal e As System.Windows.Forms.MouseEventArgs)
Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
Dim x, y As Integer
x = e.Location.X
y = e.Location.Y
Label1.Text = x.ToString
Label2.Text = y.ToString
Width = 2
If x = 0 Then
x = y
End If
Dim brush As SolidBrush = New SolidBrush(color)
Dim pen1 As Pen = New Pen(color, 4)
g.DrawRectangle(pen1, e.X, e.Y, 10, 10)
g.FillRectangle(brush, e.X, e.Y, 10, 10)
End Using
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If draw Then
drawBrush(e)
End If
end Sub
When using PictureBoxSizeMode.StretchImage, that only affects what you see on the screen. The Image object doesn't change. Let's say that you have an Image that is 10x10 and a PictureBox that is 100x100. If you put the mouse pointer in the centre of the control then the location will be (50, 50). If the Image is only 10x10, how could (50, 50) possibly be used for a point in the Image? You would need to do some arithmetic to scale your mouse coordinates based on the scaling of the Image. If the Image is stretched by N times in a direction then you have to divide the mouse coordinate by N in that direction to map to the corresponding point on the actual Image:
finalValue = imageDimension * initialValue / controlDimension
That's going to give you a Double value, so you need to decide what you want to do with that. You might convert it to a Single and then use the overloads of DrawRectangle and FillRectangle that accept Single values.
I have to use the stretched image after being stretched
using PictureBox1.CreateGraphics.DrawRectangle
Private draw As Boolean = False
Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
draw = True
End Sub
Sub drawBrush(ByVal e As System.Windows.Forms.MouseEventArgs)
Label1.Text = CStr(e.X)
Label2.Text = CStr(e.Y)
PictureBox1.CreateGraphics.DrawRectangle(Pens.Beige, e.X, e.Y, 10, 10)
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If draw Then
drawBrush(e)
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
draw = False
End Sub

Draw Rectangle over PictureBox

The next code lets you draw Rectangles in the Form with mouse clics.
Why not, or how can be draw over a PictureBox?
Public Class Form1
Dim SelectRect As Rectangle = New Rectangle()
Dim ps As Point = New Point()
Dim pe As Point = New Point()
This catch the first click, starting point or corner of the rectangle
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
SelectRect.Width = 0
SelectRect.Height = 0
SelectRect.X = e.X
SelectRect.Y = e.Y
ps.X = e.X
ps.Y = e.Y
pe = ps
End Sub
This part determine the width and height of the rectangle:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If (e.Button = MouseButtons.Left) Then
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
SelectRect.Width = e.X - SelectRect.X
SelectRect.Height = e.Y - SelectRect.Y
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
End If
End Sub
This part determine the last coordinate, the second corner of the rectangle:
Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Dim g As Graphics = Me.CreateGraphics()
Dim p As Pen = New Pen(Color.Blue, 2)
ControlPaint.DrawReversibleFrame(Me.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed)
g.DrawRectangle(p, SelectRect)
g.Dispose()
End Sub
End Class
Your code uses a control (a Form in this case) mouse events to enable the drawing of rectangular shapes, with the help of guidelines, provided by Control.DrawReversibleFrame().
You just have to define the same events of a different, drawable, control - like a PictureBox - and repeat, more or less, the same procedure (after a cleanup).
As many have stated, here and before, use the Graphics object that
the Paint event kindly offers, so that your drawing will persist.
The Graphics object you get from Control.CreateGraphics() is not
persistent, and it can be erase/clipped when you don't want to.
Use it only if that is really what you have planned to do for the
reasons you know.
I've adden an event handler that checks if Control Key is pressed.
If Control is pressed, you add a rectangle, if not, only one rectangle is drawn.
I've also included, as an example, a line of code that fills the rectangle. I think it's interesting, because you have to control the size of the invalidated Region.
Comment out these lines of code to draw just the frame:
SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
e.Graphics.FillRectangle(_brush, SelectRect)
Dim SelectRect As Rectangle = New Rectangle()
Dim _pen As Pen = New Pen(Color.Green, 4)
Dim _brush As SolidBrush = New SolidBrush(Color.Orange)
Dim _ControlPressed As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
_ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
_ControlPressed = (e.Modifiers And Keys.Control) = Keys.Control
End Sub
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
SelectRect.Location = e.Location
SelectRect.Size = New Size(0, 0)
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If (e.Button = MouseButtons.Left) Then
ControlPaint.DrawReversibleFrame(PictureBox1.RectangleToScreen(SelectRect), PictureBox1.BackColor, FrameStyle.Dashed)
SelectRect.Width = e.X - SelectRect.X
SelectRect.Height = e.Y - SelectRect.Y
ControlPaint.DrawReversibleFrame(PictureBox1.RectangleToScreen(SelectRect), PictureBox1.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
If (e.Y < SelectRect.Y) Then
SelectRect.Location = If(SelectRect.Location.X > e.X,
New Point(e.X, e.Y), New Point(SelectRect.X, e.Y))
SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
Else
If SelectRect.Location.X > SelectRect.Right Then
SelectRect.Location = New Point(e.X, SelectRect.Y)
SelectRect.Size = New Size(Math.Abs(SelectRect.Width), Math.Abs(SelectRect.Height))
End If
End If
If _ControlPressed Then
Dim _InflatedRect As Rectangle = New Rectangle(SelectRect.Location, SelectRect.Size)
_InflatedRect.Inflate(CInt(_pen.Width / 2), CInt(_pen.Width / 2))
PictureBox1.Invalidate(_InflatedRect)
Else
PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
'Draw the outer rectangle with the color of _pen
e.Graphics.DrawRectangle(_pen, SelectRect)
'Fill the rectangle with the color of _brush
'It's half Pen.Width smaller so it doesn't erase the contour
SelectRect.Inflate(CInt(-_pen.Width / 2), CInt(-_pen.Width / 2))
e.Graphics.FillRectangle(_brush, SelectRect)
End Sub

Creating Dynamic Controls in VB and having them pre-programmed in different events

I have a project that can create a picturebox control but I want every picturebox the user creates to have events already set in place such as the mouse down and mouse up events. But since the control hasnt been created yet, I can't refer to it in the code without getting an error and the form not being able to load because of it. In other words, after the user creates a picturebox, they can move the picturebox around the screen and draw on it. Then they can create another picturebox and move it around and draw on it as well and arrange the pictureboxes as they please. Any ideas? Thanks.
here is my code:
Private Sub AddCanvasToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddCanvasToolStripMenuItem.Click
Dim canvas As New PictureBox
Dim i As Integer = 0
i = i + 1
canvas.Name = "canvas"
canvas.BackColor = Color.White
canvas.BorderStyle = BorderStyle.FixedSingle
canvas.Image = Nothing
canvas.Height = 200
canvas.Width = 200
AddHandler canvas.MouseDown, AddressOf PictureBox1_MouseDown
AddHandler canvas.MouseMove, AddressOf PictureBox1_MouseMove
canvas.Top = Panel2.Bottom
canvas.Left = Panel1.Right
Controls.Add(canvas)
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If RadioButton1.Checked = True Then
xpos = Cursor.Position.X - PictureBox1.Location.X
ypos = Cursor.Position.Y - PictureBox1.Location.Y
End If
If RadioButton2.Checked = True Then
down = True
If down = True Then
PictureBox1.CreateGraphics.FillEllipse(mybrush, e.X, e.Y, 2, 2)
End If
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If RadioButton1.Checked = True Then
If e.Button = Windows.Forms.MouseButtons.Left Then
pos = MousePosition
pos.X = pos.X - xpos
pos.Y = pos.Y - ypos
PictureBox1.Location = pos
End If
End If
If down = True Then
PictureBox1.CreateGraphics.FillEllipse(mybrush, e.X, e.Y, 2, 2)
End If
End Sub
But this only makes what I want to happen to canvas happen to picturebox1.
I dont even want picturebox1 to exist in the first place. I want them to create a new picturebox out of nowhere with events already programmed into it. So the user can create a new picturebox and then move it and draw on it.
Create events dynamically too, Like this:
Private Sub AddCanvasToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddCanvasToolStripMenuItem.Click
Dim canvas As New PictureBox
Dim i As Integer = 0
i = i + 1
canvas.Name = "canvas"
canvas.BackColor = Color.White
canvas.BorderStyle = BorderStyle.FixedSingle
canvas.Image = Nothing
canvas.Height = 200
canvas.Width = 200
AddHandler canvas.MouseDown, AddressOf pic_MouseDown
canvas.Top = Panel2.Bottom
canvas.Left = Panel1.Right
Controls.Add(canvas)
End Sub
Private Sub pic_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
'Do Something
End Sub
The above is perfect, but just to expand a little bit more:
Private Sub btnAddPictureBox_Click(sender As Object, e As EventArgs) Handles btnAddPictureBox.Click
Dim newPicBox As New PictureBox
Me.Controls.Add(newPicBox)
newPicBox.Location = New Point(50, 50)
newPicBox.Height = 100
newPicBox.Width = 100
newPicBox.BackColor = Color.White
newPicBox.BorderStyle = BorderStyle.FixedSingle
AddHandler newPicBox.MouseClick, AddressOf PictureBoxMouseClick
End Sub
Private Sub PictureBoxMouseClick(sender As Object, e As MouseEventArgs)
'Access the control the raised the event
'In this case we are changing the background colour to red
DirectCast(sender, PictureBox).BackColor = Color.Red
End Sub

How to draw a rectangle over an image and save it in VB.Net

I have used the following code to draw a rectangle on the image buI i an not able to save the image with the highlighted rectangle. Only the original image is saved not the image with the rectangle. Please help me with this.
Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
IsMouseDown = True
SelectedObjPoint = New Point(e.X, e.Y)
End If
End Sub
Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
If IsMouseDown = True Then
If e.X < SelectionBoxObj.X Then
SelectionBoxObj.X = e.X
SelectionBoxObj.Width = SelectedObjPoint.X - e.X
Else
SelectionBoxObj.X = SelectedObjPoint.X
SelectionBoxObj.Width = e.X - SelectedObjPoint.X
End If
If e.Y < SelectedObjPoint.Y Then
SelectionBoxObj.Y = e.Y
SelectionBoxObj.Height = SelectedObjPoint.Y - e.Y
Else
SelectionBoxObj.Y = SelectedObjPoint.Y
SelectionBoxObj.Height = e.Y - SelectedObjPoint.Y
End If
Me.Refresh()
End If
End Sub
Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
IsMouseDown = False
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
If SelectionBoxObj.Width > 0 And SelectionBoxObj.Height > 0 Then
Dim oGradientBrush As Brush = New Drawing.Drawing2D.LinearGradientBrush(SelectionBoxObj.RectangleF, SelectionBoxObj.FillColor, SelectionBoxObj.FillColor, Drawing.Drawing2D.LinearGradientMode.Vertical)
e.Graphics.FillRectangle(oGradientBrush, SelectionBoxObj.RectangleF)
Dim TempPen = New Pen(SelectionBoxObj.BorderLineColor, SelectionBoxObj.BorderLineWidth)
TempPen.DashStyle = SelectionBoxObj.BorderLineType
e.Graphics.DrawRectangle(TempPen, SelectionBoxObj.RectangleF.X, SelectionBoxObj.RectangleF.Y, SelectionBoxObj.RectangleF.Width, SelectionBoxObj.RectangleF.Height)
End If
End Sub
Private Sub Highlight_Ok_Click(sender As Object, e As EventArgs) Handles Highlight_Ok.Click
Dim datestring As String = Date.Now.ToString("yyyyMMddmmss")
Dim path As String = "D:\capture screenshot temp"
Dim currentsavepath As String = String.Format("{0}\capture_{1}.png", path, datestring)
PictureBox1.Image.Save(currentsavepath, Imaging.ImageFormat.Bmp)
End Sub
The rectangle you are drawing in the PictureBox is a temporary drawing. It's showing the current image with the rectangle drawn on top of it. But when you are saving the image, the rectangle isn't a part of the image.
You can remedy your situation by moving your drawing routine into a procedure that will draw the result in a graphic object you pass to it:
Private Sub DrawHighlight(g As Graphics)
If SelectionBoxObj.Width > 0 And SelectionBoxObj.Height > 0 Then
Using br As New SolidBrush(SelectionBoxObj.FillColor)
g.FillRectangle(br, SelectionBoxObj.RectangleF)
End Using
Using p As New Pen(SelectionBoxObj.BorderLineColor, SelectionBoxObj.BorderLineWidth)
g.DrawRectangle(p, SelectionBoxObj.RectangleF)
End Using
End If
End Sub
Now you can update the screen easily:
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
DrawHighlight(e.Graphics)
End Sub
and you can save the final results:
Using bmp As New Bitmap(PictureBox1.Image)
Using g As Graphics = Graphics.FromImage(bmp)
DrawHighlight(g)
End Using
bmp.Save(currentsavepath, Imaging.ImageFormat.Bmp)
End Using

Form with custom title bar & formborderstyle = None remains draggable when maximized

I used this code to maximize and restore my custom form. But when the form is maximized, it still remains draggable, I use a timer to drag the form.
Private Sub btnMaximize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximize.Click, lblTitle.MouseDoubleClick
Dim maximizeHeight As Integer = Screen.PrimaryScreen.WorkingArea.Height
Dim maximizeWidth As Integer = Screen.PrimaryScreen.WorkingArea.Width
Dim maximizeLocation As Point = New Point(0, 0)
Dim fullscreen As Boolean = False
If Me.Height = maximizeHeight Or Me.Width = maximizeWidth Or Me.Location = maximizeLocation Then
fullscreen = True
Else
fullscreen = False
End If
If fullscreen = True Then
Me.Size = New Size(1000, 500)
Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2
ElseIf fullscreen = False Then
Me.Location = New Point(0, 0)
Me.Size = New Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
End If
End Sub
Private Sub pnlBar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseDown
MoveTmr.Start()
refpositions()
End Sub
Private Sub MoveTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MoveTmr.Tick
Me.Location = oloc - ocur + System.Windows.Forms.Cursor.Position
refpositions()
End Sub
Private Sub pnlBar_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTitle.MouseUp
MoveTmr.Stop()
refpositions()
End Sub
Private Sub RszTmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles RszTmr.Tick
Me.Size = appSize - curPos + Cursor.Position
refpositions()
End Sub
make:
Dim fullscreen As Boolean = False
a class variable.
Then edit this code to fit your variables:
Private Sub Mover_Tick(sender As System.Object, e As System.EventArgs) Handles Mover.Tick
If fullscreen = false Then
Dim pt As New Point((Me.Location.X + (MousePosition.X - mPosX)), (Me.Location.Y + (MousePosition.Y - mPosY)))
Me.Location = pt
mPosX = MousePosition.X
mPosY = MousePosition.Y
End If
End Sub
EDIT:
Also implement this:
Private Sub Title_StartDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseDown
mPosX = MousePosition.X
mPosY = MousePosition.Y
If e.Button = Windows.Forms.MouseButtons.Left Then
Mover.Start()
End If
End Sub
Private Sub Title_StopDrag(sender As System.Object, e As MouseEventArgs) Handles Title.MouseUp
Mover.Stop()
End Sub
also you may want to make it much simpler by saying
Me.WindowState = FormWindowState.Maximized
I use the Mousedown, Mouseup and Mousemove events to move my forms.
Public Class Form1
Private Is_Dragged As Boolean = False
Private M_DownX As Integer
Private M_DownY As Integer
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
If e.Button = MouseButtons.Left Then
Is_Dragged = True
M_DownX = e.X
M_DownY = e.Y
End If
End Sub
Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
If e.Button = MouseButtons.Left Then
Is_Dragged = False
End If
End Sub
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If Is_Dragged Then
Dim tmp_pnt As Point = New Point()
tmp_pnt.X = Me.Location.X + (e.X - M_DownX)
tmp_pnt.Y = Me.Location.Y + (e.Y - M_DownY)
Me.Location = tmp_pnt
tmp_pnt = Nothing
End If
End Sub
End Class
and to maximize my form.
Private Sub Btn_Main_Max_Click(sender As Object, e As EventArgs) Handles Btn_Main_Max.Click
Static IsAlreadyResized As Boolean
If Not IsAlreadyResized Then
Me.WindowState = FormWindowState.Maximized
IsAlreadyResized = True
Exit Sub
End If
If IsAlreadyResized Then
Me.WindowState = FormWindowState.Normal
IsAlreadyResized = False
End If
End Sub
You can create a panel in code or visually and place dock.top then you can put a label on the side or in the middle and a picturebox on the left side of the window create the buttons to close minimize and maximize or others and add the friend code above to move the form by the titl bar as in windows