Click here to Skip to main content
15,896,557 members
Articles / Desktop Programming / Windows Forms

Shape Control for .NET

Rate me:
Please Sign up or sign in to vote.
4.84/5 (170 votes)
23 Mar 2017CPOL10 min read 381.8K   21.7K   301  
Implementing shape control that supports transparency, custom design-time editors and simple animation
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Reflection
Imports System.Linq
Imports System.IO

Public Partial Class Form3
	Inherits Form
	Private ctrllist As New List(Of ShapeControl.CustomControl1)()
	'cam
	Private ctrllist1 As New List(Of ShapeControl.CustomControl1)()
	'line
	Private linelist As List(Of ShapeControl.CustomControl1)
	'temp line list
	Private sx As Integer, sy As Integer
	'mouse down start pos
	Private static_i As Integer = 0

	Private ctrlKey As Boolean = False
	Private altKey As Boolean = False
	Private plusKey As Boolean = False
	Private shiftKey As Boolean = False
	Private minusKey As Boolean = False
	Public Sub New()
		InitializeComponent()
	End Sub

	Private Sub btnAddCam_Click(sender As Object, e As EventArgs)
		AddCam("")
	End Sub

    Private Function getNextCamIndex() As Integer
        If ctrllist.Count = 0 Then
            Return 1
        End If
        Dim tempvar As Object
        tempvar = ctrllist.OrderBy(Function(x) x.Name).ToList()
        ctrllist = DirectCast(tempvar, List(Of ShapeControl.CustomControl1))
        ctrllist = ctrllist.OrderBy(Function(x) x.Name.Length).ToList()
        Dim templist As List(Of ShapeControl.CustomControl1) = ctrllist.ToList()
        Dim count As Integer = templist.Count
        Dim retval As Integer = count + 1

        'find missing index
        For i As Integer = 0 To count - 1
            Dim ctrlname As String = templist(i).Name
            Dim ctrlindex As String = ctrlname.Substring(3)
            If (i + 1) <> Integer.Parse(ctrlindex) Then
                retval = (i + 1)
                Exit For
            End If
        Next


        Return retval
    End Function

	'get list of line connected to the cam
	Private Function getLines(cam As Integer) As List(Of ShapeControl.CustomControl1)
		Dim list As New List(Of ShapeControl.CustomControl1)()
		For i As Integer = 0 To ctrllist1.Count - 1
			Dim v = (ctrllist1(i).Name).Split("_"C)
			If Integer.Parse(v(1)) = cam OrElse Integer.Parse(v(2)) = cam Then
				list.Add(ctrllist1(i))
			End If
		Next

		Return list
	End Function





	'set position and size for the line shape control
	'special for line joing 2 cams
	Private Sub setLineJoiningCams(ByRef ctrl1 As ShapeControl.CustomControl1)

		'note that we want the arrow in the direction of the dest_cam
		Dim src_cam As Integer, dest_cam As Integer
		Dim v = ctrl1.Name.Split("_"C)
		src_cam = Integer.Parse(v(1))
		dest_cam = Integer.Parse(v(2))

		Dim ctrlsFromcam As Control() = Me.panel1.Controls.Find("cam" & src_cam, False)
		Dim ctrlsTocam As Control() = Me.panel1.Controls.Find("cam" & dest_cam, False)



		'''////connection pts//////
		'    ----*---
		'    |       |
		'    *   *   *
		'    |       |
		'    ----*---
		'''////////////////////////

		Dim ctrlsrc As ShapeControl.CustomControl1 = DirectCast(ctrlsFromcam(0), ShapeControl.CustomControl1)
		Dim ctrldest As ShapeControl.CustomControl1 = DirectCast(ctrlsTocam(0), ShapeControl.CustomControl1)


		'default src_cam location
		ctrlsrc.Connector = ShapeControl.ConnecterType.Center
		Dim x0 As Integer = ctrlsrc.Location.X + ctrlsrc.Width / 2
		Dim y0 As Integer = ctrlsrc.Location.Y + ctrlsrc.Height / 2

		'default dest_cam location
		ctrldest.Connector = ShapeControl.ConnecterType.Center
		Dim x1 As Integer = ctrldest.Location.X + ctrldest.Width / 2
		Dim y1 As Integer = ctrldest.Location.Y + ctrldest.Height / 2


		're-adjust connection points
		ShapeControl.Line.setConnectors(ctrlsrc, ctrldest)
		ShapeControl.Line.setConnectorPoint(x0, y0, ctrlsrc)
		ShapeControl.Line.setConnectorPoint(x1, y1, ctrldest)

		'call the generic function
		ShapeControl.Line.setLine(ctrl1, x0, y0, x1, y1)


	End Sub



	'add a line joining fromcam to tocam
	Private Sub AddLine(fromcam As Integer, tocam As Integer, argb As Integer)
		Dim  ctrl2 As New ShapeControl.CustomControl1()
		ctrl2.Name = "line_" & fromcam & "_" & tocam
		'ctrllist.Count;
		ctrl2.Tag2 = "noflip"
		' to support direction change and toggle
		ctrl2.Direction = ShapeControl.LineDirection.LeftDown
		'default direction 
        AddHandler ctrl2.MouseDoubleClick, New MouseEventHandler(AddressOf ctrl2_MouseDoubleClick)
        AddHandler ctrl2.MouseEnter, New EventHandler(AddressOf ctrl2_MouseEnter)
        AddHandler ctrl2.MouseLeave, New EventHandler(AddressOf ctrl2_MouseLeave)


		' ctrl2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(126)))), Color.Red);
		ctrl2.Blink = False
		ctrl2.Vibrate = False
		ctrl2.AnimateBorder = False

		ctrl2.BackColor = System.Drawing.Color.FromArgb(CInt(CByte(0)), CInt(CByte(255)), CInt(CByte(255)), CInt(CByte(255)))
		ctrl2.BorderColor = System.Drawing.Color.FromArgb(argb)
		ctrl2.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid
		ctrl2.BorderWidth = 8
		'            ctrl2.Font = new System.Drawing.Font("Arial", 8f, System.Drawing.FontStyle.Bold);

		ctrl2.UseGradient = False
		ctrl2.Visible = True

		setLineJoiningCams(ctrl2)

		ctrllist1.Add(ctrl2)
		Me.panel1.Controls.Add(ctrl2)
		' ctrl2.SendToBack();
		ctrl2.BringToFront()


	End Sub

	Private Sub ctrl2_MouseLeave(sender As Object, e As EventArgs)
		Cursor = Cursors.[Default]
	End Sub

	Private Sub ctrl2_MouseEnter(sender As Object, e As EventArgs)
		Cursor = Cursors.Hand
	End Sub


	Private Sub ctrl2_MouseDoubleClick(sender As Object, e As MouseEventArgs)
		If e.Button = MouseButtons.Left Then

			If ctrlKey AndAlso Not altKey AndAlso Not shiftKey Then
				Dim dr As DialogResult = MessageBox.Show(Me, "Delete line?", "Delete", MessageBoxButtons.OKCancel)
				If dr = DialogResult.OK Then
					ctrllist1.Remove(DirectCast(sender, ShapeControl.CustomControl1))
					panel1.Controls.Remove(DirectCast(sender, ShapeControl.CustomControl1))
				End If
				ctrlKey = False

				Return
			End If



			If altKey AndAlso Not ctrlKey AndAlso Not shiftKey Then
				DirectCast(sender, ShapeControl.CustomControl1).AnimateBorder = Not DirectCast(sender, ShapeControl.CustomControl1).AnimateBorder
				altKey = False
				Return
			End If

			'toggle none=0, noflip=1, flip=2
			If Not altKey AndAlso Not ctrlKey AndAlso shiftKey Then


				Dim ctrl1 As ShapeControl.CustomControl1 = DirectCast(sender, ShapeControl.CustomControl1)


				If ctrl1.Tag2.ToString() <> "none" Then
					ctrl1.Direction = ShapeControl.LineDirection.RightDown
					' default;
					Dim v = ctrl1.Name.Split("_"C)
					Dim newname As String = (v(0) + "_" + v(2) & "_") + v(1)
					ctrl1.Name = newname
					setLineJoiningCams(ctrl1)
				Else

					ctrl1.Direction = ShapeControl.LineDirection.None
					setLineJoiningCams(ctrl1)
				End If

				Select Case ctrl1.Tag2
					Case "noflip"
						ctrl1.Tag2 = "none"
						Exit Select
					Case "none"
						ctrl1.Tag2 = "flip"
						Exit Select
					Case "flip"
						ctrl1.Tag2 = "noflip"
						Exit Select
				End Select


				shiftKey = False
				Return
			End If


			DirectCast(sender, ShapeControl.CustomControl1).BorderColor = getNextColor()
		End If


	End Sub

	'adding a new cam base on caminfo
	Private Sub AddCam(caminfo As String)


		Dim bNew As Boolean = (caminfo = "")
		Dim name As String = ""
		Dim tag As String = "", tag2 As String = ""
		Dim x As Integer = 0, y As Integer = 0, w As Integer = 0, h As Integer = 0, c As Integer = 0

		'caminfo format
		'name=cam1|x=400|y=146|w=40|h=40|c=2130640896|tag=16,-97,359956|tag2=127.0.0.1:New cam
		If caminfo <> "" Then
			Dim info = caminfo.Split("|"C)
			For i As Integer = 0 To info.Length - 1
				Dim details = info(i).Split("="C)
				Select Case details(0)
					Case "name"
						name = details(1)
						Exit Select
					Case "x"
						x = Integer.Parse(details(1))
						Exit Select
					Case "y"
						y = Integer.Parse(details(1))
						Exit Select
					Case "w"
						w = Integer.Parse(details(1))
						Exit Select
					Case "h"
						h = Integer.Parse(details(1))
						Exit Select
					Case "c"
						c = Integer.Parse(details(1))
						Exit Select
					Case "tag"
						tag = details(1)
						Exit Select
					Case "tag2"
						tag2 = details(1)
						Exit Select

				End Select
			Next
		End If
        Dim ctrl1 As New ShapeControl.CustomControl1()

		ctrl1.BackColor = If(bNew, System.Drawing.Color.FromArgb(CInt(CByte(126)), Color.Red), Color.FromArgb(c))
		ctrl1.Blink = False
		ctrl1.BorderColor = System.Drawing.Color.FromArgb(CInt(CByte(0)), CInt(CByte(255)), CInt(CByte(255)), CInt(CByte(255)))
		ctrl1.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid
		ctrl1.BorderWidth = 3

		ctrl1.Font = New System.Drawing.Font("Arial", 8F, System.Drawing.FontStyle.Bold)

		ctrl1.Name = If(bNew, "cam" & getNextCamIndex(), name)
		'ctrllist.Count;
		ctrl1.Shape = ShapeControl.ShapeType.Ellipse
        ctrl1.ShapeImage = DirectCast(Global.TestShapeControl.My.Resources.camshape, System.Drawing.Image)
		ctrl1.Size = If(bNew, New System.Drawing.Size(40, 40), New System.Drawing.Size(w, h))

		ctrl1.TabIndex = 0
		ctrl1.UseGradient = False
		ctrl1.Vibrate = False
		ctrl1.Visible = True


        AddHandler ctrl1.MouseDown, New MouseEventHandler(AddressOf ctrl1_MouseDown)
        AddHandler ctrl1.MouseMove, New MouseEventHandler(AddressOf ctrl1_MouseMove)
        AddHandler ctrl1.MouseUp, New MouseEventHandler(AddressOf ctrl1_MouseUp)
        AddHandler ctrl1.MouseDoubleClick, New MouseEventHandler(AddressOf ctrl1_MouseDoubleClick)
        AddHandler ctrl1.MouseHover, New EventHandler(AddressOf ctrl1_MouseHover)

		'for drag and drop
        AddHandler ctrl1.DragEnter, New DragEventHandler(AddressOf ctrl1_DragEnter)
        AddHandler ctrl1.DragDrop, New DragEventHandler(AddressOf ctrl1_DragDrop)
		ctrl1.AllowDrop = True


		ctrllist.Add(ctrl1)
		Dim ypos As Integer = (50 * ctrllist.Count) Mod panel1.Height
		Dim xpos As Integer = ((50 * ctrllist.Count) \ panel1.Height) * 50

		ctrl1.Location = If(bNew, New System.Drawing.Point(50 + xpos, ypos - 20), New System.Drawing.Point(50, 50))
		Me.panel1.Controls.Add(ctrl1)
		ctrl1.Text = "cam"
		ctrl1.Text = If(bNew, DirectCast(ctrl1.Name.ToString().Clone(), String), name)
		ctrl1.BringToFront()
		ctrl1.Tag2 = If(bNew, "127.0.0.1:New cam", tag2)

		'set the color
		If bNew Then
			ctrl1.BackColor = getNextColor()
		End If


		Dim dy As Single = CSng(ctrl1.Top + ctrl1.Height / 2) - CSng(panel1.Height) / 2
		Dim dx As Single = CSng(ctrl1.Left + ctrl1.Width / 2) - CSng(panel1.Width) / 2

		ctrl1.Tag = If(bNew, (dx & "," & dy & "," & getNumPixelforImageDisplayed()), tag)

	End Sub


	'when a cam is drop form source to dest
	Private Sub ctrl1_DragDrop(sender As Object, e As DragEventArgs)
		Dim s_src As String = e.Data.GetData(DataFormats.Text).ToString()
		Dim s_dest As String = DirectCast(sender, Control).Name
		'   System.Diagnostics.Debug.Print("Data:" + s_src);
		If s_dest <> s_src Then
			Dim src_cam As Integer, dest_cam As Integer
			src_cam = Integer.Parse(s_src.Substring(3))
			dest_cam = Integer.Parse(s_dest.Substring(3))

			'find if there are already any line joining src_cam and dest_cam 
			Dim ctrlsFromTo As Control() = Me.panel1.Controls.Find("line_" & src_cam & "_" & dest_cam, False)
			Dim ctrlsToFrom As Control() = Me.panel1.Controls.Find("line_" & dest_cam & "_" & src_cam, False)

			'default color of new line
			Dim defaultargb As Integer = Color.FromArgb(126, Color.Red).ToArgb()

			' no line joining src_cam and dest_cam ,so we can add line
			' naming convention is:
			' line_<src_cam>_<dest_cam>
			If ctrlsFromTo.Length = 0 AndAlso ctrlsToFrom.Length = 0 Then
				AddLine(src_cam, dest_cam, defaultargb)


			End If
		End If

		'    System.Diagnostics.Debug.Print("Drop " + data + " " + s);

	End Sub


	Private Sub ctrl1_DragEnter(sender As Object, e As DragEventArgs)
		'source cam in data
		Dim src_cam As String = e.Data.GetData(DataFormats.Text).ToString()
		'dest cam in s
		Dim dest_cam As String = DirectCast(sender, Control).Name
		'  System.Diagnostics.Debug.Print("Data:" + data  );

		'show the cursor for the Move effect
		If src_cam <> dest_cam Then
			e.Effect = DragDropEffects.Move
		End If

		'  System.Diagnostics.Debug.Print("Enter " +  data + " " +s);
	End Sub



	Private Sub ctrl1_MouseUp(sender As Object, e As MouseEventArgs)
		'if (e.Button.Equals(MouseButtons.Left) && ctrlKey )
		'    System.Diagnostics.Debug.Print("mouse up " +((ShapeControl.CustomControl1)sender).Name);
	End Sub

    Private Sub ctrl1_MouseHover(ByVal sender As Object, ByVal e As EventArgs)
        toolTip1.Show(DirectCast(sender, ShapeControl.CustomControl1).Tag2 & ",(" & DirectCast(sender, ShapeControl.CustomControl1).Left & "," & DirectCast(sender, ShapeControl.CustomControl1).Top & ")", DirectCast(sender, ShapeControl.CustomControl1), 2000)
    End Sub


	Private Function getNextColor() As Color

		If static_i >= 6 Then
			static_i = 0
		End If
		Dim retcolr As Color = System.Drawing.Color.FromArgb(126, Color.Red)
		Select Case static_i
			Case 0
				retcolr = System.Drawing.Color.FromArgb(126, Color.Red)
				Exit Select
			Case 1
				retcolr = System.Drawing.Color.FromArgb(126, Color.Blue)

				Exit Select

			Case 2
				retcolr = System.Drawing.Color.FromArgb(126, Color.Green)

				Exit Select

			Case 3
				retcolr = System.Drawing.Color.FromArgb(126, Color.Wheat)
				Exit Select
			Case 4
				retcolr = System.Drawing.Color.FromArgb(126, Color.GreenYellow)

				Exit Select

			Case 5
				retcolr = System.Drawing.Color.FromArgb(126, Color.Cyan)

				Exit Select

		End Select
		static_i += 1
		Return retcolr
	End Function

	Private Sub ctrl1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
		If e.Clicks < 2 Then
			Return
		End If

		If e.Button.Equals(MouseButtons.Left) Then
			If plusKey AndAlso Not minusKey Then
				If DirectCast(sender, ShapeControl.CustomControl1).Width < 80 Then

					DirectCast(sender, ShapeControl.CustomControl1).Size = New Size(DirectCast(sender, ShapeControl.CustomControl1).Width + 5, DirectCast(sender, ShapeControl.CustomControl1).Height + 5)
				End If
				plusKey = False
				Return
			End If

			If minusKey AndAlso Not plusKey Then
				If DirectCast(sender, ShapeControl.CustomControl1).Width > 20 Then

					DirectCast(sender, ShapeControl.CustomControl1).Size = New Size(DirectCast(sender, ShapeControl.CustomControl1).Width - 5, DirectCast(sender, ShapeControl.CustomControl1).Height - 5)
				End If
				minusKey = False
				Return
			End If
			If ctrlKey AndAlso Not altKey Then
				Dim dr As DialogResult = MessageBox.Show(Me, "Delete cam?", "Delete", MessageBoxButtons.OKCancel)
				If dr = DialogResult.OK Then
					'delete all lines connected to the cam
					Dim camindex As Integer = Integer.Parse(DirectCast(sender, ShapeControl.CustomControl1).Name.Substring(3))

					linelist = getLines(camindex)
					For i As Integer = 0 To linelist.Count - 1
						Dim line As ShapeControl.CustomControl1 = linelist(i)
						ctrllist1.Remove(line)
						panel1.Controls.Remove(line)
					Next

					ctrllist.Remove(DirectCast(sender, ShapeControl.CustomControl1))


					panel1.Controls.Remove(DirectCast(sender, ShapeControl.CustomControl1))
				End If
				ctrlKey = False

				Return
			End If

			If altKey AndAlso Not ctrlKey Then
				DirectCast(sender, ShapeControl.CustomControl1).Vibrate = Not DirectCast(sender, ShapeControl.CustomControl1).Vibrate
				altKey = False
				Return
			End If



			DirectCast(sender, ShapeControl.CustomControl1).BackColor = getNextColor()
		End If


	End Sub

	Private Sub ctrl1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
		If e.Button.Equals(MouseButtons.Left) Then

			If ctrlKey AndAlso altKey Then



				DirectCast(sender, Control).DoDragDrop(DirectCast(sender, ShapeControl.CustomControl1).Name, DragDropEffects.Copy Or DragDropEffects.Move)
			Else

				Dim s As String = DirectCast(sender, ShapeControl.CustomControl1).Name.Substring(3)
				sx = e.X
				sy = e.Y


				Dim camindex As Integer = Integer.Parse(s)
				linelist = getLines(camindex)

			End If
		End If

		If e.Button.Equals(MouseButtons.Right) Then
			'if (ctrlKey )
			'{

			'  //  AddLine(1, 2);
			'    System.Diagnostics.Debug.Print("mouse down" + ((ShapeControl.CustomControl1)sender).Name);
			'    ((Control)sender).DoDragDrop("addline", DragDropEffects.All);
			'}
			'else
			' {
			Dim frm As New FormProperty()
			frm.Caller = DirectCast(sender, ShapeControl.CustomControl1)

				' }


			frm.ShowDialog()
		End If
	End Sub

	Private Sub ctrl1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs)
		If ctrlKey OrElse altKey Then
			Return
		End If

		If e.Button.Equals(MouseButtons.Left) Then
			DirectCast(sender, Control).Left = DirectCast(sender, Control).Left + (e.X - sx)
			DirectCast(sender, Control).Top = DirectCast(sender, Control).Top + (e.Y - sy)

			DirectCast(sender, Control).Refresh()

			Dim dy As Single = CSng(DirectCast(sender, Control).Top) + DirectCast(sender, Control).Height \ 2 - CSng(panel1.Height) / 2
			Dim dx As Single = CSng(DirectCast(sender, Control).Left) + DirectCast(sender, Control).Width \ 2 - CSng(panel1.Width) / 2
			DirectCast(sender, Control).Tag = dx & "," & dy & "," & getNumPixelforImageDisplayed()

			For i As Integer = 0 To linelist.Count - 1
				Dim ctrl1 = linelist(i)
				setLineJoiningCams(ctrl1)

				ctrl1.Parent.Refresh()
				ctrl1.Refresh()


			Next
		End If
	End Sub

	Private Sub Form3_KeyDown(sender As Object, e As KeyEventArgs)
		ctrlKey = e.Control
		altKey = e.Alt
		shiftKey = e.Shift
		If e.KeyCode = Keys.OemMinus Then
			minusKey = True
		End If
		If e.KeyCode = Keys.Oemplus Then
			plusKey = True
		End If
	End Sub

	Private Sub Form3_KeyUp(sender As Object, e As KeyEventArgs)
		ctrlKey = False
		altKey = False
		shiftKey = False
		minusKey = False
		plusKey = False
	End Sub

	Private Sub panel1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
		openFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"
		Dim dr As DialogResult = openFileDialog1.ShowDialog()
		If dr = DialogResult.OK Then
			Try
				Using tempImage as Bitmap = New Bitmap(openFileDialog1.FileName)
					panel1.BackgroundImage = New Bitmap(tempImage)
				End Using

			Catch
			End Try
		End If

	End Sub

	Private Function getNumPixelforImageDisplayed() As Integer
		Dim panelratio As Single = CSng(panel1.Width) / panel1.Height
		Dim imgratio As Single = CSng(panel1.BackgroundImage.Width) / CSng(panel1.BackgroundImage.Height)
		Dim dispwidth As Single, dispheight As Single
		If panelratio > imgratio Then
			'height limiting
			dispheight = panel1.Height
			dispwidth = imgratio * dispheight
		Else
			dispwidth = panel1.Width
			dispheight = dispwidth / imgratio
		End If

		' System.Diagnostics.Debug.Print(imgratio +"," + dispwidth + "," + dispheight);

		Return CInt(Math.Truncate(dispwidth * dispheight))
	End Function


	Private Sub button2_Click(sender As Object, e As EventArgs)
		label1.Text = "NewMap_" & Guid.NewGuid().ToString() & ".map"
		panel1.BackgroundImage = New Bitmap(panel1.Width, panel1.Height)
		Graphics.FromImage(panel1.BackgroundImage).FillRectangle(Brushes.White, New Rectangle(0, 0, panel1.Width, panel1.Height))
		Graphics.FromImage(panel1.BackgroundImage).DrawString("Dbl Click here to insert floor plan..", New Font(FontFamily.GenericSansSerif, 12), Brushes.Black, 50, 50)
		ctrllist.Clear()
		ctrllist1.Clear()
		panel1.Controls.Clear()

	End Sub

	Private Sub button1_Click(sender As Object, e As EventArgs)
		Using writer As StreamWriter = File.CreateText(label1.Text)
            Dim tempvar As List(Of ShapeControl.CustomControl1) = ctrllist.OrderBy(Function(x) x.Name).ToList()
            tempvar = tempvar.OrderBy(Function(x) x.Name.Length).ToList()

			Dim templist As List(Of ShapeControl.CustomControl1) = tempvar.ToList()
			writer.WriteLine("CAM_COUNT=" & templist.Count)
			For i As Integer = 0 To templist.Count - 1

                writer.WriteLine("name=" & templist(i).Name & "|" & "x=" & templist(i).Left & "|" & "y=" & templist(i).Top & "|" & "w=" & templist(i).Width & "|" & "h=" & templist(i).Height & "|" & "c=" & templist(i).BackColor.ToArgb() & "|" & "tag=" & templist(i).Tag.ToString() & "|" & "tag2=" & templist(i).Tag2.ToString())
			Next
			writer.WriteLine("LINE_COUNT=" & ctrllist1.Count)
			For i As Integer = 0 To ctrllist1.Count - 1
                writer.WriteLine("name=" & ctrllist1(i).Name & "|c=" & ctrllist1(i).BorderColor.ToArgb())
			Next
		End Using
		If panel1.BackgroundImage IsNot Nothing Then
			panel1.BackgroundImage.Save(label1.Text & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
		End If

		MessageBox.Show(label1.Text & " is saved")
	End Sub

	Private Sub Form3_Load(sender As Object, e As EventArgs)

		Me.DoubleBuffered = True

		'invoke double buffer 
		GetType(Panel).InvokeMember("DoubleBuffered", System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.SetProperty, Nothing, panel1, New Object() {True})

		label2.Text = "On Cam> Right Click:Set Properties, Dbl_Click:Change Color, Ctl+Dbl_Click:Del, Alt+Dbl_Click:Vibrate, Minus+Dbl_Click:Smaller, Plus+Dbl_Click:Larger," & " Ctl+Alt Drag:Add Line" & vbLf & "On Line>Dbl_Click:Change Color, Ctl + Dbl_click:Delete, Alt+Dbl_Click:Animate Border, Shift+Dbl_Click:Dir Flip->None->Flip"

		button2_Click(Nothing, Nothing)
	End Sub

	Private Sub btnImportMap_Click(sender As Object, e As EventArgs)
		openFileDialog1.Filter = "Map files (*.map)|*.map"
		Dim dr As DialogResult = openFileDialog1.ShowDialog()
		If dr = DialogResult.OK Then

			button2_Click(Nothing, Nothing)
			label1.Text = openFileDialog1.FileName

			Try
				Using reader As StreamReader = File.OpenText(label1.Text)
					Dim s As String = reader.ReadLine()
					'info example: CAM_COUNT=5
					Dim info = s.Split("="C)
					For i As Integer = 0 To Integer.Parse(info(1)) - 1
						s = reader.ReadLine()
						'format for s:
						'name=cam1|x=400|y=146|w=40|h=40|c=2130640896|tag=16,-97,359956|tag2=127.0.0.1:New cam
						AddCam(s)
					Next

					s = reader.ReadLine()

					If s IsNot Nothing Then
						'info example: LINE_COUNT=5
						info = s.Split("="C)

						For i As Integer = 0 To Integer.Parse(info(1)) - 1
							'format : name=line_3_1|c=2130640896
							s = reader.ReadLine()

							Dim v = s.Split("|"C)
							'name=line_3_1
							Dim v0 = v(0).Split("="C)
							Dim vv0 = v0(1).Split("_"C)

							'c=2130640896
							Dim v1 = v(1).Split("="C)

							AddLine(Integer.Parse(vv0(1)), Integer.Parse(vv0(2)), Integer.Parse(v1(1)))
						Next


					End If
				End Using

				'    this.panel1.Visible = false;
				If File.Exists(openFileDialog1.FileName & ".jpg") Then
					Using tempImage as Bitmap = New Bitmap(openFileDialog1.FileName & ".jpg")
						panel1.BackgroundImage = New Bitmap(tempImage)
					End Using
				End If


				'resize

					'   this.panel1.Visible = true;
				updateCamPosAfterResize()

			Catch
			End Try
		End If

	End Sub


	Private Sub updateCamPosAfterResize()
		Dim newarea As Integer = getNumPixelforImageDisplayed()


		For i As Integer = 0 To ctrllist.Count - 1
			Dim info = ctrllist(i).Tag.ToString().Split(","C)
			Dim dx As Single = Single.Parse(info(0))
			Dim dy As Single = Single.Parse(info(1))
			Dim area As Integer = Integer.Parse(info(2))
			Dim ratio As Single = CSng(Math.Sqrt(CSng(newarea) / area))
			Dim newdx As Single = ratio * dx
			Dim newdy As Single = ratio * dy

			ctrllist(i).Left = CInt(panel1.Width \ 2 + newdx - ctrllist(i).Width / 2)


			ctrllist(i).Top = CInt(panel1.Height \ 2 + newdy - ctrllist(i).Height / 2)
		Next

		For i As Integer = 0 To ctrllist.Count - 1
			Dim camindex As Integer = Integer.Parse(ctrllist(i).Name.Substring(3))
			linelist = getLines(camindex)
			For j As Integer = 0 To linelist.Count - 1
				Dim ctrl1 = linelist(j)

				setLineJoiningCams(ctrl1)

			Next
		Next



	End Sub

	Private Sub Form3_Resize(sender As Object, e As EventArgs)

		Me.panel1.Visible = False

		panel1.Height = Me.ClientSize.Height - (3 * panel1.Top \ 2)
		panel1.Width = Me.ClientSize.Width - 2 * panel1.Left
		label2.Top = panel1.Top + panel1.Height + 2
		updateCamPosAfterResize()

		Me.panel1.Visible = True

	End Sub

	Private Sub panel1_MouseEnter(sender As Object, e As EventArgs)
		Cursor = Cursors.[Default]
	End Sub





End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
Coder. Hacker. Fixer.

Comments and Discussions