WPF 绘正方形,支持从各个方向开始绘

来源:互联网 时间:1970-01-01


1.UI界面

<Window x:Class="DrawBoard.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" Title="Window1" Height="500" Width="650"> <Grid> <DockPanel SnapsToDevicePixels="True"> <Canvas x:Name="DrawCanvas" Background="White" Cursor="Pen" MouseLeftButtonDown="Canvas_MouseLeftButtonDown" MouseMove="Canvas_MouseMove" MouseLeftButtonUp="Canvas_MouseLeftButtonUp"/> </DockPanel> </Grid></Window>
2.逻辑代码

<span style="white-space:pre"> </span>/// <summary> /// 鼠标按下 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DrawCanvas.Children.Clear(); m_drawFlag = true; Rectangle rectangle = new Rectangle(); rectangle.Stroke = Brushes.Red; rectangle.StrokeThickness = 3; rectangle.Opacity = 1; m_insertShape = rectangle; m_startPosition = e.GetPosition(DrawCanvas); Canvas.SetLeft(rectangle, e.GetPosition(DrawCanvas).X); Canvas.SetTop(rectangle, e.GetPosition(DrawCanvas).Y); DrawCanvas.Children.Add(m_insertShape); return; }
<pre name="code" class="csharp"><span style="white-space:pre"> </span>/// <summary> /// 鼠标移动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Canvas_MouseMove(object sender, MouseEventArgs e) { if (!m_drawFlag || m_insertShape == null) { <span style="white-space:pre"> </span>return; } if (e.GetPosition(DrawCanvas).X > m_startPosition.X)            {                m_width = e.GetPosition(DrawCanvas).X - m_startPosition.X;            }            else            {                m_width = m_startPosition.X - e.GetPosition(DrawCanvas).X;            }            if (e.GetPosition(DrawCanvas).Y > m_startPosition.Y)            {                m_height = e.GetPosition(DrawCanvas).Y - m_startPosition.Y;            }            else            {                m_height = m_startPosition.Y - e.GetPosition(DrawCanvas).Y;            }            if (m_width < m_height)            {                // 边长为width                m_insertShape.Width = m_width;                m_insertShape.Height = m_width;                if (e.GetPosition(DrawCanvas).X <= m_startPosition.X)                {                    Canvas.SetLeft(m_insertShape, m_startPosition.X - m_width);                }                else                {                    Canvas.SetRight(m_insertShape, e.GetPosition(DrawCanvas).X);                }                if (e.GetPosition(DrawCanvas).Y <= m_startPosition.Y)                {                    Canvas.SetTop(m_insertShape, m_startPosition.Y - m_width);                }                else                {                    Canvas.SetBottom(m_insertShape, m_startPosition.Y + m_width);                }            }            else            {                // 边长为height                m_insertShape.Width = m_height;                m_insertShape.Height = m_height;                if (e.GetPosition(DrawCanvas).X <= m_startPosition.X)                {                    Canvas.SetLeft(m_insertShape, m_startPosition.X - m_height);                }                else                {                    Canvas.SetRight(m_insertShape, m_startPosition.X + m_height);                }                if (e.GetPosition(DrawCanvas).Y <= m_startPosition.Y)                {                    Canvas.SetTop(m_insertShape, m_startPosition.Y - m_height);                }                else                {                    Canvas.SetBottom(m_insertShape, e.GetPosition(DrawCanvas).Y);                }            } return; }
<span style="white-space:pre"> </span>/// <summary> /// 鼠标提起 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { m_drawFlag = false; return; }








相关阅读:
Top