我目前正在开发一个使用与Google Chrome标签相同的标签的程序, 标签工作正常,没问题。 但我遇到的情况是我可以将标签拖出显示器屏幕(左右边缘)。
问题是,如何限制拖动到屏幕边缘的标签? 因此标签不会通过屏幕。基本上它只是关于审美目的.. 请注意,我已经从谷歌和这个网站进行了研究,但仍然无法弄清楚如何做到这一点。
这是一段代码。
private bool draggingWindow;
private Size finalSize;
private double overlap;
private double leftMargin;
private double rightMargin;
private double maxTabWidth;
private double minTabWidth;
private double defaultMeasureHeight;
private double currentTabWidth;
private int captureGuard;
private int originalIndex;
private int slideIndex;
private List<double> slideIntervals;
private ChromeTabItem draggedTab;
private Point downPoint;
private ChromeTabControl parent;
public Rect addButtonRect;
private Size addButtonSize;
public Button addButton;
private bool modifyLeftOffset, modifyTopOffset;
private Point origCursorLocation;
private double origHorizOffset, origVertOffset;
protected override void OnPreviewMouseMove(MouseEventArgs e)
{
try
{
base.OnPreviewMouseMove(e);
if (this.addButtonRect.Contains(e.GetPosition(this)) && this.addButton.Background != Brushes.White && this.addButton.Background != Brushes.DarkGray)
{
this.addButton.Background = Brushes.White;
this.InvalidateVisual();
}
else if (!this.addButtonRect.Contains(e.GetPosition(this)) && this.addButton.Background != null)
{
this.addButton.Background = null;
this.InvalidateVisual();
}
if (this.draggedTab == null || this.draggingWindow) { return; }
Point nowPoint = e.GetPosition(this);
this.addButton.Visibility = Visibility.Hidden;
double newHorizontalOffset;
if (this.modifyLeftOffset)
newHorizontalOffset = this.origHorizOffset + (nowPoint.X - this.origCursorLocation.X);
else
newHorizontalOffset = this.origHorizOffset - (nowPoint.X - this.origCursorLocation.X);
Thickness margin = new Thickness(nowPoint.X - this.downPoint.X, 0, this.downPoint.X - nowPoint.X, 0);
this.draggedTab.Margin = margin;
Rect elemRect = this.CalculateDragElementRect(newHorizontalOffset, 1);
bool leftAlign = elemRect.Left < 0;
bool rightAlign = elemRect.Right > this.ActualWidth;
if (leftAlign)
newHorizontalOffset = modifyLeftOffset ? 0 : this.ActualWidth - elemRect.Width;
else if (rightAlign)
newHorizontalOffset = modifyLeftOffset ? this.ActualWidth - elemRect.Width : 0;
if (this.modifyLeftOffset)
Canvas.SetLeft(this.draggedTab, newHorizontalOffset);
else
Canvas.SetRight(this.draggedTab, newHorizontalOffset);
base.OnPreviewMouseMove( e );
if (margin.Left != 0)
{
int guardValue = Interlocked.Increment(ref this.captureGuard);
if (guardValue == 1)
{
this.originalIndex = this.draggedTab.Index;
this.slideIndex = this.originalIndex + 1;
this.slideIntervals = new List<double>();
this.slideIntervals.Add(double.NegativeInfinity);
for (int i = 1; i <= this.Children.Count; i += 1)
{
var diff = i - this.slideIndex;
var sign = diff == 0 ? 0 : diff / Math.Abs(diff);
var bound = Math.Min(1, Math.Abs(diff)) * ((sign * this.currentTabWidth / 3) + ((Math.Abs(diff) < 2) ? 0 : (diff - sign) * (this.currentTabWidth - this.overlap)));
this.slideIntervals.Add(bound);
}
this.slideIntervals.Add(double.PositiveInfinity);
this.CaptureMouse();
}
else
{
int changed = 0;
if (this.slideIntervals != null)
{
if (margin.Left < this.slideIntervals[this.slideIndex - 1])
{
SwapSlideInterval(this.slideIndex - 1);
this.slideIndex -= 1;
changed = 1;
}
else if (margin.Left > this.slideIntervals[this.slideIndex + 1])
{
SwapSlideInterval(this.slideIndex + 1);
this.slideIndex += 1;
changed = -1;
}
}
if (changed != 0)
{
var rightedOriginalIndex = this.originalIndex + 1;
var diff = 1;
if (changed > 0 && this.slideIndex >= rightedOriginalIndex)
{
changed = 0;
diff = 0;
}
else if (changed < 0 && this.slideIndex <= rightedOriginalIndex)
{
changed = 0;
diff = 2;
}
ChromeTabItem shiftedTab = this.Children[this.slideIndex - diff] as ChromeTabItem;
if (shiftedTab != this.draggedTab)
{
StickyReanimate(shiftedTab, changed * (this.currentTabWidth - this.overlap), .13);
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
真的很感激任何回应和关注。
谢谢