473,569 Members | 2,526 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,
translateFactor X, translateFactor Y in the program. I would like to
'correct' the values of e.X and e.Y in "OnMouseMov e" 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.Sca leTransform(sca leFactor, 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 translateFactor X As Integer = 0
Dim translateFactor Y As Integer = 0

Protected Overrides Sub OnPaint(ByVal e As
System.Windows. Forms.PaintEven tArgs)
MyBase.OnPaint( e)
Dim container As Drawing2D.Graph icsContainer =
e.Graphics.Begi nContainer
Dim myOriginalMatri x As Drawing2D.Matri x =
e.Graphics.Tran sform()
e.Graphics.Scal eTransform(scal eFactor, scaleFactor)
e.Graphics.Rota teTransform(rot ateFactor)
e.Graphics.Tran slateTransform( translatefactor X,
translateFactor Y)

'drawing stuff in here
Dim drawRect As Rectangle = New Rectangle(rectO ffX, rectOffY,
30, 10)
e.Graphics.Draw Rectangle(New Pen(Color.Blue) , drawRect)
e.Graphics.EndC ontainer(contai ner)
e.Graphics.Tran sform = myOriginalMatri x
End Sub

Protected Overrides Sub OnMouseMove(ByV al e As
System.Windows. Forms.MouseEven tArgs)
MyBase.OnMouseM ove(e)
Dim cursorRect As Rectangle = New Rectangle(e.X, e.Y, 1, 1)
Dim drawRect As Rectangle = New Rectangle(rectO ffX, rectOffY,
30, 10)
If drawRect.Inters ectsWith(cursor Rect) Then
Me.BackColor = Color.Yellow
Else
Me.BackColor = Color.Gray
End If
End Sub
End Class
Nov 20 '05 #1
4 2651
What kind of object is your rectangle.

Simple answer, see HitTest


"Colin McGuire" <co***********@ lycos.co.uk> wrote in message
news:ab******** *************** ***@posting.goo gle.com...
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,
translateFactor X, translateFactor Y in the program. I would like to
'correct' the values of e.X and e.Y in "OnMouseMov e" 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.Sca leTransform(sca leFactor, 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 translateFactor X As Integer = 0
Dim translateFactor Y As Integer = 0

Protected Overrides Sub OnPaint(ByVal e As
System.Windows. Forms.PaintEven tArgs)
MyBase.OnPaint( e)
Dim container As Drawing2D.Graph icsContainer =
e.Graphics.Begi nContainer
Dim myOriginalMatri x As Drawing2D.Matri x =
e.Graphics.Tran sform()
e.Graphics.Scal eTransform(scal eFactor, scaleFactor)
e.Graphics.Rota teTransform(rot ateFactor)
e.Graphics.Tran slateTransform( translatefactor X,
translateFactor Y)

'drawing stuff in here
Dim drawRect As Rectangle = New Rectangle(rectO ffX, rectOffY,
30, 10)
e.Graphics.Draw Rectangle(New Pen(Color.Blue) , drawRect)
e.Graphics.EndC ontainer(contai ner)
e.Graphics.Tran sform = myOriginalMatri x
End Sub

Protected Overrides Sub OnMouseMove(ByV al e As
System.Windows. Forms.MouseEven tArgs)
MyBase.OnMouseM ove(e)
Dim cursorRect As Rectangle = New Rectangle(e.X, e.Y, 1, 1)
Dim drawRect As Rectangle = New Rectangle(rectO ffX, rectOffY,
30, 10)
If drawRect.Inters ectsWith(cursor Rect) 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.goo gle.com...
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,
translateFactor X, translateFactor Y in the program. I would like to
'correct' the values of e.X and e.Y in "OnMouseMov e" 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.Sca leTransform(sca leFactor, 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 translateFactor X As Integer = 0
Dim translateFactor Y As Integer = 0

Protected Overrides Sub OnPaint(ByVal e As
System.Windows. Forms.PaintEven tArgs)
MyBase.OnPaint( e)
Dim container As Drawing2D.Graph icsContainer =
e.Graphics.Begi nContainer
Dim myOriginalMatri x As Drawing2D.Matri x =
e.Graphics.Tran sform()
e.Graphics.Scal eTransform(scal eFactor, scaleFactor)
e.Graphics.Rota teTransform(rot ateFactor)
e.Graphics.Tran slateTransform( translatefactor X,
translateFactor Y)

'drawing stuff in here
Dim drawRect As Rectangle = New Rectangle(rectO ffX, rectOffY,
30, 10)
e.Graphics.Draw Rectangle(New Pen(Color.Blue) , drawRect)
e.Graphics.EndC ontainer(contai ner)
e.Graphics.Tran sform = myOriginalMatri x
End Sub

Protected Overrides Sub OnMouseMove(ByV al e As
System.Windows. Forms.MouseEven tArgs)
MyBase.OnMouseM ove(e)
Dim cursorRect As Rectangle = New Rectangle(e.X, e.Y, 1, 1)
Dim drawRect As Rectangle = New Rectangle(rectO ffX, rectOffY,
30, 10)
If drawRect.Inters ectsWith(cursor Rect) Then
Me.BackColor = Color.Yellow
Else
Me.BackColor = Color.Gray
End If
End Sub
End Class

Nov 20 '05 #3
"CJ Taylor" <no****@blowgoa ts.com> wrote in message news:<10******* ******@corp.sup ernews.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****@blowgoa ts.com> wrote in message news:<10******* ******@corp.sup ernews.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
3584
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> </head> <style type="text/css">
2
1933
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 aqua buttons, (using just windows forms buttons... Main Form cs file...
0
2607
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 cannot change the size of the ListView's font (ListView.Font) from e.g.
2
4902
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? private Boolean isCanPaint = false; private MouseEventArgs MouseEvent1;
2
1160
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 (composed of lots of elements). What I need to do is, on each MouseMove event of the picture box, draw a rectangle in front of this graphic in...
22
2338
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 the user autoscrolls the usercontrol (other things that are on the usercontrol I want to scroll, but haven't included any here). Here are the...
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 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,...
0
1898
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... then taking that date range and finding dates specifiec between them (in a list of dates) and putting snap marks, so if you slide it near one of...
4
26963
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 = e.X; //save original positions m_oldY = e.Y; //in the MouseMove event if (m_isDragging)
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.