用户控件之箭头
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Test
{
public partial class DirectionLine : UserControl
{
public DirectionLine()
{
InitializeComponent();
}
private string caption = "";
public string Caption
{
set { caption = value; }
get { return caption; }
}
private bool hand = false;
/// <summary>
/// 是否需手状
/// </summary>
public bool Hand
{
set { hand = value; }
get { return hand; }
}
private int degrees = 0;
/// <summary>
/// 度数
/// </summary>
public int Degrees
{
set { degrees = value; }
get { return degrees; }
}
private int widths = 6;
/// <summary>
/// 线条宽度
/// </summary>
public int Widths
{
set { widths = value; }
get { return widths; }
}
/// <summary>
/// 起点X坐标
/// </summary>
private int x1 = 0;
public int X1
{
set { x1 = value; }
get { return x1; }
}
/// <summary>
/// 终点X坐标
/// </summary>
private int x2 = 100;
public int X2
{
set { x2 = value; }
get { return x2; }
}
private int y1 = 20;
public int Y1
{
set { y1 = value; }
get { return y1; }
}
private int y2 = 20;
public int Y2
{
set { y2 = value; }
get { return y2; }
}
/// <summary>
/// 标题X坐标
/// </summary>
private int captionX = 0;
public int CaptionX
{
set { captionX = value; }
get { return captionX; }
}
private int captionY = 0;
public int CaptionY
{
set { captionY = value; }
get { return captionY; }
}
/// <summary>
/// 箭头颜色
/// </summary>
private Color colors=Color.Blue;
public Color Colors
{
set { colors = value; }
get { return colors; }
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Graphics g = this.CreateGraphics();
//单色填充
//SolidBrush b1 = new SolidBrush(Color.Blue);//定义单色画刷
Pen p = new Pen(colors, widths);
p.DashStyle = DashStyle.Solid;//实线
p.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头
g.RotateTransform(degrees);//旋转度数
g.DrawLine(p, x1, y1, x2, y2);
g.ResetTransform();//恢复坐标轴坐标
if (this.Caption != null)
{
// 绘制文本
using (StringFormat f = new StringFormat())
{
// 水平居中对齐
f.Alignment = System.Drawing.StringAlignment.Center;
// 垂直居中对齐
f.LineAlignment = System.Drawing.StringAlignment.Center;
// 设置为单行文本
f.FormatFlags = System.Drawing.StringFormatFlags.NoWrap;
// 绘制文本
using (SolidBrush b = new SolidBrush(this.ForeColor))
{
g.DrawString(
this.Caption,
this.Font,
b, captionX, captionY);
}
}
}
g.Dispose();
}
protected override void OnMouseMove(MouseEventArgs e)
{
// Make the cursor the Hand cursor when the mouse moves
if (hand)
Cursor = Cursors.Hand;
// Call MyBase.OnMouseMove to activate the delegate.
base.OnMouseMove(e);
}
/// <summary>
/// 处理鼠标单击事件
/// </summary>
/// <param name="e"></param>
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
}
}
}
浙公网安备 33010602011771号