C# 如何测试窗口是否正在被拖动到WPF
我有一个无标题的窗口,因为我想创建自己的窗口样式 标题、最小化、最大化和关闭按钮位于停靠面板中。我添加了以下事件处理程序来最大化、还原和拖动窗口 当窗口最大化时,问题就出现了 我所发现的是,每当我在标题上单击一次,就会恢复。当我只希望它在双击或拖动时恢复。我可以理解为什么会发生这种情况,但不确定如何解决这个问题C# 如何测试窗口是否正在被拖动到WPF,c#,wpf,C#,Wpf,我有一个无标题的窗口,因为我想创建自己的窗口样式 标题、最小化、最大化和关闭按钮位于停靠面板中。我添加了以下事件处理程序来最大化、还原和拖动窗口 当窗口最大化时,问题就出现了 我所发现的是,每当我在标题上单击一次,就会恢复。当我只希望它在双击或拖动时恢复。我可以理解为什么会发生这种情况,但不确定如何解决这个问题 public void TITLEBAR_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
public void TITLEBAR_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DockPanel dp = (DockPanel)sender;
Window parentWindow = Window.GetWindow(dp);
bool doubleClick = IsDoubleClick(sender, e);
if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Pressed && !doubleClick)
{
if (parentWindow.WindowState == WindowState.Maximized)
{
double mouseX = e.GetPosition(parentWindow).X;
double width = parentWindow.RestoreBounds.Width;
System.Drawing.Rectangle screenBounds = getCurrentScreenBounds(parentWindow);
double x = screenBounds.Left + (mouseX - ((width / 100.00) * ((100.00 / screenBounds.Width) * mouseX)));
if (x < 0)
{
x = 0;
}
else
{
if (x + width > screenBounds.Left + screenBounds.Width)
{
x = screenBounds.Left + screenBounds.Width - width;
}
}
parentWindow.Left = x;
parentWindow.Top = screenBounds.Top;
parentWindow.WindowState = System.Windows.WindowState.Normal;
}
parentWindow.DragMove();
//MessageBox.Show("");
}
if (doubleClick)
{
if (parentWindow.WindowState == System.Windows.WindowState.Maximized)
{
parentWindow.WindowState = System.Windows.WindowState.Normal;
}
else
{
parentWindow.WindowState = System.Windows.WindowState.Maximized;
}
}
}
有一个名为Thumb
的WPF元素(控件),我用它来制作可拖动的部件。它有一个DragDelta
事件,您可以使用该事件检查可拖动零件的水平偏移
和垂直偏移
。您可以保存以前的值,并检查新值是否相同或更改;也就是说它被拖走了
(这只是一个对我有用的建议)。好吧,也许有人会觉得这很有帮助 我改变了周围的情况,以便它通过两个事件识别拖动,即MouseMove和MouseLeftButtonDown事件 MouseLeftButtonDown在setStartPosition()中捕获拖动的可能开始位置
public void TITLEBAR\u MouseLeftButtonDown(对象发送器,MouseButtonEventArgs e)
{
DockPanel dp=(DockPanel)发送方;
windowparentwindow=Window.GetWindow(dp);
双击=双击(发送方,e);
如果(e.ChangedButton==MouseButton.Left&&e.LeftButton==MouseButtonState.Pressed&&!双击)
{
如果(parentWindow.WindowsState==WindowsState.Maximized)
{
设置起始位置(发送器,e);
}
}
如果(双击)
{
if(parentWindow.WindowsState==System.Windows.WindowsState.Maximized)
{
parentWindow.WindowsState=System.Windows.WindowsState.Normal;
}
其他的
{
parentWindow.WindowsState=System.Windows.WindowsState.Maximized;
}
}
}
私有无效标题栏\u MouseMove(对象发送方,MouseEventArgs e)
{
DockPanel dp=(DockPanel)发送方;
windowparentwindow=Window.GetWindow(dp);
如果(e.LeftButton==鼠标按钮状态。按下)
{
如果(IsDraging(发送方,e)&&!双击)
{
如果(parentWindow.WindowsState==WindowsState.Maximized)
{
双鼠标=e.GetPosition(parentWindow).X;
双宽度=parentWindow.RestoreBounds.width;
System.Drawing.Rectangle screenBounds=getCurrentScreenBounds(父窗口);
双x=屏幕边界。左+(鼠标-((宽度/100.00)*((100.00/screenBounds.width)*鼠标));
if(x<0)
{
x=0;
}
其他的
{
如果(x+width>screenBounds.Left+screenBounds.width)
{
x=屏幕边界。左+屏幕边界。宽度-宽度;
}
}
parentWindow.Left=x;
parentWindow.Top=screenBounds.Top;
parentWindow.WindowsState=System.Windows.WindowsState.Normal;
}
parentWindow.DragMove();
}
}
}
下面是修改后的类:
public static class MouseButtonHelper
{
private const long k_DoubleClickSpeed = 500;
private const double k_MaxMoveDistance = 10;
private static long _LastClickTicks = 0;
private static System.Windows.Point _LastPosition;
private static WeakReference _LastSender;
private static System.Windows.Point _DragStartPosition;
public static bool IsDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
System.Windows.Point position = e.GetPosition(null);
long clickTicks = DateTime.Now.Ticks;
long elapsedTicks = clickTicks - _LastClickTicks;
long elapsedTime = elapsedTicks / TimeSpan.TicksPerMillisecond;
bool quickClick = (elapsedTime <= k_DoubleClickSpeed);
bool senderMatch = (_LastSender != null && sender.Equals(_LastSender.Target));
if (senderMatch && quickClick && position.Distance(_LastPosition) <= k_MaxMoveDistance)
{
// Double click!
_LastClickTicks = 0;
_LastSender = null;
return true;
}
// Not a double click
_LastClickTicks = clickTicks;
_LastPosition = position;
if (!quickClick)
_LastSender = new WeakReference(sender);
return false;
}
public static void setStartPosition(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
_DragStartPosition = e.GetPosition(null);
}
public static bool IsDragging(object sender, System.Windows.Input.MouseEventArgs e)
{
System.Windows.Point mousePos = e.GetPosition(null);
System.Windows.Vector diff = _DragStartPosition - mousePos;
if (Math.Abs(diff.X) > System.Windows.SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > System.Windows.SystemParameters.MinimumVerticalDragDistance)
{
return true;
}
return false;
}
private static double Distance(this System.Windows.Point pointA, System.Windows.Point pointB)
{
double x = pointA.X - pointB.X;
double y = pointA.Y - pointB.Y;
return Math.Sqrt(x * x + y * y);
}
}
public静态类MouseButtonHelper
{
private const long k_双击速度=500;
私有常数双k_MaxMoveDistance=10;
私有静态长_LastClickTicks=0;
私有静态系统.Windows.Point\u LastPosition;
私有静态WeakReference\u LastSender;
专用静态System.Windows.Point\u DragStartPosition;
公共静态布尔值为双击(对象发送器,System.Windows.Input.MouseButtonEventArgs e)
{
System.Windows.Point position=e.GetPosition(空);
长点击滴答声=DateTime.Now.Ticks;
long elapsedTicks=点击滴答声-\u Last clickTicks;
long elapsedTime=elapsedTicks/TimeSpan.tick毫秒;
布尔快速点击=(elapsedTime System.Windows.SystemParameters.MinimumVerticalDragDistance)
{
返回true;
}
返回false;
}
专用静态双距离(此System.Windows.Point pointA、System.Windows.Point pointB)
{
双x=点A.x-点B.x;
双y=点A.y-点B.y;
返回Math.Sqrt(x*x+y*y);
}
}
您可以添加答案,然后在2天后进行标记,这将是一种更好的方法。你可以获得投票权:)。汉克,将你的更新复制到一个答案中,你的问题和答案都会获得我的投票权。谢谢@Kaveh,我一定会用这个来改进我所做的!
public static class WindowHelper
{
public static System.Drawing.Rectangle getCurrentScreenBounds(System.Windows.Window pWnd)
{
System.Windows.Forms.Screen parentScreen = GetCurrentScreen(pWnd);
if (parentScreen == null)
{
return System.Windows.Forms.Screen.PrimaryScreen.Bounds;
}
return parentScreen.Bounds;
}
private static System.Windows.Forms.Screen GetCurrentScreen(System.Windows.Window pWnd)
{
System.Drawing.Rectangle intersectingRect = new System.Drawing.Rectangle();
System.Drawing.Rectangle windowRect = new System.Drawing.Rectangle(Convert.ToInt32(pWnd.Left), Convert.ToInt32(pWnd.Top), Convert.ToInt32(pWnd.Width), Convert.ToInt32(pWnd.Height));
int largestIntersectingArea = 0;
System.Windows.Forms.Screen curScreen = null;
foreach (System.Windows.Forms.Screen s in System.Windows.Forms.Screen.AllScreens)
{
if (s.Bounds.IntersectsWith(windowRect))
{
intersectingRect = System.Drawing.Rectangle.Intersect(s.Bounds, windowRect);
int intersectingArea = intersectingRect.Width * intersectingRect.Height;
if (intersectingArea > largestIntersectingArea)
{
largestIntersectingArea = intersectingArea;
curScreen = s;
}
}
}
return curScreen;
}
}
public void TITLEBAR_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DockPanel dp = (DockPanel)sender;
Window parentWindow = Window.GetWindow(dp);
doubleClick = IsDoubleClick(sender, e);
if (e.ChangedButton == MouseButton.Left && e.LeftButton == MouseButtonState.Pressed && !doubleClick)
{
if (parentWindow.WindowState == WindowState.Maximized)
{
setStartPosition(sender, e);
}
}
if (doubleClick)
{
if (parentWindow.WindowState == System.Windows.WindowState.Maximized)
{
parentWindow.WindowState = System.Windows.WindowState.Normal;
}
else
{
parentWindow.WindowState = System.Windows.WindowState.Maximized;
}
}
}
private void TITLEBAR_MouseMove(object sender, MouseEventArgs e)
{
DockPanel dp = (DockPanel)sender;
Window parentWindow = Window.GetWindow(dp);
if (e.LeftButton == MouseButtonState.Pressed)
{
if (IsDragging(sender, e) && !doubleClick)
{
if (parentWindow.WindowState == WindowState.Maximized)
{
double mouseX = e.GetPosition(parentWindow).X;
double width = parentWindow.RestoreBounds.Width;
System.Drawing.Rectangle screenBounds = getCurrentScreenBounds(parentWindow);
double x = screenBounds.Left + (mouseX - ((width / 100.00) * ((100.00 / screenBounds.Width) * mouseX)));
if (x < 0)
{
x = 0;
}
else
{
if (x + width > screenBounds.Left + screenBounds.Width)
{
x = screenBounds.Left + screenBounds.Width - width;
}
}
parentWindow.Left = x;
parentWindow.Top = screenBounds.Top;
parentWindow.WindowState = System.Windows.WindowState.Normal;
}
parentWindow.DragMove();
}
}
}
public static class MouseButtonHelper
{
private const long k_DoubleClickSpeed = 500;
private const double k_MaxMoveDistance = 10;
private static long _LastClickTicks = 0;
private static System.Windows.Point _LastPosition;
private static WeakReference _LastSender;
private static System.Windows.Point _DragStartPosition;
public static bool IsDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
System.Windows.Point position = e.GetPosition(null);
long clickTicks = DateTime.Now.Ticks;
long elapsedTicks = clickTicks - _LastClickTicks;
long elapsedTime = elapsedTicks / TimeSpan.TicksPerMillisecond;
bool quickClick = (elapsedTime <= k_DoubleClickSpeed);
bool senderMatch = (_LastSender != null && sender.Equals(_LastSender.Target));
if (senderMatch && quickClick && position.Distance(_LastPosition) <= k_MaxMoveDistance)
{
// Double click!
_LastClickTicks = 0;
_LastSender = null;
return true;
}
// Not a double click
_LastClickTicks = clickTicks;
_LastPosition = position;
if (!quickClick)
_LastSender = new WeakReference(sender);
return false;
}
public static void setStartPosition(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
_DragStartPosition = e.GetPosition(null);
}
public static bool IsDragging(object sender, System.Windows.Input.MouseEventArgs e)
{
System.Windows.Point mousePos = e.GetPosition(null);
System.Windows.Vector diff = _DragStartPosition - mousePos;
if (Math.Abs(diff.X) > System.Windows.SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > System.Windows.SystemParameters.MinimumVerticalDragDistance)
{
return true;
}
return false;
}
private static double Distance(this System.Windows.Point pointA, System.Windows.Point pointB)
{
double x = pointA.X - pointB.X;
double y = pointA.Y - pointB.Y;
return Math.Sqrt(x * x + y * y);
}
}