vb.net add Aero Snap property in my borderless form - vb.net

I am trying to make a borderless form' It works fine,
but I can't add "Aero Snap" property for it.
I have tried different methods to make a borderless form, but they are not resizing smoothly.
That method is only best way for smooth resize.
I want it maximize when I drag it to the upper side of screen.
How do I do that?
Here is my whole code.
Imports System.Runtime.InteropServices
Public Class Form2
<DllImport("user32.dll")>
Public Shared Function ReleaseCapture() As Boolean
End Function
<DllImport("user32.dll")>
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
Private Const HTCAPTION As Integer = 2
Private Const WM_NCLBUTTONDOWN As Integer = &HA1
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.Style = cp.Style Or &H20000 '<--- Minimize borderless form from taskbar
Return cp
End Get
End Property
Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
If locked = True Then
locked = False
Exit Sub
End If
If direction_ = ResizeDirection.None Then
ElseIf direction_ = ResizeDirection.title Then
MoveForm()
Else
ResizeForm()
End If
End Sub
Private Sub MoveForm()
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
Invalidate()
End Sub
Private Sub ResizeForm()
' mouse_down = True
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, direction_, 0)
End Sub
Dim frame_width As Integer = 3
Dim title_width As Integer = 31
Public Enum ResizeDirection
None = 0
title = 1
Left = 10
TopLeft = 13
Top = 12
TopRight = 14
Right = 11
BottomRight = 17
Bottom = 15
BottomLeft = 16
End Enum
Dim direction_ As ResizeDirection
Dim locked As Boolean = False
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If Me.WindowState = FormWindowState.Maximized Then
If e.Y < title_width Then
direction_ = ResizeDirection.title
Else
direction_ = ResizeDirection.None
End If
Cursor = Cursors.Default
Exit Sub
End If
If e.Y < frame_width Then
If e.X < frame_width Then
Cursor = Cursors.SizeNWSE
direction_ = ResizeDirection.TopLeft
ElseIf e.X > Me.Width - frame_width Then
Cursor = Cursors.SizeNESW
direction_ = ResizeDirection.TopRight
Else
Cursor = Cursors.SizeNS
direction_ = ResizeDirection.Top
End If
ElseIf e.Y > Me.Height - frame_width Then
If e.X < frame_width Then
Cursor = Cursors.SizeNESW
direction_ = ResizeDirection.BottomLeft
ElseIf e.X > Me.Width - frame_width Then
Cursor = Cursors.SizeNWSE
direction_ = ResizeDirection.BottomRight
Else
Cursor = Cursors.SizeNS
direction_ = ResizeDirection.Bottom
End If
ElseIf e.X < frame_width Then
Cursor = Cursors.SizeWE
direction_ = ResizeDirection.Left
ElseIf e.X > Me.Width - frame_width Then
Cursor = Cursors.SizeWE
direction_ = ResizeDirection.Right
Else
If e.Y < title_width Then
direction_ = ResizeDirection.title
Else
direction_ = ResizeDirection.None
End If
Cursor = Cursors.Default
End If
End Sub
Private Sub BTN_MINIMIZE_Click(sender As Object, e As EventArgs) Handles BTN_MINIMIZE.Click
Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub BTN_MAXIMIZE_Click(sender As Object, e As EventArgs) Handles BTN_MAXIMIZE.Click
' Dim Hd As Integer = 6
If Me.WindowState = FormWindowState.Maximized Then
BTN_MAXIMIZE.Text = "๐Ÿ——"
Me.WindowState = FormWindowState.Normal
' Panel_TITLE.Height -= Hd
' BTN_MINIMIZE.Top -= Hd
' BTN_MAXIMIZE.Top -= Hd
' BTN_CLOSE.Top -= Hd
' SplitContainer1.Top -= Hd
' SplitContainer1.Height -= Hd
Else
BTN_MAXIMIZE.Text = "๐Ÿ—–"
' Me.ControlBox = True
Me.WindowState = FormWindowState.Maximized
' Me.ControlBox = False
' Panel_TITLE.Height += Hd
' BTN_MINIMIZE.Top += Hd
' BTN_MAXIMIZE.Top += Hd
' BTN_CLOSE.Top += Hd
' SplitContainer1.Top += Hd
' SplitContainer1.Height += Hd
End If
End Sub
Private Sub BTN_CLOSE_Click(sender As Object, e As EventArgs) Handles BTN_CLOSE.Click
Application.Exit()
End Sub
'===============================================================================================================
<DllImport("user32.dll", EntryPoint:="FindWindowEx")>
Public Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
End Function
<DllImport("User32.dll")>
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal uMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
End Function
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
' TextBox1.Text = Cursor.Position.ToString + "%%%"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.FormBorderStyle = FormBorderStyle.None
Me.ControlBox = False
Me.BackColor = SystemColors.ControlDark
Me.ResizeRedraw = True
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Dim rrr As Rectangle = Screen.FromRectangle(Me.Bounds).WorkingArea
rrr.Width += 18
rrr.Height -= 1
Me.MaximumSize = rrr.Size
End Sub
Private Sub Form1_Move(sender As Object, e As EventArgs) Handles Me.Move
If Me.Top = 0 Then
ReleaseCapture()
SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
' Invalidate()
locked = True
Else
locked = False
End If
End Sub
End Class

