C#-利用自定义控件绘制一个箭头控件

简介: 利用自定义控件绘制一个箭头控件

1.向解决方案中添加windows窗体,目的用来显示我们创建的自定义控件。这里我创建一个ArrowView的窗口类。

2.鼠标右键->添加->新建项->自定义控件,这里我们命名为Arrow.cs,接下来编写箭头的代码,我们可以给几个属性,比如箭头的颜色,箭头边框的颜色,边框的跨度等等,你可以增加你需要控制的属性。

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Navigation;
usingSystem.Windows.Shapes;
namespaceDemo.CustomControl{
/// <summary>/// 按照步骤 1a 或 1b 操作,然后执行步骤 2 以在 XAML 文件中使用此自定义控件。////// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根/// 元素中://////     xmlns:MyNamespace="clr-namespace:Demo.CustomControl"///////// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。/// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根/// 元素中://////     xmlns:MyNamespace="clr-namespace:Demo.CustomControl;assembly=Demo.CustomControl"////// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用,/// 并重新生成以避免编译错误://////     在解决方案资源管理器中右击目标项目,然后依次单击///     “添加引用”->“项目”->[浏览查找并选择此项目]///////// 步骤 2)/// 继续操作并在 XAML 文件中使用控件。//////     <MyNamespace:Arrow/>////// </summary>publicclassArrow : Control    {
staticArrow()
        {
DefaultStyleKeyProperty.OverrideMetadata(typeof(Arrow), newFrameworkPropertyMetadata(typeof(Arrow)));
        }
/// <summary>/// 箭头颜色/// </summary>privateSolidColorBrushm_ArrowColor;
publicSolidColorBrushArrowColor        {
get            {
returnm_ArrowColor;
            }
set            {
m_ArrowColor=value;
            }
        }
/// <summary>/// 箭头边框颜色/// </summary>privateSolidColorBrushm_ArrowBorderColor;
publicSolidColorBrushArrowBorderColor        {
get            {
returnm_ArrowBorderColor;
            }
set            {
m_ArrowBorderColor=value;
            }
        }
/// <summary>/// 箭头边框宽度/// </summary>publicdoublem_ArrowBorder=0;
publicdoubleArrowBorder        {
get            {
returnm_ArrowBorder;
            }
set            {
m_ArrowBorder=value;
            }
        }
protectedoverridevoidOnRender(DrawingContextdrawingContext)
        {
drawingContext.DrawGeometry(ArrowColor, newPen(ArrowBorderColor, 1), ArrowDraw());
        }
privateStreamGeometryArrowDraw() 
        {
StreamGeometrystreamGeometry=newStreamGeometry();
varpoint0=newPoint(this.ActualWidth/3,0);
varpoint1=newPoint(this.ActualWidth/3*2, 0);
varpoint2=newPoint(this.ActualWidth/3*2, this.ActualHeight/3*2);
varpoint3=newPoint(this.ActualWidth, this.ActualHeight/3*2);
varpoint4=newPoint(this.ActualWidth/2, this.ActualHeight);
varpoint5=newPoint(0, this.ActualHeight/3*2);
varpoint6=newPoint(this.ActualWidth/3,this.ActualHeight/3*2);
using (StreamGeometryContextstreamGeometryContext=streamGeometry.Open()) 
            {
streamGeometryContext.BeginFigure(point0,true,false);
streamGeometryContext.LineTo(point1, true, true);
streamGeometryContext.LineTo(point2, true, true);
streamGeometryContext.LineTo(point3, true, true);
streamGeometryContext.LineTo(point4, true, true);
streamGeometryContext.LineTo(point5, true, true);
streamGeometryContext.LineTo(point6, true, true);
streamGeometryContext.LineTo(point0, true, true);
            }
returnstreamGeometry;
        }
    }
}

3.重新生成下项目,打开视图->工具箱,我们回到ArrowView的设计窗口,在工具箱中我们就能够找到我们刚才创建的自定义箭头控件Arrow,将其拖到我们需要显示的窗口,然后设置我们刚才定义的一些属性。

<CustomControl:Arrow ArrowColor="AliceBlue" ArrowBorder="1" ArrowBorderColor="Red" Height="100" Width="100"/>

4.至此,我们可以通过自定义控件创建出箭头控件了,可以通过此方法创建出更复杂的控件。

目录
相关文章
|
1月前
|
C# 数据库 开发者
44.c#:combobox控件
44.c#:combobox控件
19 1
|
1月前
|
C# 数据库
40.c#:TreeView 控件
40.c#:TreeView 控件
19 1
|
6月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
37 0
|
1月前
|
C# Windows
49.c#:StatusStrip 控件
49.c#:StatusStrip 控件
26 1
49.c#:StatusStrip 控件
|
1月前
|
C# 开发者 Windows
48.c#:toolstrip控件
48.c#:toolstrip控件
17 1
|
1月前
|
C# Windows
47.c#:menustrip控件
47.c#:menustrip控件
15 1
|
1月前
|
存储 缓存 C#
46.c#:datagridview控件
46.c#:datagridview控件
24 1
|
1月前
|
C#
45.c#:listview控件
45.c#:listview控件
12 1
|
1月前
|
C# 数据库 虚拟化
43.c#:listbox控件
43.c#:listbox控件
16 1
|
1月前
|
数据处理 C# UED
42.c#:progressbar控件
42.c#:progressbar控件
16 1