Click here to Skip to main content
15,896,063 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.7K   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.Drawing.Drawing2D
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms


Public Partial Class Form4
	Inherits Form
	Private theta As Double = Math.PI / 10
	Private theta2 As Double = 0
	Private bReadyToAutoSwitchPhoto As Boolean = False

	Private photoNo As Integer = 0
	Private maxNumPhoto As Integer = 8

	Private x0 As Integer = 0
	Private y0 As Integer = 0
	Private dist As Integer = 0
	Private dist2 As Integer = 0
	Public Sub New()
		InitializeComponent()
		nextPhoto()
		' this.Size = shape1.Size;
		shape1.Left = 0
		shape1.Top = 0
		shape1.Text = vbLf & vbLf & vbLf & vbLf & vbLf & vbLf & vbLf & vbLf & "Left Click: Start/Stop" & vbLf & "Right Click: Next Photo" & vbLf & "Dbl Click: Close"
		x0 = shape1.Left + shape1.Width / 2
		y0 = shape1.Top + shape1.Height / 2

		dist = line1.Width - 2 * line1.BorderWidth
		dist2 = line2.Width - 2 * line2.BorderWidth

		Dim x1 As Integer = CInt(Math.Truncate(dist * Math.Cos(theta) + x0))
		Dim y1 As Integer = CInt(Math.Truncate(dist * Math.Sin(theta) + y0))
		ShapeControl.Line.setLine(line1, x0, y0, x1, y1)

		Dim x2 As Integer = CInt(Math.Truncate(dist2 * Math.Cos(theta2) + x0))
		Dim y2 As Integer = CInt(Math.Truncate(dist2 * Math.Sin(theta2) + y0))
		ShapeControl.Line.setLine(line2, x2, y2, x0, y0)
		Me.panel1.Region = shape1.Region



		Me.Region = shape1.Region
	End Sub



	Private Function RadianToDegree(angle As Double) As Double
		Return angle * (180.0 / Math.PI)
	End Function

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

		theta += (Math.PI / 180.0) / 3.0
		If theta > (2 * Math.PI) Then
			theta -= (2.0 * Math.PI)
		End If

		Dim x1 As Integer = CInt(Math.Truncate(dist * Math.Cos(theta) + x0))
		Dim y1 As Integer = CInt(Math.Truncate(dist * Math.Sin(theta) + y0))
		ShapeControl.Line.setLine(line1, x0, y0, x1, y1)

		Dim mindelta As Double = 2
		Dim maxdelta As Double = 178

		Dim deltaangle As Integer = CInt(Math.Truncate(RadianToDegree(theta) - RadianToDegree(theta2)))
		If deltaangle < 0 Then
			deltaangle += 180
		End If
		If bReadyToAutoSwitchPhoto Then
			If deltaangle < mindelta Then
				nextPhoto()
				bReadyToAutoSwitchPhoto = False
			End If
		End If

		If deltaangle > maxdelta Then
			bReadyToAutoSwitchPhoto = True
		End If


	End Sub



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

		timer1.Enabled = Not timer1.Enabled
		timer2.Enabled = Not timer2.Enabled
	End Sub



	Private Sub timer2_Tick(sender As Object, e As EventArgs)
		theta2 -= (Math.PI / 180.0) / 2.5

		' if (theta2 > (2 * Math.PI)) theta2 -= (2 * Math.PI);
		If theta2 < 0 Then
			theta2 += (2.0 * Math.PI)
		End If
		Dim x2 As Integer = CInt(Math.Truncate(dist2 * Math.Cos(theta2) + x0))
		Dim y2 As Integer = CInt(Math.Truncate(dist2 * Math.Sin(theta2) + y0))
		ShapeControl.Line.setLine(line2, x2, y2, x0, y0)

	End Sub



	Private Sub nextPhoto()


        Dim o As Object = Global.TestShapeControl.Resources.ResourceManager.GetObject("photo" & photoNo)
		panel1.BackgroundImage = DirectCast(o, Bitmap)
		photoNo = (System.Threading.Interlocked.Increment(photoNo)) Mod maxNumPhoto
	End Sub

	Private Sub shape1_MouseDown(sender As Object, e As MouseEventArgs)
		If e.Clicks > 1 Then
			Return
		End If
		If e.Button = MouseButtons.Left Then
			shape1_Click(Nothing, Nothing)
		End If
		If e.Button = MouseButtons.Right Then
			nextPhoto()
		End If
	End Sub

	Private Sub shape1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
		If e.Button = MouseButtons.Left Then
			Me.Close()
		End If
	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