在WPF中拖动选择框

时间:2015-03-17 19:16:54

标签: c# wpf mouse selection mousehover

是否可以通过将鼠标悬停在对象上并在WPF中绘制圆圈并拖动选择悬停框来实现鼠标悬停选择。我想要的是当用户通过画一个圆圈将鼠标悬停在按钮a上时,这个按钮会像点击它一样进行特定操作。 你能给一些示例代码或链接吗?

1 个答案:

答案 0 :(得分:2)

这是一个高级主题,涉及使用装饰品 更多信息可以在这里找到:

https://denisvuyka.wordpress.com/2007/10/15/wpf-simple-adorner-usage-with-drag-and-resize-operations/

What's the point to WPF adorners?

我使用它的一个小例子:

public class SelectionAdorner : Adorner
{
    public SelectionAdorner(UIElement adornedElement)
        : base(adornedElement)
    {
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        Rect adornedElementRect = new Rect(0,0,ActualWidth,ActualHeight);

        // Some arbitrary drawing implements.
        SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green);
        renderBrush.Opacity = 0.2;
        Pen renderPen = new Pen(new SolidColorBrush(Colors.Navy), 1.5);
        double renderRadius = 5.0;

        // Draw a circle at each corner.
        drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius);
        drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius);
        drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius);
        drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius);
    }

}