From: Webbiz on
[Beginner C# Programmer]

Say you have an ovalShape control on a form.

When you move the mouse around the form, you can easily read the
form's X and Y coordinates from the MouseEventsArgs e.Location.

However, when the mouse moves into the ovalShape control area, it now
gets the coordinates from within that control, not the Form's
coordinates.

Now I suppose you can do a little math to get the form's relative
coordinates, but I was wondering if there was already a method built
into .Net that gives you the Form's coordinates from anywhere on the
mouse happens to be, inside or outside of an object.

Is there such a thing?

If not, what is the best way to do the math on this?

Yes, I'm newbie C# guy. Trying to move beyond my VB6 world.

Thanks.

Webbiz
From: Jeff Johnson on
"Webbiz" <nospam(a)noway.com> wrote in message
news:lfm8v5p36dfjaotm3o32k77turad5gkb5o(a)4ax.com...

> [Beginner C# Programmer]
>
> Say you have an ovalShape control on a form.
>
> When you move the mouse around the form, you can easily read the
> form's X and Y coordinates from the MouseEventsArgs e.Location.
>
> However, when the mouse moves into the ovalShape control area, it now
> gets the coordinates from within that control, not the Form's
> coordinates.
>
> Now I suppose you can do a little math to get the form's relative
> coordinates, but I was wondering if there was already a method built
> into .Net that gives you the Form's coordinates from anywhere on the
> mouse happens to be, inside or outside of an object.
>
> Is there such a thing?
>
> If not, what is the best way to do the math on this?
>
> Yes, I'm newbie C# guy. Trying to move beyond my VB6 world.

The Control class (and therefore anything that inherits from it) has
PointToScreen() and PointToClient() methods. However, from a cursory glance
at the methods on properties of Control and Form, I don't see a good way of
determining where inside the complete bounds of a Form it is possible to
determine exactly where the client area begins. The Control.ClientRectangle
property states that its top left coords are always (0, 0), although we know
that could translate to (4, 34) when considering the entire form. (And I'm
making the assumption that you want the coordinates relative to the client
area of the Form only, not to the entire Form--including the title bar,
menus, etc.--itself.) So I guess the answer is: I dunno. I must be missing
something, as this shouldn't be so hard.


From: Webbiz on
On Wed, 19 May 2010 18:45:52 -0400, "Jeff Johnson" <i.get(a)enough.spam>
wrote:

>"Webbiz" <nospam(a)noway.com> wrote in message
>news:lfm8v5p36dfjaotm3o32k77turad5gkb5o(a)4ax.com...
>
>> [Beginner C# Programmer]
>>
>> Say you have an ovalShape control on a form.
>>
>> When you move the mouse around the form, you can easily read the
>> form's X and Y coordinates from the MouseEventsArgs e.Location.
>>
>> However, when the mouse moves into the ovalShape control area, it now
>> gets the coordinates from within that control, not the Form's
>> coordinates.
>>
>> Now I suppose you can do a little math to get the form's relative
>> coordinates, but I was wondering if there was already a method built
>> into .Net that gives you the Form's coordinates from anywhere on the
>> mouse happens to be, inside or outside of an object.
>>
>> Is there such a thing?
>>
>> If not, what is the best way to do the math on this?
>>
>> Yes, I'm newbie C# guy. Trying to move beyond my VB6 world.
>
>The Control class (and therefore anything that inherits from it) has
>PointToScreen() and PointToClient() methods. However, from a cursory glance
>at the methods on properties of Control and Form, I don't see a good way of
>determining where inside the complete bounds of a Form it is possible to
>determine exactly where the client area begins. The Control.ClientRectangle
>property states that its top left coords are always (0, 0), although we know
>that could translate to (4, 34) when considering the entire form. (And I'm
>making the assumption that you want the coordinates relative to the client
>area of the Form only, not to the entire Form--including the title bar,
>menus, etc.--itself.) So I guess the answer is: I dunno. I must be missing
>something, as this shouldn't be so hard.
>


Yes, the client area is what I'm looking for.

You see, I have drawn a circle on the form using the ovalShape
control. From the center of this circle I have drawn a line outwords
just beyond the circumference of the circle.

My code works fine for moving the line around and around the circle
while anchored to the center like a Radar screen, but only by having
the Form1_MouseMove detecting it. That is, when the mouse is outside
the circle.

But when the mouse is inside the circle, it falls to the
ovalShape1_MouseMove event itself. Unfortunately, I cannot use the
same code that is inside the Form1_Mousemove because the coordinates
are different.

I figured the easy way to solve this would be if there was a method
that provides client area coordinates of the form NO MATTER if the
mouse is over a control or not.

From your response it seems that perhaps I was being too hopeful?

Thanks.

Webbiz

From: Peter Duniho on
Webbiz wrote:
> [...]
> I figured the easy way to solve this would be if there was a method
> that provides client area coordinates of the form NO MATTER if the
> mouse is over a control or not.
>
> From your response it seems that perhaps I was being too hopeful?

The mouse coordinates for the MouseMove event will always be client
coordinates for the control that is raising the event (i.e. the one to
which the "mouse move" window message was sent).

If you write the code to handle the event by always taking as the input
the screen coordinates for the mouse location instead, and then
immediately translating those coordinates to the form's client
coordinates, then that will normalize the coordinates so that they are
always relative to the form. If you have some other control you'd
rather have them relative to, you can use that one instead of the form.

See the PointToScreen() and PointToClient() methods that Jeff mentioned.
You'll use the first method in any control that might get a MouseMove
event you're interested in, and you'll use the second method in some
method that is called by each of your MouseMove handlers.

Of course, to the extent that the issue is being caused by your use of
actual controls just to do something simple like draw a circle or line,
one alternative is to just not do that, and draw directly to the form by
overriding the OnPaint() method in the form (or subscribing to the Paint
event).

Pete
From: Webbiz on
On Thu, 20 May 2010 00:12:36 -0700, Peter Duniho
<no.peted.spam(a)no.nwlink.spam.com> wrote:

>Webbiz wrote:
>> [...]
>> I figured the easy way to solve this would be if there was a method
>> that provides client area coordinates of the form NO MATTER if the
>> mouse is over a control or not.
>>
>> From your response it seems that perhaps I was being too hopeful?
>
>The mouse coordinates for the MouseMove event will always be client
>coordinates for the control that is raising the event (i.e. the one to
>which the "mouse move" window message was sent).
>
>If you write the code to handle the event by always taking as the input
>the screen coordinates for the mouse location instead, and then
>immediately translating those coordinates to the form's client
>coordinates, then that will normalize the coordinates so that they are
>always relative to the form. If you have some other control you'd
>rather have them relative to, you can use that one instead of the form.
>
>See the PointToScreen() and PointToClient() methods that Jeff mentioned.
> You'll use the first method in any control that might get a MouseMove
>event you're interested in, and you'll use the second method in some
>method that is called by each of your MouseMove handlers.
>
>Of course, to the extent that the issue is being caused by your use of
>actual controls just to do something simple like draw a circle or line,
>one alternative is to just not do that, and draw directly to the form by
>overriding the OnPaint() method in the form (or subscribing to the Paint
>event).
>
>Pete


Thanks Pete. I guess I didn't understand Jeff's reference to P2S and
P2C.

The drawing of the circle is for test purposes. Actually, what will be
on the form is a 12 x 12 grid with drawn borders and sensitive to
mouse hover and clicks. If the user activates the line (we'll call it
the Radar line), then the grid/cells must not process the mouse hover
and the line then must take over, which is moved by knowing the
coordinates.

So I think under those circumstances I won't be able to simply draw
the pattern in the background. Otherwise, that would be a great way to
go (and force me to delve into the graphics end of .Net).

I'll look into the Point2...() methods you (and Jeff) mentioned and
see if I can get a handle on what you are referring to.

Thanks Pete (and Jeff)!

:-)

Webbiz