473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2622
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.