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


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

Here is how to use this sourcecode

1. Launch VS2003
2. Select "New Project"
3. Select "Visual Basic Projects" and "Windows Application" and press
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
Dim container As Drawing2D.GraphicsContainer =
Dim myOriginalMatrix As Drawing2D.Matrix =
e.Graphics.ScaleTransform(scaleFactor, scaleFactor)

'drawing stuff in here
Dim drawRect As Rectangle = New Rectangle(rectOffX, rectOffY,
30, 10)
e.Graphics.DrawRectangle(New Pen(Color.Blue), drawRect)
e.Graphics.Transform = myOriginalMatrix
End Sub

Protected Overrides Sub OnMouseMove(ByVal e As
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
Me.BackColor = Color.Gray
End If
End Sub
End Class

Colin McGuire

CJ Taylor said:
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).

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question