相信大家都在象棋中兵的位置和一些射击游戏里的准心都见过,废话就少说了,看代码吧。
基类:
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); }