run smooth

jawadh90

Member
Joined
Jun 19, 2003
Messages
18
Graphics gurus, i need your help here. this is a copy of my project and i was wondering how i could make this needle move smooth.
and make needle stop at a certain point when a user clicks on the form. thankx

Imports System.Drawing.Drawing2D
Imports System.Math
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
setstyle(ControlStyles.ResizeRedraw, True)
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Form Designer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents tmrRotate As System.Windows.Forms.Timer
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.tmrRotate = New System.Windows.Forms.Timer(Me.components)
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Bitmap)
Me.PictureBox1.Location = New System.Drawing.Point(8, 40)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(80, 16)
Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
Me.tmrRotate.Interval = 20
Me.Button1.Location = New System.Drawing.Point(8, 8)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Stop Timer"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(592, 573)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.PictureBox1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim g As Graphics
Dim theta As Single = -1
Dim angle As Double
Dim sections(4) As Rectangle
Dim bmpNeedle As Bitmap
Private m_SourceBm As Bitmap
Private m_SourceWid As Integer
Private m_SourceHgt As Integer
Private m_SourceCx As Single
Private m_SourceCy As Single
Private m_SourceCorners As PointF()
Private m_DestWid As Integer
Private m_DestHgt As Integer
Private m_DestCx As Single
Private m_DestCy As Single
Private m_DestBm As Bitmap
Private m_DestBackColor As Color
Private Sub tmrRotate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRotate.Tick
Static angle As Single = 0
Dim AngStep As Single = 5
If theta > 2 * PI Then
theta -= 2 * PI
End If
If theta = angle Then
tmrRotate.Enabled = False
ElseIf angle > theta Then
angle = angle - (AngStep * PI / 180.0)
ElseIf angle < theta Then
angle = angle + (AngStep * PI / 180.0)
End If
Dim corners() As PointF = m_SourceCorners
ReDim Preserve corners(2)
Dim sin_theta As Single = Sin(angle)
Dim cos_theta As Single = Cos(angle)
Dim CorX As Single
Dim CorY As Single
Dim i As Long
For i = 0 To 2
CorX = corners(i).X
CorY = corners(i).Y
corners(i).X = CorX * cos_theta + CorY * sin_theta
corners(i).Y = -CorX * sin_theta + CorY * cos_theta
Next i
For i = 0 To 2
corners(i).X += m_DestCx
corners(i).Y += m_DestCy
Next i
Dim gr_out As Graphics = Graphics.FromImage(m_DestBm)
gr_out.Clear(m_DestBackColor)
gr_out.DrawImage(m_SourceBm, corners)
g = Me.CreateGraphics
Me.Invalidate()
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
Dim a As Double
Dim b As Double
Dim c As Double
Dim myPen As New Pen(Color.Red, 2)
Dim flip As Integer
Dim point1, point2, point3 As Point
point1 = New Point(Me.Width / 2, Me.Height / 2)
point3 = New Point(point1.X + m_SourceBm.Width, point1.Y)
If e.X >= Me.Width / 2 And e.X <= Me.Width And e.Y >= 0 And e.Y <= Me.Height / 2 Then
point2 = New Point(e.X - point1.X, point1.Y - e.Y)
flip = +1
ElseIf e.X >= 0 And e.X <= Me.Width / 2 And e.Y >= 0 And e.Y <= Me.Height / 2 Then
point2 = New Point(e.X - point1.X, point1.Y - e.Y)
flip = +1
ElseIf e.X >= 0 And e.X <= Me.Width / 2 And e.Y >= Me.Height / 2 And e.Y <= Me.Height Then
point2 = New Point(e.X - point1.X, point1.Y - e.Y)
flip = -1
ElseIf e.X >= Me.Width / 2 And e.X <= Me.Width And e.Y >= Me.Height / 2 And e.Y <= Me.Height Then
point2 = New Point(e.X - point1.X, point1.Y - e.Y)
flip = -1
End If
a = Math.Sqrt(Math.Pow(point3.X - point1.X, 2) + Math.Pow(point3.Y - point1.Y, 2))
b = Math.Sqrt(Math.Pow(e.X - point1.X, 2) + Math.Pow(e.Y - point1.Y, 2))
c = Math.Sqrt(Math.Pow(e.X - point3.X, 2) + Math.Pow(e.Y - point3.Y, 2))
theta = Math.Acos((Math.Pow(a, 2) + Math.Pow(b, 2) - Math.Pow(c, 2)) / (2 * a * b))
theta = (theta / Math.PI) * 180
theta += 180
theta = flip * theta
theta = theta * PI / 180.0
MessageBox.Show(theta.ToString)
tmrRotate.Enabled = True
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim myPen As New Pen(Color.Red, 2)
e.Graphics.DrawImage(New Bitmap("gauge1.gif"), 0, 0, Me.Width, Me.Height)
e.Graphics.DrawImage(m_DestBm, CInt((Me.Width / 2) - (m_DestBm.Width / 2)), CInt((Me.Height / 2) - (m_DestBm.Height / 2)))
setStyle(ControlStyles.DoubleBuffer, True)
setStyle(ControlStyles.AllPaintingInWmPaint, True)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
m_SourceBm = New Bitmap(PictureBox1.Image)
m_SourceWid = m_SourceBm.Width
m_SourceHgt = m_SourceBm.Height
m_SourceCx = m_SourceWid / 2
m_SourceCy = m_SourceHgt / 2
m_SourceCorners = New PointF() { _
New PointF(0, 0), _
New PointF(m_SourceWid, 0), _
New PointF(0, m_SourceHgt), _
New PointF(m_SourceWid, m_SourceHgt)}
m_DestWid = Sqrt(m_SourceWid * m_SourceWid + m_SourceHgt * m_SourceHgt)
m_DestHgt = m_DestWid
m_DestCx = m_DestWid
m_DestCy = m_DestHgt
m_DestBm = New Bitmap(m_DestWid * 2, m_DestHgt * 2)
Dim i As Long
For i = 0 To 3
m_SourceCorners(i).X -= m_SourceCx
m_SourceCorners(i).Y -= m_SourceCy
Next i
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tmrRotate.Enabled = False
End Sub
End Class
 
Im not a graph guru but one thing alot of ppl say to do is add this
Code:
SetStyle(ControlStyles.DoubleBuffer, True) to reduce flicker if there will be any
i dont know if it will help u or not but i thought id let u see
 
Can you post your project via a zip file? Dont include the EXE please.

The code wont run as you didnt include any of the graphics and some are (apparently) embedded and some are loaded from disk. Having the whole project is essential to debugging in this case.

-ner
 
Back
Top