繪製半口角

來源:互聯網
上載者:User

相信大家都在象棋中兵的位置和一些射擊遊戲裡的准心都見過,廢話就少說了,看代碼吧。

基類:

using System;using System.Collections.Generic;using System.Text;using System.Drawing;namespace GDI_Form{    /// <summary>    /// 抽象基類    /// <para>Author:Evan Lee</para>    /// <para>QQ:278631309</para>    /// <para>Blog:http://www.cnblogs.com/magic_evan </para>    /// </summary>    public abstract class BaseControl    {        private Graphics _g;        private string _name;        private int _ID;        private object _Tag;        private bool _v;        private float _x;        private float _y;        private float _w;        private float _h;        /// <summary>        /// 擷取設定GDI對象        /// </summary>        public Graphics G        {            get { return _g; }            set { _g = value; }        }        /// <summary>        /// 對象名稱        /// </summary>        public string Name        {            get { return _name; }            set { _name = value; }        }        /// <summary>        /// 編號        /// </summary>        public int ID        {            get { return _ID; }            set { _ID = value; }        }        /// <summary>        /// 標籤資料        /// </summary>        public object Tag        {            get { return _Tag; }            set { _Tag = value; }        }        /// <summary>        /// 對象是否可見        /// </summary>        public bool Visible        {            get { return _v; }            set { _v = value; }        }        /// <summary>        /// 起始X軸點        /// </summary>        public float X        {            get { return _x; }            set { _x = value; }        }        /// <summary>        /// 起始Y軸點        /// </summary>        public float Y        {            get { return _y; }            set { _y = value; }        }        /// <summary>        /// 寬度        /// </summary>        public float Width        {            get { return _w; }            set { _w = value; }        }        /// <summary>        /// 高度        /// </summary>        public float Heiht        {            get { return _h; }            set { _h = value; }        }    }}

 

實作類別:

using System;using System.Collections.Generic;using System.Text;using System.Drawing;namespace GDI_Form{    /// <summary>    /// 畫邊角類    /// <para>Author:Evan Lee</para>    ///<para>QQ:278631309</para>    ///<para>Blog:http://www.cnblogs.com/magic_evan </para>    /// </summary>    public class Angle : BaseControl    {        /// <summary>        /// 畫邊角類        /// </summary>        /// <param name="g"></param>        public Angle(Graphics g)        {            this.G = g;        }        /// <summary>        /// 角口的方向        /// ┏ ┓┗ ┛         /// </summary>        public enum AngleDirection        {            /// <summary>            /// 右上方┗            /// </summary>            UpperRight,            /// <summary>            /// 右下角┏            /// </summary>            LowerRight,            /// <summary>            /// 左下角┓            /// </summary>            LowerLeft,            /// <summary>            /// 左上方┛            /// </summary>            UpperLeft        }        /// <summary>        /// 繪製半口角        /// </summary>        /// <param name="pen">畫筆</param>        /// <param name="centerP">中心座標</param>        /// <param name="offsetP">位移中心的座標</param>        /// <param name="len">角的邊長</param>        /// <param name="direction">角口的方向</param>        public void DrawAngle(Pen pen, Point centerP, Point offsetP, int len, AngleDirection direction)        {            if (this.G == null)                return;            switch (direction)            {                case AngleDirection.UpperRight:                    this.G.DrawLine(pen, centerP.X + offsetP.X, centerP.Y - offsetP.Y, centerP.X + offsetP.X + len, centerP.Y - offsetP.Y);                    this.G.DrawLine(pen, centerP.X + offsetP.X, centerP.Y - offsetP.Y, centerP.X + offsetP.X, centerP.Y - offsetP.Y - len);                    break;                case AngleDirection.UpperLeft:                    this.G.DrawLine(pen, centerP.X - offsetP.X, centerP.Y - offsetP.Y, centerP.X - offsetP.X - len, centerP.Y - offsetP.Y);                    this.G.DrawLine(pen, centerP.X - offsetP.X, centerP.Y - offsetP.Y, centerP.X - offsetP.X, centerP.Y - offsetP.Y - len);                    break;                case AngleDirection.LowerRight:                    this.G.DrawLine(pen, centerP.X + offsetP.X, centerP.Y + offsetP.Y, centerP.X + offsetP.X + len, centerP.Y + offsetP.Y);                    this.G.DrawLine(pen, centerP.X + offsetP.X, centerP.Y + offsetP.Y, centerP.X + offsetP.X, centerP.Y + offsetP.Y + len);                    break;                case AngleDirection.LowerLeft:                    this.G.DrawLine(pen, centerP.X - offsetP.X, centerP.Y + offsetP.Y, centerP.X - offsetP.X - len, centerP.Y + offsetP.Y);                    this.G.DrawLine(pen, centerP.X - offsetP.X, centerP.Y + offsetP.Y, centerP.X - offsetP.X, centerP.Y + offsetP.Y + len);                    break;            }        }    }}

 


 使用方法

        private void test()        {            int centerX = this.Width / 2;            int centerY = this.Height / 2;            Point centerP = new Point(centerX, centerY);            Point offsetP = new Point(3, 3);            int len = 10;            Graphics g = this.CreateGraphics();            Pen pen = new Pen(Color.Red, 2);            Angle angle = new Angle(g);            angle.DrawAngle(pen, centerP, offsetP, len, Angle.AngleDirection.LowerLeft);           angle.DrawAngle(pen, centerP, offsetP, len, Angle.AngleDirection.LowerRight);            angle.DrawAngle(pen, centerP, offsetP, len, Angle.AngleDirection.UpperLeft);            angle.DrawAngle(pen, centerP, offsetP, len, Angle.AngleDirection.UpperRight);        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.