SilverLight鼠标绘制矩形

SilverLight鼠标绘制矩形,第1张

概述技术要点: 1.Canvas控件布局用到的Canvas.TopProperty以及Canvas.LeftProperty属性 2.MouseLeftButtonDown,MouseMove,MouseLeftButtonUp方法处理程序的调用 VB.NET代码如下:     Private isAddMouseEvent As Boolean = False     Private rectang

技术要点:

1.Canvas控件布局用到的Canvas.topProperty以及Canvas.leftProperty属性

2.MouseleftbuttonDown,MouseMove,MouseleftbuttonUp方法处理程序的调用

VB.NET代码如下:

    Private isAddMouseEvent As Boolean = False
    Private rectangle As Rectangle
    Private point As Point
    Private Sub btnMain_Click(ByVal sender As System.Object,ByVal e As System.windows.RoutedEventArgs) Handles btnMain.Click
        If Not isAddMouseEvent Then
            AddHandler canvasMain.MouseleftbuttonDown,AddressOf MouseEventDown
            isAddMouseEvent = True
            Me.btnMain.IsEnabled = False
        End If
    End Sub
    Private Sub MouseEventDown(ByVal sender As Object,ByVal e As MouseEventArgs)
        rectangle = New Rectangle()
        point = e.Getposition(canvasMain)
        rectangle.SetValue(Canvas.leftProperty,point.X)
        rectangle.SetValue(Canvas.topProperty,point.Y)
        rectangle.Opacity = 1
        rectangle.Fill = New SolIDcolorBrush(colors.Blue)
        rectangle.RadiusX = 10
        rectangle.RadiusY = 10
        AddHandler canvasMain.MouseMove,AddressOf MouseMove
        AddHandler canvasMain.MouseleftbuttonUp,AddressOf MouseUp
        canvasMain.Children.Add(rectangle)
    End Sub
    Private Sub MouseMove(ByVal sender As Object,ByVal e As MouseEventArgs)
        Dim tempPorint = e.Getposition(canvasMain)
        If tempPorint.X > point.X Then
            rectangle.WIDth = tempPorint.X - point.X
        End If
        If tempPorint.X <= point.X Then
            rectangle.SetValue(Canvas.leftProperty,tempPorint.X)
            rectangle.WIDth = point.X - tempPorint.X
        End If
        If tempPorint.Y > point.Y Then
            rectangle.Height = tempPorint.Y - point.Y
        End If
        If tempPorint.Y <= point.Y Then
            rectangle.Height = point.Y - tempPorint.Y
            rectangle.SetValue(Canvas.topProperty,tempPorint.Y)
        End If
    End Sub
    Private Sub MouseUp(ByVal sender As Object,ByVal e As MouseEventArgs)
        RemoveHandler canvasMain.MouseleftbuttonUp,AddressOf MouseUp
        RemoveHandler canvasMain.MouseMove,AddressOf MouseMove
        RemoveHandler canvasMain.MouseleftbuttonDown,AddressOf MouseEventDown
        isAddMouseEvent = False
        btnMain.IsEnabled = True
    End Sub

布局页面代码如下:

<GrID x:name="LayoutRoot" Background="White">        <Canvas x:name="canvasMain" WIDth="1000" Height="1000" Background="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">                    </Canvas>        <button x:name="btnMain" WIDth="90" Height="28" Content="点我开始绘制矩形" margin="0 0 0 0" VerticalAlignment="top" HorizontalAlignment="left">                    </button>    </GrID>

总结

以上是内存溢出为你收集整理的SilverLight鼠标绘制矩形全部内容,希望文章能够帮你解决SilverLight鼠标绘制矩形所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1077249.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存