Dvlnblk

In C# how can I get the x and y positions of the mouse and also of an object on the stage say a box called myBox

Re: Windows Presentation Foundation (WPF) X Y position

Drew Marsh

Call Mouse.GetPosition to get the current X,Y of the mouse.

Your second question doesn't have a direct answer in the world of WPF. Since you used the term "stage" I'll assume you're coming from the world of Flash. In WPF there are many different ways that elements can be layed out on the screen. These layouts are provided by various Panel implementations and X,Y coordinates are only apply when using a Canvas. You should read more about the layout system here in the SDK. Ultimately you can get the screen X, Y coordinates of a Visual in WPF, but I'd rather you get an understanding of how WPF works so you can do things the WPF way rather than go down the wrong avenue.

HTH,
Drew





Re: Windows Presentation Foundation (WPF) X Y position

Dvlnblk

Thanks Drew. Yeah, I am coming from Flash. But I have been doing WPF for around 8 months now and I know the layout system, like when to use a Stackpanel vs a WrapPanel vs a Canvas, how to use a Border, a Grid and a GridSplitter. I just want to be able to "import" them into my Class as a Framework UI Element and then perform something on them. Will the layout SDK help me with that Thanks very much for your time and help.



Re: Windows Presentation Foundation (WPF) X Y position

JCRobinson

Are you trying to rotate or move the object by moving the mouse





Re: Windows Presentation Foundation (WPF) X Y position

Dvlnblk

Actually, I am trying to resize an object when two mice are placed on each side of a rectangle and spread apart. For right now I am just faking the second mouse.

Here is what I have thus far:

// this is the point where the mouse is in relation to the rectangle

private Point _point = new Point();

// this is a fake var for now and eventually will be replaced with a real second mouse...don't ask how I am going to do that for NDA reasons

private Point _secondMouse = new Point(450.0,450.0);

// I want this var to be the difference between mouse1 (the real mouse) and _secondMouse (the fake mouse)

private Double _mouseDiff = 0.0;

When the rectangle is clicked on I run a DispatchTimer handler every 100 ms

void resizeVideo(object sender, EventArgs e)

{

// gets the mouse position (works great)

_point = Mouse.GetPosition(this);

// trying to calculate the difference and Print it ERROR:

_mouseDiff = _point as double + _secondMouse as double;

Debug.Print(_mouseDiff.ToString());

}

It is a casting error:

Error 2 Cannot convert type 'System.Windows.Point' to 'double' C:\Projects\Milan\MilanVideoControl_0_0_1\MilanVideoControl_0_0_1\REZN8_MilanVideoControl.cs 73 26 MilanVideoControl_0_0_1

Any ideas

PS: If you work at MS and you look at my naming convention in the error you can probably figure out how I am going to have two mouseEvents available to me soon Wink





Re: Windows Presentation Foundation (WPF) X Y position

JDPeckham

It is a casting error:

Error 2 Cannot convert type 'System.Windows.Point' to 'double' C:\Projects\Milan\MilanVideoControl_0_0_1\MilanVideoControl_0_0_1\REZN8_MilanVideoControl.cs 73 26 MilanVideoControl_0_0_1

as the error message says, you can't cast a point to a double... you need to get the x or y position and cast -that- to a double. (if it isn't already)






Re: Windows Presentation Foundation (WPF) X Y position

Dvlnblk

Can you possibly tell me how to do that Thanks for the help.



Re: Windows Presentation Foundation (WPF) X Y position

WPCoder

I assume this has been answered already here:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1664297&SiteID=1