Related

Move PictureBox with Arrow Keys?

I use the following code to handle positioning of certain controls in my Form;
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
'Sub detects which arrow key is pressed
Dim strControlName As String
' Get the name of the control
strControlName = Me.ActiveControl.Name
Dim aControl = Me.Controls.Item(strControlName)
If strControlName <> "PrintButton" Then
If keyData = Keys.Up Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y - 1)
Return True
End If
'detect down arrow ke
If keyData = Keys.Down Then
aControl.Location = New Point(aControl.Location.X, aControl.Location.Y + 1)
Return True
End If
'detect left arrow key
If keyData = Keys.Left Then
aControl.Location = New Point(aControl.Location.X - 1, aControl.Location.Y)
Return True
End If
'detect right arrow key
If keyData = Keys.Right Then
aControl.Location = New Point(aControl.Location.X + 1, aControl.Location.Y)
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
I also have a PictureBox that I allow a Drag n Drop image into;
Private Sub pbSig_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
pbSig.ImageLocation = files(0)
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub pbSig_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles pbSig.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Is there a way I can make the PictureBox "movable" using the arrow keys? I can't use a KeyPress event on the Form since I am already using it elsewhere. I was hoping I could set a focus on the PictureBox or allow the user to do a "+Arrow" event.
Also, if I make the PictureBox move, is the dropped image going to move with it?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
PictureBox1.Top -= 5
End If
If e.KeyCode = Keys.Down Then
PictureBox1.Top += 5
End If
If e.KeyCode = Keys.Left Then
PictureBox1.Left -= 5
End If
If e.KeyCode = Keys.Right Then
PictureBox1.Left += 5
End If
End Sub
You can use this code to move the PictureBox using Arrow Keys.
Here is what I ended up using. The Mouse just much better sense, plus I get to store it to Settings along with the other settings. I think this is a good solution without getting into any DB work. Opinions?
Private Sub CheckForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'...
pbSig.Location = My.Settings.pbSigLoc
'Allow an image to be dropped
pbSig.AllowDrop = True
End Sub
End Sub
' The next three subs control the moving of the pbSig location using the mouse
Dim startX As Integer
Dim startY As Integer
Dim endX As Integer
Dim endY As Integer
Dim mDown As Boolean
Dim valX As Boolean
Dim valY As Boolean
Private Sub pbSig_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseDown
startX = MousePosition.X
startY = MousePosition.Y
mDown = True
valX = False
valY = False
End Sub
Private Sub Main_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
End Sub
Private Sub pbSig_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseMove
'Check if mouse=down
If mDown = True Then
endX = (MousePosition.X - Me.Left)
endY = (MousePosition.Y - Me.Top)
If valY = False Then
startY = endY - sender.top
valY = True
End If
If valX = False Then
startX = endX - sender.left
valX = True
End If
sender.left = endX - startX
sender.top = endY - startY
End If
End Sub
'If mouseUp=True then End and Save to Settings
Private Sub pbSig_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles pbSig.MouseUp
My.Settings.pbSigLoc = pbSig.Location
mDown = False
valX = False
valY = False
End Sub
This way all the user needs to do is use their mouse to locate the pB and it's contents and I don't need to call the ProcessCmdKey again. And I still have the arrow keys functionality on the Controls I need it to be.

Global Hot Key (RegisterHotKey) even form is not shown in taskbar

