C# 繪製箭頭的方法,仿微信截圖的箭頭
效果見下圖,實際上還是有區別的,箭頭的起點處微信的是圓端,而我實現的是尖端。
說說我的實現吧,實現方法其實是劃線,線的兩端都要設置端點樣式。看代碼:
Point _StarPoint = new Point(0, 0);
Point _EndPoint = new Point(300, 300);
System.Drawing.Drawing2D.AdjustableArrowCap _LineCap = new System.Drawing.Drawing2D.AdjustableArrowCap(9, 9, true); //設置一個線頭
_LineCap.Filled = true;
_LineCap.MiddleInset = 3.1f; //設置箭頭中間的縮進
Pen _Pen = new Pen(Brushes.Red, 5);
_Pen.CustomStartCap = new System.Drawing.Drawing2D.AdjustableArrowCap(5, 80, true);
_Pen.CustomEndCap = (System.Drawing.Drawing2D.CustomLineCap)_LineCap;
Graphics _Graphics = this.CreateGraphics();
_Graphics.SmoothingMode = SmoothingMode.AntiAlias;
_Graphics.DrawLine(_Pen, _StarPoint, _EndPoint);
_Graphics.Dispose();
注意這一句:_LineCap.MiddleInset = 3.1f; //設置箭頭中間的縮進
如果不設置箭頭中間的縮進,那麼繪製出來的是下面這樣:
C# 的 中有很多線段端點的樣式,如果需要其它不同的箭頭樣式,可以逐個試試。