Position WPF Window relative to the calling button - wpf

I have a button which opens a new Window. How can I position the new Window relative to that button so that the Window right side is aligned with the button right side and Window top is aligned with the button bottom?
I tried to calculate the positions like this in the calling window, first of all to try make them aligned relative to the calling window itself but it seems not to be correct.
Dim SecondWin As New SecondWindow()
SecondWin.Top = Me.Top + Me.ActualHeight - SecondWin.Height
SecondWin.Left = Me.Left + Me.ActualWidth - SecondWin.Width
SecondWin.Show()

This sounds like a job the Popup control is made for. SO related

Related

WPF TouchDevice get absolute coordinates

I try to create a window, that can be moves or resized via multi-touch gestures. I tried it this way. I captures the TouchDown-Event of the window and saved all active TouchDevices in a List, to know which TouchDevices are active. I catch updated and deactivated event of the TouchDevices to know when they are moved and when they are deactivated. I save the Left and Top Property of the Window and the position where the TouchDevice started and everytime the Updated event is called I move the Window to the new Position relative to the new position of the TouchDevice. This works if I move the finger. But if I don't move the finger (or just very little) the window suddenly began shaking (moving chaotically) and then soon disappears to a position outside of the screen.
I think the problem here is, that the function "GetTouchPoint" of the TouchDevice only give relative coordinates related to the window (even if I set the parameter null instead if the window reference). And because the Window moves the relative position of the TouchDevice (that doesn't move) changes too. So I did a research but wasn't able to find a way to determine the screen coordinates of the touch device.
So I hope anyone can help me how to get the absolute coordinates of the TouchDevice. Or can help me find another way to "DragMove" the window with touch.(I tried DragMove, but that only works for mouse clicks, not TouchDowns) Also I like to resize the window when two Touch Devices are active and therefor I also need absolute coordinates because otherwise same effect happens.
I ran into this issue because my taskbar is on the right edge of the screen, effectively pushing the maximized window to the right. This issue also arises when the application's window is not maximized, and floating somewhere on the screen.
Here is an extension method that fixes the coordinates based on the application window's position.
public static Point FixCoordinates(this Point point)
{
var left = Application.Current.MainWindow.Left;
var top = Application.Current.MainWindow.Top;
return new Point(point.X + left, point.Y + top);
}
You may want to pass in a window that hosts your touched control as a parameter. In my case it is application's main window.
Also, since you tagged the question with "multi-touch", here is a method which averages multiple touch coordinates:
public static Point GetAverage(this IEnumerable<Point> points)
{
var averageX = points.Average(p => p.X);
var averageY = points.Average(p => p.Y);
return new Point(averageX, averageY);
}
And I use it in the code like this:
private void TouchAdornment_TouchDown(object sender, TouchEventArgs e)
{
var touchPosition = (sender as UIElement).TouchesOver.Select(t => t.GetTouchPoint(null).Position).GetAverage().FixCoordinates();
}

Get Mouse Position on Canvas (But NOT on window)?

I have a project in WPF 4 and vb.net 2010.
I have a canvas inside a window. The window is full screen, but the canvas is set to a solid 640x480 in the center of the window. I need to get the mouse position inside of the canvas, but NOT inside of the window. How do I do this?
Doesn't this work?
Point p = Mouse.GetPosition(canvas);
The position of the mouse pointer is
calculated relative to the specified
element with the upper-left corner of
element being the point of origin,
Hi the important thing is the
NOT on the Window
the canvas is part of the window as well.
one example:
the Window.AllowsTransparency state is on true
the Window.Background is #00000000 (completely transparent)
the Window.Style is None
the Window.State is Maximized and
there are NO controls or elements on the window!
...
so if you start the application you will see Nothing
now tell me how to get the mouseposition on the screen in pixel
!Warning!
if you juse Mouse.GetPosition(this); it will return x0 y0 every time
so I solved the Problem by using System.Windows.Forms.Control.MousePosition it's a bit a mix of wpf and Windows.Forms but I've given up xD.
Sorry for yelling :/
To make it easy for me I made a Extension:
<DebuggerHidden> _
<System.Runtime.CompilerServices.Extension> _
Public Function toWfpPoint(p As System.Drawing.Point) As Point
Return new Point(p.X, p.Y)
End Function
Now I just can juse it like this:
Dim MousPos As Point = System.Windows.Forms.Control.MousePosition.toWfpPoint

Silverlight - Possible to not rotate parent window overlay when ChildWindow rotates?

I've got a ChildWindow that rotates 180 degrees when I click a button.
I'm also using a ChildWindow.OverlayBrush to dim out the parent window.
This of course rotates as well when the ChildWindow rotates.
Is there anyway to dim out the parent window without it rotating with the ChildWindow?
2 options spring to mind:
You either need the OverlayBrush to
not be under the same element you
rotate (can you rotate a child
within the window instead?)
Or, you need to rotate the overlay
brush in the opposite direct in the
same animation so that it cancels
out the motion.

How to move WPF window above top of screen?

I have a window with the following:
Background="{x:Null}" AllowsTransparency="True" WindowStyle="None"
Dragging the window by hand beyond the left, right and bottom limits of the screen results in a predictably cropped window. However this behaviour is not the same for dragging it above the top limit. Instead of cropping, it pushes it back down as if there's an automatic If Window.Top < 0 Then Window.Top = 0.
This is probably in place so that users don't lose a Windows titlebar (which is the standard way to drag windows around, and losing sight of that effectively makes it undraggable). I don't need that as my entire window is draggable via Me.DragMove().
So, how do I let a window be dragged above the top limit of the screen?
(This is unrelated to Aero Snap which only occurs if the mouse touches the borders. I'm trying to move the window beyond visible bounds)
The DragMove function do not allow you to drag a window above the screen. You need to manually move the window, for example:
How do I move a wpf window into a negitive top value?

Making a moveable control in WPF

I have a panel, within that panel are several rectangular controls (the number of controls vaires) I want the user to be able to move the controls around within the panel so that they can arrange the controls in the way that suits them best. does anyone have any resources i could read or simple tips which would get me headed down the right road?
thanks
I figured out a possible, simple method of moving a control in a drag/move style... Here are the steps.
Select an element in your control which you wish to be the movement area. This is the area in which, if me user holds the mouse down, the control will move. In my case it was a rectangular border at the top of the control.
Use the OnMouseDown event to set a boolean (in my case IsMoving) to true and the MouseUp event to set it to false
On the first MouseDown event, set some Point property (InitialPosition) using the following code
if (FirstClick)
{
GeneralTransform transform = this.TransformToAncestor(this.Parent as Visual);
Point StartPoint = transform.Transform(new Point(0, 0));
StartX = StartPoint.X;
StartY = StartPoint.Y;
FirstClick = false;
}
Now that you have the starting position, you need to get the position of the mouse relative to your movement control. This is so you dont end up clicking the middle of your header to move it and it instantly moves the top left of the control to the mouse pointer location. To do this, place this code in the MouseDown event:
Point RelativeMousePoint = Mouse.GetPosition(Header);
RelativeX = RelativeMousePoint.X;
RelativeY = RelativeMousePoint.Y;
Now you have the point the control originated at (startX and STartY), the position of the mouse within your movement control (RelativeX, RelativeY), we just need to move the control to a new location! There are a few steps involved in doing this. Firstly your control needs to have a RenderTransform which is a TranslateTransform. If you dont want to set this in XAML, feel free to set it using this.RenderTransform = new TranslateTransform.
Now we need to set the X and Y coordinates on the RenderTransform so that the control will move to a new location. The following code accomplishes this
private void Header_MouseMove(object sender, MouseEventArgs e)
{
if (IsMoving)
{
//Get the position of the mouse relative to the controls parent
Point MousePoint = Mouse.GetPosition(this.Parent as IInputElement );
//set the distance from the original position
this.DistanceFromStartX= MousePoint.X - StartX - RelativeX ;
this.DistanceFromStartY= MousePoint.Y - StartY - RelativeY;
//Set the X and Y coordinates of the RenderTransform to be the Distance from original position. This will move the control
TranslateTransform MoveTransform = base.RenderTransform as TranslateTransform;
MoveTransform.X = this.DistanceFromStartX;
MoveTransform.Y = this.DistanceFromStartY;
}
}
As you can guess, there is a bit of code left off(variable declarations etc) but this should be all you need to get you started :) happy coding.
EDIT:
One problem you may encounter is that this allows you to move the control out of the area of its parent control. Here is some quick and dirty code to fix that issue...
if ((MousePoint.X + this.Width - RelativeX > Parent.ActualWidth) ||
MousePoint.Y + this.Height - RelativeY > Parent.ActualHeight ||
MousePoint.X - RelativeX < 0 ||
MousePoint.Y - RelativeY < 0)
{
IsMoving = false;
return;
}
Place this code in your MouseMove event before the actual movement takes place. This will check if the control is trying to move outside the bounds of the parent control. The IsMoving = false command will cause the control to exit movement mode. This means that the user will need to click the movement area again to try to move the control as it will have stopped at the boundary. If you want the control to automatically continue movement, just take that line out and the control will jump back onto the cursor as soon as it is back in a legal area.
You can find a lot of inspiration here:
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part1.aspx
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part2.aspx
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part3.aspx
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part4.aspx

Resources