I have a program that has 4 functions. When form is minimized I am showing the NotifyIcon and hiding the form in taskbar.
Private Sub FrmMain_Resize(sender As Object, e As EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
NotifyIcon1.Visible = True
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Some Text"
NotifyIcon1.BalloonTipText = "Some Text"
NotifyIcon1.ShowBalloonTip(1000)
Me.ShowInTaskbar = False
End If
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Me.ShowInTaskbar = True
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
End Sub
Even the program is hidden, I still want to execute the 4 functions using hot keys. I found this but it works only when I am not hiding the form in taskbar.
Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Private Enum KeyModifier
None = 0
Alt = &H1
Control = &H2
Shift = &H4
Winkey = &H8
End Enum
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
If m.WParam = 1001 Then
btnFunction1.PerformClick()
ElseIf m.WParam = 1002 Then
btnFunction2.PerformClick()
ElseIf m.WParam = 1003 Then
btnFunction3.PerformClick()
ElseIf m.WParam = 1004 Then
btnFunction4.PerformClick()
End If
End If
MyBase.WndProc(m)
End Sub
Private Sub FrmMain_Closed(sender As Object, e As EventArgs) Handles Me.Closed
Call UnregisterHotKey(Me.Handle, 1001)
Call UnregisterHotKey(Me.Handle, 1002)
Call UnregisterHotKey(Me.Handle, 1003)
Call UnregisterHotKey(Me.Handle, 1004)
End Sub
Private Sub FrmClient_Load(sender As Object, e As EventArgs) Handles MyBase.Load
NotifyIcon1.Text = Me.Text
Call RegisterHotKey(Me.Handle, 1001, KeyModifier.Alt, Keys.F1)
Call RegisterHotKey(Me.Handle, 1002, KeyModifier.Alt, Keys.F2)
Call RegisterHotKey(Me.Handle, 1003, KeyModifier.Alt, Keys.F3)
Call RegisterHotKey(Me.Handle, 1004, KeyModifier.Alt, Keys.F4)
End Sub
Is there a way to use the RegisterHotKey even the form ShowInTaskbar is set to False?

Smart Phone Like Scrolling Solution issue (vb.net)

This solution seems to be the best one out there and the most commonly accepted one - however, if you scroll to the bottom and touch a the actual flowcontrol behind the buttons (I tried to make this so that there would be empty space to make this sample test easier), you then have to double tap-and-hold the button for the scrolling to resume. Restarting the application restores the phone-like scrolling functionality. I am wondering if anyone else has seen this or figured it out - try it with your apps and see if it is the case as well. I modified the snippet above so that you can start a new project, copy and paste this into form1's code, and hit run.
Public Class Form1
Dim FlowPanel As New FlowLayoutPanel
Private Function GenerateButton(ByVal pName As String) As Button
Dim mResult As New Button
With mResult
.Name = pName
.Text = pName
.Width = 128
.Height = 128
.Margin = New Padding(0)
.Padding = New Padding(0)
.BackColor = Color.CornflowerBlue
AddHandler .MouseDown, AddressOf Button_MouseDown
AddHandler .MouseMove, AddressOf Button_MouseMove
End With
Return mResult
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.Width = 806
Me.Height = 480
FlowPanel.Padding = New Padding(0)
FlowPanel.Margin = New Padding(0)
' FlowPanel.ColumnCount = Me.Width / (128 + 6)
FlowPanel.Dock = DockStyle.Fill
FlowPanel.AutoScroll = True
Me.Controls.Add(FlowPanel)
Dim i As Integer
For i = 1 To 98
FlowPanel.Controls.Add(GenerateButton("btn" & i.ToString))
Next
End Sub
Dim myMouseDownPoint As Point
Dim myCurrAutoSMouseDown As Point
Private Sub Button_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
myMouseDownPoint = PointToClient(Cursor.Position)
myCurrAutoSMouseDown = FlowPanel.AutoScrollPosition
End Sub
Private Sub Button_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim mLocation As Point = PointToClient(Cursor.Position)
If myMouseDownPoint <> mLocation Then
Dim mCurrAutoS As Point
Dim mDeslocation As Point = myMouseDownPoint - mLocation
mCurrAutoS.X = Math.Abs(myCurrAutoSMouseDown.X) + mDeslocation.X
mCurrAutoS.Y = Math.Abs(myCurrAutoSMouseDown.Y) + mDeslocation.Y
FlowPanel.AutoScrollPosition = mCurrAutoS
End If
End If
End Sub
End Class
Thanks for the code , I made โ€‹โ€‹some changes to improve behavior . I hope it can be useful to someone .
Dim myMouseDownPoint As Point
Dim myCurrAutoSMouseDown As Point
'Add boolean variable a true.
Private _ValidateClickEvent As Boolean = True
Private Sub MyMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
myMouseDownPoint = PointToClient(Cursor.Position)
myCurrAutoSMouseDown = Panel1.AutoScrollPosition
End Sub
' Add MouseUp event for return the boolean variable a true.
Private Sub MyMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
_ValidateClickEvent = True
End Sub
'Set boolean variable a false when change mlocation.
Private Sub MyMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim mLocation As Point = PointToClient(Cursor.Position)
If myMouseDownPoint <> mLocation Then
Dim mCurrAutoS As Point
Dim mDeslocation As Point = CType(myMouseDownPoint - mLocation, Size)
mCurrAutoS.X = Math.Abs(myCurrAutoSMouseDown.X) + mDeslocation.X
mCurrAutoS.Y = Math.Abs(myCurrAutoSMouseDown.Y) + mDeslocation.Y
Panel1.AutoScrollPosition = mCurrAutoS
_ValidateClickEvent = False
End If
End If
End Sub
' Test boolean variable to perform click event.
Private Sub MyClick(sender As System.Object, e As System.EventArgs)
If _ValidateClickEvent Then
........................
Else
_ValidateClickEvent = True
End If
End Sub

Need to change a mouse event args with a checkbox

I have a class that changes the mouse event args of a picturebox and allows the user to resize it during runtime. I am trying to add a checkbox for each picturebox that will maintain the aspect ratio of the picturebox when the checkbox is checked.
I can get it to work if I add the code separately for each checkbox on the form but I want to keep it in a separate class so that it works for any checkbox.
Here is the code for the class to resize the pictureboxes
Public Class ResizeableControl
Public WithEvents mControl As Control
Public mPreserveAspectRatio As Boolean
Dim AtRightEdge As Boolean = False
Dim AtBottomEdge As Boolean = False
Dim InBoxWidth As Boolean = False
Dim InBoxHeight As Boolean = False
Dim DraggingHorizontal As Boolean
Dim DraggingVerticle As Boolean
Dim DraggingCorner As Boolean
Const DragMarginWidth As Integer = 6
Const DragMarginHeight As Integer = 6
Public dragOrigin As Point
Dim MoveBox As Boolean = False
Dim LastPos As Point
Public Sub New(ByVal Control As Control, ByVal preserveAspectRatio As Boolean)
mControl = Control
mPreserveAspectRatio = preserveAspectRatio
End Sub
Private Sub mControl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseMove
If MoveBox Then
Dim movement As Point = Cursor.Position
' move image by the distance the mouse moved hoizontally and vertically
movement.Offset(-LastPos.X, -LastPos.Y)
mControl.Location = movement
ElseIf DraggingHorizontal Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y) 'How far did the mouse move? (Distance = newPoint - oldPoint)
If mPreserveAspectRatio = True Then
mControl.Width += movement.X 'Change width of the image by the distance the mouse moved
mControl.Height = Math.Round((Convert.ToDouble(mControl.Width) / 6.0) * 4.0)
Else
mControl.Width += movement.X 'Change width of the image by the distance the mouse moved
End If
dragOrigin = e.Location 'Next time we will measure from the now-current mouse position
ElseIf DraggingVerticle Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y)
If mPreserveAspectRatio = True Then
mControl.Height += movement.Y 'Change height of the image by the distance the mouse moved
mControl.Width = Math.Round((Convert.ToDouble(mControl.Height) / 4.0) * 6.0)
Else
mControl.Height += movement.Y 'Change height of the image by the distance the mouse moved
End If
dragOrigin = e.Location
ElseIf DraggingCorner Then
Dim movement As Point = New Point(e.X - dragOrigin.X, e.Y - dragOrigin.Y)
If mPreserveAspectRatio = True Then
' Resize the image by the distance the mouse moved hoizontally and vertically
mControl.Height += movement.Y
mControl.Width += movement.X
mControl.Height = Math.Round((Convert.ToDouble(mControl.Width) / 6.0) * 4.0)
mControl.Width = Math.Round((Convert.ToDouble(mControl.Height) / 4.0) * 6.0)
Else
' Resize the image by the distance the mouse moved hoizontally and vertically
mControl.Height += movement.Y
mControl.Width += movement.X
End If
dragOrigin = e.Location
Else
' Is mouse within right six-or-so pixels?
AtRightEdge = e.X > (mControl.Width - DragMarginWidth)
' Is mouse within bottom six-or-so pixels?
AtBottomEdge = e.Y > (mControl.Height - DragMarginHeight)
' Is mouse within the box?
InBoxWidth = e.X < (mControl.Width - DragMarginWidth)
InBoxHeight = e.Y < (mControl.Height - DragMarginHeight)
' Set the cursor accordingly
If (AtBottomEdge And AtRightEdge) Then
mControl.Cursor = Cursors.SizeNWSE
ElseIf (InBoxWidth And InBoxHeight) Then
mControl.Cursor = Cursors.SizeAll
ElseIf (AtBottomEdge) Then
mControl.Cursor = Cursors.SizeNS
ElseIf (AtRightEdge) Then
mControl.Cursor = Cursors.SizeWE
Else
mControl.Cursor = Cursors.Default
End If
End If
End Sub
Private Sub mControl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseDown
' If the user presses the mouse button at the bottom right corner, begin dragging
If (InBoxWidth And InBoxHeight) Then
Dim movement As Point = Cursor.Position
movement.Offset(-mControl.Location.X, -mControl.Location.Y)
LastPos = movement
MoveBox = True
ElseIf (AtBottomEdge And AtRightEdge) Then
dragOrigin = e.Location
DraggingCorner = True
ElseIf AtRightEdge Then
dragOrigin = e.Location
DraggingHorizontal = True
ElseIf AtBottomEdge Then
dragOrigin = e.Location
DraggingVerticle = True
End If
End Sub
Private Sub mControl_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mControl.MouseUp
' Stop dragging
MoveBox = False
DraggingHorizontal = False
DraggingVerticle = False
DraggingCorner = False
End Sub
Private Sub mControl_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mControl.MouseLeave
MoveBox = False
DraggingHorizontal = False
DraggingVerticle = False
DraggingCorner = False
mControl.Cursor = Cursors.Default
End Sub
End Class
I create a new instance of the class for each picturebox (I have just 2 here to keep it simple but have more) when the form loads but then I can't figure out how to change the aspect ratio boolean (mPreserveAspectRatio). I have tried just changing the boolean when the checkbox changes states but that doesn't work. And I think I may need to somehow create a public method to change the boolean but can't wrap my head around it. Here is the latest code I have been trying that doesn't work
Imports WindowsApplication1.ResizeableControl
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mControl As New ResizeableControl(PictureBox1, True)
Dim mControl2 As New ResizeableControl(PictureBox2, True)
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Dim mControl1 As New ResizeableControl(PictureBox1, True)
Else
Dim mControl1 As New ResizeableControl(PictureBox1, False)
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Dim mControl1 As New ResizeableControl(PictureBox2, True)
Else
Dim mControl1 As New ResizeableControl(PictureBox2, False)
End If
End Sub
End Class
I figured it out. You pointed me in the right direction Jeff. I did need to add the control to the form. Here is what I did.
Imports WindowsApplication1.ResizeableControl
Public Class Form1
Dim mControl1 As New ResizeableControl(PictureBox1, True)
Dim mControl2 As New ResizeableControl(PictureBox2, True)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Control1 As New ResizeableControl(PictureBox1, True)
Me.mControl1 = Control1
Dim Control2 As New ResizeableControl(PictureBox2, True)
Me.mControl2 = Control2
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
Me.mControl1.mPreserveAspectRatio = True
Else
Me.mControl1.mPreserveAspectRatio = False
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.Checked Then
Me.mControl2.mPreserveAspectRatio = True
Else
Me.mControl2.mPreserveAspectRatio = False
End If
End Sub
End Class

