473,505 Members | 13,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 2630
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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3570
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
2
1927
by: MyNameIsnt | last post by:
Can anyone tell me why, when I click on the buttons it register 2 characters on the display? if you use the right mousebutton it works ok, but the buttons dont flash?? it works fine without the...
0
2603
by: Martin Streller | last post by:
Hello, The code below represents a simple ownerdrawn, Listview class in C#. Its purpose is to avoid the flicker of the MS ListView. So I can't fall back to their one. Does anybody know why I...
2
4898
by: Serge Klokov | last post by:
Hi! 1. Please, help with example "paint on form by mouse" 2. Below is my example, but it clear the line after each Refresh()... how to fix? 3. How to draw the line in Mouse_Move event? ...
2
1152
by: Marcelo | last post by:
Greetings! I develop a multiple forms application on Microsoft Visual Studio C++ ..NET 2003. In this application, I draw into a picture box using the Invalidate() method a very "heavy" graphic...
22
2330
by: Colin McGuire | last post by:
I apologize for posting yet another scrollbar question. Here is my code. All I want is for a diagonal line to appear from coordinates (0,0) to (width,height) in a usercontrol regardless of whether...
0
353
by: Colin McGuire | last post by:
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...
0
1892
by: Brian Henry | last post by:
Ok I've never implemented a snap location before so I dont really know what im doing wrong here... anyways, I am making a custom slider control that takes dates as its values instead of integers......
4
26935
by: RobinS | last post by:
I am drawing a rectangle on a picture that has already been drawn on the graphics area (a user control). It works something like this: //in the MouseDown event m_isDragging = true; m_oldX =...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.