C#常用函数总结

1、弧度角度转换函数


        ///<summary>
        ///角度转弧度函数
        ///</summary>
        static double ToRadian(double radius)
        {
            return Math.PI * radius / 180;
        }

        ///<summary>
        ///弧度转角度函数
        ///</summary>
        static double ToRadius(double radian)
        {
            return 180 * radian / Math.PI;
        }

 2、 画布画点函数


        /// <summary>
        /// 画布画点函数;
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        private void DrawPoints(List <Point> points)
        {
            //获取该窗体的绘制上下文
            using (Graphics g = this.splitter_show.CreateGraphics())
            {
                SolidBrush sb = new SolidBrush(Color.Aquamarine );
                foreach (var point in points)
                {
                    g.FillEllipse(sb, point.X, point.Y, 2, 2);
                }
            }      
        }

 3、画点函数


            using (Graphics g = this.splitter_window.CreateGraphics())
            {
                Pen pen = new Pen(Color.Green, 3);
                
                foreach (var point in ListPoint)
                {
                    g.FillEllipse(new SolidBrush(Color.Green), point.X, point.Y, 2, 2);
                }  
            }

 4、画点和画线


            using (Graphics g = this.splitter_show.CreateGraphics())
            {
                //打点
                g.FillEllipse(new SolidBrush(Color.Black), 100, 300, 10, 10);
                g.FillEllipse(new SolidBrush(Color.Black), 200, 200, 10, 10);
                g.FillEllipse(new SolidBrush(Color.Black), 300, 200, 10, 10);
                g.FillEllipse(new SolidBrush(Color.Black), 300, 350, 10, 10);
                g.FillEllipse(new SolidBrush(Color.Black), 400, 250, 10, 10);
                g.FillEllipse(new SolidBrush(Color.Black), 450, 300, 10, 10);
                g.FillEllipse(new SolidBrush(Color.Black), 300, 50, 10, 10);
                g.FillEllipse(new SolidBrush(Color.Black), 100, 150, 10, 10);

                //连线
                g.DrawLine(new Pen(Color.Black, 4), new Point(100, 300), new Point(200, 200));
                g.DrawLine(new Pen(Color.Black, 4), new Point(200, 200), new Point(300, 200));
                g.DrawLine(new Pen(Color.Black, 4), new Point(300, 200), new Point(300, 350));
                g.DrawLine(new Pen(Color.Black, 4), new Point(300, 350), new Point(400, 250));
                g.DrawLine(new Pen(Color.Black, 4), new Point(400, 250), new Point(450, 300));
                g.DrawLine(new Pen(Color.Black, 4), new Point(450, 300), new Point(300, 50));
                g.DrawLine(new Pen(Color.Black, 4), new Point(300, 50), new Point(100, 150));
                g.DrawLine(new Pen(Color.Black, 4), new Point(100, 150), new Point(100, 300));
            }

 5、判断完整性


        /// <summary>
        /// 判断完整性函数
        /// </summary>
        /// <returns></returns>
        private bool isComplete()
        {
            return this.textBox_M0.Text != null && this.textBox_λ0.Text != null && this.textBox_φ1.Text != null && this.textBox_φ2.Text != null && this.textBox_φ_min.Text!= null;
        }

        /// <summary>
        /// 传数据函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click_OK(object sender, EventArgs e)
        {
            //预防性措施
            if(!this.isComplete())
            {
                MessageBox.Show("hello");
                return;
            }
     }

 6、判断字符串是否能转换成数字


        //判断字符串是否能转换成数字
        static bool IsNumberic(string str)
        {
            double num;
            bool isnum = double.TryParse(str, System.Globalization.NumberStyles.Float,System .Globalization .NumberFormatInfo.InvariantInfo  ,out num );
            return isnum;
        }

 7、注册鼠标事件


        /// <summary>
        /// 获取画布点击开始坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_Input(object sender, EventArgs e)
        {
            //注册鼠标单击事件;
            this.splitter_window.MouseClick += splitter_window_Click_GetXY;
        }

        /// <summary>
        /// 手写的函数,获取画布点击开始坐标;
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void splitter_window_Click_GetXY(object sender, MouseEventArgs e)
        {
            //获取 XY;
            this.textBox_x1.Text = $"{e.X}";
            this.textBox_y1.Text = $"{e.Y}";
            //画点;
            using (Graphics g = this.splitter_window.CreateGraphics())
            {  
                g.FillEllipse(new SolidBrush(Color.Black), e.X, e.Y, 10, 10);
            }
            //注销;
            this.splitter_window.MouseClick -= splitter_window_Click_GetXY;
        }
原文地址:https://www.cnblogs.com/zhangxiaoshuang/p/6437160.html