C
Colin McGuire
Hi, this is a really simple question I have been banging my head on a
brick wall over.
The program below changes the background colour of a form depending on
whether the cursor is inside a rectangle drawn on the form or not. It
works perfectly as shown below.
But it won't work if I change the values of scaleFactor, rotateFactor,
translateFactorX, translateFactorY in the program. I would like to
'correct' the values of e.X and e.Y in "OnMouseMove" so the program
works if I change scaleFactor, rotateFactor etc. I can't get it right
without using slow trigonometry and lots of if statements. I want to
use a matrix transformation similar to how I have done it with
"e.Graphics.ScaleTransform(scaleFactor, scaleFactor)" shown below -
but can't figure it out.
Please please please can someone help me out here.
Thank you in advance
Colin
Here is how to use this sourcecode
1. Launch VS2003
2. Select "New Project"
3. Select "Visual Basic Projects" and "Windows Application" and press
"OK"
4. Double click on "Form1" and paste in the following
Public Class Form1
Inherits System.Windows.Forms.Form
'[+] Windows Form Designer generated code
Dim rectOffX As Integer = 40 'rectangle offset on form
Dim rectOffY As Integer = 30
Dim scaleFactor As Integer = 1
Dim rotateFactor As Integer = 0
Dim translateFactorX As Integer = 0
Dim translateFactorY As Integer = 0
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim container As Drawing2D.GraphicsContainer =
e.Graphics.BeginContainer
Dim myOriginalMatrix As Drawing2D.Matrix =
e.Graphics.Transform()
e.Graphics.ScaleTransform(scaleFactor, scaleFactor)
e.Graphics.RotateTransform(rotateFactor)
e.Graphics.TranslateTransform(translatefactorX,
translateFactorY)
'drawing stuff in here
Dim drawRect As Rectangle = New Rectangle(rectOffX, rectOffY,
30, 10)
e.Graphics.DrawRectangle(New Pen(Color.Blue), drawRect)
e.Graphics.EndContainer(container)
e.Graphics.Transform = myOriginalMatrix
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
Dim cursorRect As Rectangle = New Rectangle(e.X, e.Y, 1, 1)
Dim drawRect As Rectangle = New Rectangle(rectOffX, rectOffY,
30, 10)
If drawRect.IntersectsWith(cursorRect) Then
Me.BackColor = Color.Yellow
Else
Me.BackColor = Color.Gray
End If
End Sub
End Class
brick wall over.
The program below changes the background colour of a form depending on
whether the cursor is inside a rectangle drawn on the form or not. It
works perfectly as shown below.
But it won't work if I change the values of scaleFactor, rotateFactor,
translateFactorX, translateFactorY in the program. I would like to
'correct' the values of e.X and e.Y in "OnMouseMove" so the program
works if I change scaleFactor, rotateFactor etc. I can't get it right
without using slow trigonometry and lots of if statements. I want to
use a matrix transformation similar to how I have done it with
"e.Graphics.ScaleTransform(scaleFactor, scaleFactor)" shown below -
but can't figure it out.
Please please please can someone help me out here.
Thank you in advance
Colin
Here is how to use this sourcecode
1. Launch VS2003
2. Select "New Project"
3. Select "Visual Basic Projects" and "Windows Application" and press
"OK"
4. Double click on "Form1" and paste in the following
Public Class Form1
Inherits System.Windows.Forms.Form
'[+] Windows Form Designer generated code
Dim rectOffX As Integer = 40 'rectangle offset on form
Dim rectOffY As Integer = 30
Dim scaleFactor As Integer = 1
Dim rotateFactor As Integer = 0
Dim translateFactorX As Integer = 0
Dim translateFactorY As Integer = 0
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim container As Drawing2D.GraphicsContainer =
e.Graphics.BeginContainer
Dim myOriginalMatrix As Drawing2D.Matrix =
e.Graphics.Transform()
e.Graphics.ScaleTransform(scaleFactor, scaleFactor)
e.Graphics.RotateTransform(rotateFactor)
e.Graphics.TranslateTransform(translatefactorX,
translateFactorY)
'drawing stuff in here
Dim drawRect As Rectangle = New Rectangle(rectOffX, rectOffY,
30, 10)
e.Graphics.DrawRectangle(New Pen(Color.Blue), drawRect)
e.Graphics.EndContainer(container)
e.Graphics.Transform = myOriginalMatrix
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
Dim cursorRect As Rectangle = New Rectangle(e.X, e.Y, 1, 1)
Dim drawRect As Rectangle = New Rectangle(rectOffX, rectOffY,
30, 10)
If drawRect.IntersectsWith(cursorRect) Then
Me.BackColor = Color.Yellow
Else
Me.BackColor = Color.Gray
End If
End Sub
End Class