• DrawRectangle


     1 public class Test
     2    {
     3        private bool drawing;
     4        private Point beginPoint;
     5        private Point endPoint;
     6        private Size size = new Size();
     7
     8        public Test()
     9        {
    10            InitializeComponent();
    11        }

    12
    13        protected override void OnPaint(PaintEventArgs e)
    14        {
    15            if (drawing)
    16            {
    17                size.Width = endPoint.X - beginPoint.X;
    18                size.Height = endPoint.Y - beginPoint.Y;
    19
    20                if (size.Width >= 0 && size.Height >= 0)
    21                {
    22                    e.Graphics.DrawRectangle(Pens.Red, new Rectangle(beginPoint, size));
    23                }

    24                else
    25                {
    26                    if (size.Width <= 0 && size.Height <= 0)
    27                    {
    28                        size.Width = Math.Abs(size.Width);
    29                        size.Height = Math.Abs(size.Height);
    30                        e.Graphics.DrawRectangle(Pens.Red, new Rectangle(endPoint, size));
    31                    }

    32                    else
    33                    {
    34                        if (size.Width > 0)
    35                        {
    36                            size.Width = Math.Abs(size.Width);
    37                            size.Height = Math.Abs(size.Height);
    38                            int x = beginPoint.X;
    39                            int y = endPoint.Y;
    40                            Point p = new Point(x, y);
    41
    42                            e.Graphics.DrawRectangle(Pens.Red, new Rectangle(p, size));
    43                        }

    44                        else
    45                        {
    46                            size.Width = Math.Abs(size.Width);
    47                            size.Height = Math.Abs(size.Height);
    48                            int x = endPoint.X;
    49                            int y = beginPoint.Y;
    50                            Point p = new Point(x, y);
    51
    52                            e.Graphics.DrawRectangle(Pens.Red, new Rectangle(p, size));
    53                        }

    54                    }

    55                }

    56
    57            }

    58            base.OnPaint(e);
    59        }

    60
    61        private void Test_MouseMove(object sender, MouseEventArgs e)
    62        {
    63            if (drawing)
    64            {
    65                endPoint = e.Location;
    66                Invalidate();
    67            }

    68        }

    69
    70        private void Test_MouseUp(object sender, MouseEventArgs e)
    71        {
    72            drawing = false;
    73            endPoint = e.Location;
    74        }

    75
    76        private void Test_MouseDown(object sender, MouseEventArgs e)
    77        {
    78            drawing = true;
    79            beginPoint = e.Location;
    80        }

    81
    82    }
  • 相关阅读:
    Asp.Net MVC 路由
    EntityFramework执行SQL语句
    partial类与[MetadataType(typeof(类名))]有什么区别?
    Asp.net mvc + .net ef database first 或 model first 时如何添加验证特性
    Entity Framework 学习总结之一:ADO.NET 实体框架概述
    C#中dynamic的正确用法
    Func与Action
    SVN的使用和问题解决方法总结
    微信小程序---绘图
    微信小程序---动画
  • 原文地址:https://www.cnblogs.com/xiaowy/p/771001.html
Copyright © 2020-2023  润新知