Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Excuse me for previous posts. The problems was partly solved, but a new problem has arisen. When moving the WPF window with below method to around quickly, the window be leave from the mouse cursor! is there any idea!? please guide me.
C#
public myWindow()
{
this.InitializeComponent();

Body3D.MouseLeftButtonDown += new MouseButtonEventHandler(Body3D_MouseLeftButtonDown);
Body3D.MouseLeftButtonUp += new MouseButtonEventHandler(Body3D_MouseLeftButtonUp);
Body3D.MouseMove += new MouseEventHandler(Body3D_MouseMove);
}
bool inDrag;
Point dragPoint;
 
private void Body3D_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{ 
	dragPoint = e.GetPosition(this);
	inDrag = true; 
}
 
private void Body3D_MouseMove(object sender, MouseEventArgs e) 
{
	if (inDrag)
	{
		Point pointMoveTo;
 
		// Find the current mouse position in screen coordinates.
		pointMoveTo = this.PointToScreen(e.GetPosition(this));
 
		// Compensate for the position the control was clicked.
		pointMoveTo.Offset(-dragPoint.X, -dragPoint.Y);
 
		// Compensate for the non-client region (title bar).
		// This code is not necessary if you explicitly hide the title bar 
		// by setting the form's BorderStyle to None.
		//pointMoveTo.Offset(0, -25); //Move the window.

		this.Left  = pointMoveTo.X;
		this.Top = pointMoveTo.Y; 
	} 
} 
 
private void Body3D_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
	inDrag = false;
}} 

Notice: The Body3D is a ModelUIElement3D through
window
∟viewBox
∟viewPort3d
∟modelVisual3d
∟modelUIelement3d(body3D)
and some window properties set to Background="{x:Null}", AllowsTransparency="True" , WindowStyle="None".

Thanks a lot in advance.
Posted
Updated 4-Nov-11 9:44am
v2

1 solution

I already stated in another of your posts...you can't use e.GetPosition(this). The relative-to parameter needs to be the/an element the this.Left and this.Top properties are relative to!
 
Share this answer
 
Comments
hzawary 4-Nov-11 15:38pm    
In previous post I got careless, should swapping "this.Left" with "this.Top"! Now moving is correct, but When moving the WPF window with above method to around quickly, the window be leave from the mouse cursor!
please test it! thanks
Mark Salsbery 4-Nov-11 15:44pm    
I'm not sure how many ways I can repeat this. :) If you're moving the same window you're getting the coordinates relative to, then yes the window is probably not going to stay with the cursor.
hzawary 4-Nov-11 16:15pm    
Please guide me about this case that the cursor pre-empt the window when moving it:(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900