By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,460 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

help with Form1 program - drawing rectangle and background colouring on mousemove

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
What kind of object is your rectangle.

Simple answer, see HitTest


"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
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

Nov 20 '05 #2

P: n/a
What kind of object is your rectangle.

Simple answer, see HitTest


"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
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

Nov 20 '05 #3

P: n/a
"CJ Taylor" <no****@blowgoats.com> wrote in message news:<10*************@corp.supernews.com>...
What kind of object is your rectangle.

Simple answer, see HitTest


The rectangle is not an object, it is just painted on the form (see
code in original posting).
Colin
Nov 20 '05 #4

P: n/a
"CJ Taylor" <no****@blowgoats.com> wrote in message news:<10*************@corp.supernews.com>...
What kind of object is your rectangle.

Simple answer, see HitTest


The rectangle is not an object, it is just painted on the form (see
code in original posting).
Colin
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.