How to remove a line which is drawn on a form

I'm trying to draw a triangle like this:
Dim triangle As Graphics
Dim pen1 As New Pen(Color.LimeGreen, 2)
Dim lside As Integer
Dim wside As Integer
Dim dside As Integer
triangle = Me.CreateGraphics()
triangle.DrawLine(pen1, wside, 420, 640, 420)
triangle.DrawLine(pen1, 640, lside, 640, 420)
triangle.DrawLine(pen1, dside, 420, 640, lside)
lside, wside and dside stand for length side, width side and diagonal side.
I've got 4 textboxes, for the length, width, diagonal side and one for the angle.
The purpose is to fill in 2 of the values, and then a rectangular triangle gets drawn following Pythagoras' theorem. I want to draw a line for Angle as well later on. But I first want to get this to work.
But every time I click the button to draw a new triangle, the previous one should get deleted. And that's the problem.
I've tried multiple methods, like triangle.Dispose triangle.Restore triangle.Clear and more. None of them work.
Why am I not drawing them in a picturebox you might ask. Well, when I drew a line in a picturebox, the picturebox sort of went in front of the line, making the line invisible. And I didn't know how to fix that.
Try using Me.Invalidate(), it basically clears, then draws the shape in the area you're painting on. Reference.
Private Sub ClearCanvas_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Invalidate()
End Sub
Priavte DrawTriangle_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim triangle As Graphics
Dim pen1 As New Pen(Color.LimeGreen, 2)
Dim lside As Integer
Dim wside As Integer
Dim dside As Integer
triangle = Me.CreateGraphics()
triangle.DrawLine(pen1, wside, 420, 640, 420)
triangle.DrawLine(pen1, 640, lside, 640, 420)
triangle.DrawLine(pen1, dside, 420, 640, lside)
End Sub
โ€˜Draw select delete multiple lines on Picturebox
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim drawrec, undo_delete As Boolean
Dim index_arrary_line_tobe_deleted(10000) As Integer
Dim ptA, ptB As Point ' starting and ending point
Dim down As Boolean
Dim k, Last_index_line_tobe_selected As Integer
Private temp As line
Dim List_of_line_tobe_deleted As New List(Of line)
Dim List_of_line_to_Undo As New List(Of line)
Private m_Lines As New List(Of line)
Private m_Pt As Point
Private m_Pt2 As Point
Private m_tracking As Boolean
Private Sub B2_index_arrary_line_tobe_deletedete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2_Delete.Click
Try
m_Lines.RemoveAll(AddressOf List_of_line_tobe_deleted.Contains)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub B3_Undodelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B3_Undodelete.Click
undo_delete = True
Try
m_Lines.AddRange(List_of_line_tobe_deleted)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub B1_Select_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1_Select.Click
drawrec = True
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))
If m_tracking = True And drawrec = True Then
k = -1
For i As Integer = 0 To m_Lines.Count - 1
If m_Lines(i).ContainsCompletely(r) = True Then
k = k + 1
index_arrary_line_tobe_deleted(i) = k
Debug.Print("Index of NOT selected lines " + i.ToString + "Index of selected lines " + Last_index_line_tobe_selected.ToString) 'to compare idex of two lists !!!!
index_arrary_line_tobe_deleted(k) = i
List_of_line_tobe_deleted.Add(m_Lines(i))
End If
Next
Last_index_line_tobe_selected = k 'so far no use, just to know
e.Graphics.DrawRectangle(Pens.Cyan, r)
End If
If undo_delete = False Then
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Draw(e.Graphics, r)
Debug.Print("Index of remaining lines " + i.ToString)
Next
End If
If undo_delete = True Then
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Re_Draw(e.Graphics)
Debug.Print("Index of remaining lines " + i.ToString)
Next
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
drawrec = False
down = False
undo_delete = False
For i As Integer = 0 To index_arrary_line_tobe_deleted.Length - 1
index_arrary_line_tobe_deleted(0) = -1
Next i
k = -1
Last_index_line_tobe_selected = -1
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
down = True
If down = True And drawrec = False Then
ptA = e.Location
temp = New line
temp.StartPoint = e.Location
End If
If e.Button = MouseButtons.Left Then
ResetSelected(Me.m_Lines)
m_Pt = e.Location
End If
End Sub
Private Sub ResetSelected(ByVal m_Lines As List(Of line))
For i As Integer = 0 To m_Lines.Count - 1
m_Lines(i).Selected = False
Next
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If down = True Then
End If
If e.Button = MouseButtons.Left Then
If down = True And drawrec = False Then
ptB = e.Location
temp.EndPoint = e.Location
End If
m_Pt2 = e.Location
m_tracking = True
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
down = False
If drawrec = False Then
temp.EndPoint = e.Location
m_Lines.Add(temp)
temp = Nothing
Me.PictureBox1.Invalidate()
End If
m_tracking = False
Me.PictureBox1.Invalidate()
End Sub
End Class
Public Class line
Public StartPoint As Point
Public EndPoint As Point
Private m_selected As Boolean
Public Property Selected() As Boolean
Get
Return m_selected
End Get
Set(ByVal value As Boolean)
m_selected = value
End Set
End Property
Public Sub Draw(ByVal g As Graphics, ByVal r As Rectangle)
Dim myPen1 As New Pen(Color.Red, 1)
g.SmoothingMode = SmoothingMode.AntiAlias
If Me.ContainsCompletely(r) OrElse Me.Selected Then
Me.Selected = True
g.DrawLine(myPen1, Me.StartPoint, Me.EndPoint)
Else
Dim myPen2 As New Pen(Color.Blue, 1)
g.DrawLine(myPen2, Me.StartPoint, Me.EndPoint)
End If
End Sub
Public Sub Re_Draw(ByVal g As Graphics)
g.SmoothingMode = SmoothingMode.AntiAlias
Dim myPen2 As New Pen(Color.Blue, 1)
g.DrawLine(myPen2, Me.StartPoint, Me.EndPoint)
End Sub
Public Function ContainsCompletely(ByVal r As Rectangle) As Boolean
If r.Contains(Me.StartPoint) AndAlso r.Contains(Me.EndPoint) Then
Return True
End If
Return False
End Function
End Class
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
Dim drawrec As Boolean
Dim del As Integer
Dim ptA, ptB As Point ' starting and ending point
Dim down As Boolean
Private temp As line
Private m_Lines As New List(Of line)
Private m_rnd As New Random
Private m_Pt As Point
Private m_Pt2 As Point
Private m_tracking As Boolean
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Delete.Click
Try
m_Lines.RemoveAt(del)
Catch ex As Exception
End Try
PictureBox1.Refresh()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
drawrec = False
down = False
del = -1
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
down = True
If down = True And drawrec = False Then
ptA = e.Location
temp = New line
temp.StartPoint = e.Location
End If
If e.Button = MouseButtons.Left Then
ResetSelected(Me.m_Lines)
m_Pt = e.Location
End If
End Sub
Private Sub ResetSelected(ByVal m_Lines As List(Of line))
For i As Integer = 0 To m_Lines.Count - 1
m_Lines(i).Selected = False
Next
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseMove
If down = True Then
End If
If e.Button = MouseButtons.Left Then
If down = True And drawrec = False Then
ptB = e.Location
temp.EndPoint = e.Location
End If
m_Pt2 = e.Location
m_tracking = True
Me.PictureBox1.Invalidate()
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseUp
down = False
If drawrec = False Then
temp.EndPoint = e.Location
m_Lines.Add(temp)
temp = Nothing
Me.PictureBox1.Invalidate()
End If
m_tracking = False
Me.PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim r As New Rectangle(m_Pt, New Size(m_Pt2.X - m_Pt.X, m_Pt2.Y - m_Pt.Y))
If m_tracking And drawrec = True Then
For i As Integer = 0 To m_Lines.Count - 1
If m_Lines(i).ContainsCompletely(r) = True Then
Debug.Print("KKKKKKKKKKKKKKK " + i.ToString)
del = i
End If
Next
e.Graphics.DrawRectangle(Pens.Cyan, r)
End If
For i As Integer = 0 To m_Lines.Count - 1
Me.m_Lines(i).Draw(e.Graphics, r)
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Select.Click
drawrec = True
End Sub
End Class
Public Class line
Public StartPoint As Point
Public EndPoint As Point
Public Filled As Boolean
Public ShapeColor As Color
Public PenWidth As Integer
Private m_selected As Boolean
Public Property Selected() As Boolean
Get
Return m_selected
End Get
Set(ByVal value As Boolean)
m_selected = value
End Set
End Property
Public Sub Draw(ByVal g As Graphics, ByVal r As Rectangle)
g.SmoothingMode = SmoothingMode.AntiAlias
If Me.ContainsCompletely(r) OrElse Me.Selected Then
Me.Selected = True
g.DrawLine(Pens.Red, Me.StartPoint, Me.EndPoint)
Else
g.DrawLine(Pens.Blue, Me.StartPoint, Me.EndPoint)
End If
End Sub
Public Function ContainsCompletely(ByVal r As Rectangle) As Boolean
If r.Contains(Me.StartPoint) AndAlso r.Contains(Me.EndPoint) Then
Return True
End If
Return False
End Function
End Class