如何在C#中创建自定义控件?
所以我正在开发一个C程序,它的功能类似于IDE。 问题是,我是通过一个富文本框来实现的,自定义语法高亮显示效率非常低 我想知道是否有一种方法可以使自定义控件类似于富文本框(甚至基于富文本框),然后以更简单的方式构建语法高亮显示如何在C#中创建自定义控件?,c#,winforms,controls,C#,Winforms,Controls,所以我正在开发一个C程序,它的功能类似于IDE。 问题是,我是通过一个富文本框来实现的,自定义语法高亮显示效率非常低 我想知道是否有一种方法可以使自定义控件类似于富文本框(甚至基于富文本框),然后以更简单的方式构建语法高亮显示 那么,如何创建自定义控件?如果你非常好,你会给我指出一个关于它的好教程的方向 很容易从基本控件派生 public class CustomRichTextBox:RichTextBox { public CustomRichTextBox() {
那么,如何创建自定义控件?如果你非常好,你会给我指出一个关于它的好教程的方向 很容易从基本控件派生
public class CustomRichTextBox:RichTextBox
{
public CustomRichTextBox()
{
this.Multiline = true;
this.WordWrap = false;
this.AcceptsTab = true;
//...
}
}
我建议看一些教程,比如 正如上面NavidRahmani所说,您不必从头开始—您可以扩展现有控件或使用已从不同库生成的控件
例如telerik控件。简单但功能强大的示例: Windows窗体: WPF:
不,工具箱中没有一个控件试图假装是一个好的编辑器控件。TextBox和RichTextBox有非常不同的设计目标。这个问题已经解决了很多次,就我所知,这个问题已经解决了。我发现本教程非常有用。希望这将对你帮助最大 它很简单,只需调用图形方法并绘制与控件对应的形状 假设您想创建一个自定义按钮,您知道按钮在系统中是一个矩形,所以只需使用C#中的LinearGradientBrush或SolidBrush in Paint()方法使用按钮坐标(x、y、宽度、高度)填充矩形即可。C#提供了ClientRectangle方法来获取使用矩形的控件的所有坐标。有许多使用矩形的组件,如按钮、面板、复选框、选项卡控件等 我认为这些链接也很有用 这里是完整的源代码,如果链接表单被发现损坏
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace Custom_Controls_in_CS
{
public class ButtonZ : System.Windows.Forms.Button
{
Color clr1, clr2;
private Color color1 = Color.DodgerBlue;
private Color color2 = Color.MidnightBlue;
private Color m_hovercolor1 = Color.Turquoise;
private Color m_hovercolor2 = Color.DarkSlateGray;
private int color1Transparent = 250;
private int color2Transparent = 250;
private Color clickcolor1 = Color.Yellow;
private Color clickcolor2 = Color.Red;
private int angle = 90;
private int textX = 100;
private int textY = 25;
private String text = "";
public Color buttonborder_1 = Color.FromArgb(220, 220, 220);
public Color buttonborder_2 = Color.FromArgb(150, 150, 150);
public Boolean showButtonText = true;
public int borderWidth = 2;
public Color borderColor = Color.Transparent;
public enum ButtonsShapes
{
Rect,
RoundRect,
Circle
}
ButtonsShapes buttonShape;
public ButtonsShapes ButtonShape
{
get { return buttonShape; }
set
{
buttonShape = value; Invalidate();
}
}
public String ButtonText
{
get { return text; }
set { text = value; Invalidate(); }
}
public int BorderWidth
{
get { return borderWidth; }
set { borderWidth = value; Invalidate(); }
}
void SetBorderColor(Color bdrColor)
{
int red = bdrColor.R - 40;
int green = bdrColor.G - 40;
int blue = bdrColor.B - 40;
if (red < 0)
red = 0;
if (green < 0)
green = 0;
if (blue < 0)
blue = 0;
buttonborder_1 = Color.FromArgb(red, green, blue);
buttonborder_2 = bdrColor;
}
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
if (borderColor == Color.Transparent)
{
buttonborder_1 = Color.FromArgb(220, 220, 220);
buttonborder_2 = Color.FromArgb(150, 150, 150);
}
else
{
SetBorderColor(borderColor);
}
}
}
public Color StartColor
{
get { return color1; }
set { color1 = value; Invalidate(); }
}
public Color EndColor
{
get { return color2; }
set { color2 = value; Invalidate(); }
}
public Color MouseHoverColor1
{
get { return m_hovercolor1; }
set { m_hovercolor1 = value; Invalidate(); }
}
public Color MouseHoverColor2
{
get { return m_hovercolor2; }
set { m_hovercolor2 = value; Invalidate(); }
}
public Color MouseClickColor1
{
get { return clickcolor1; }
set { clickcolor1 = value; Invalidate(); }
}
public Color MouseClickColor2
{
get { return clickcolor2; }
set { clickcolor2 = value; Invalidate(); }
}
public int Transparent1
{
get { return color1Transparent; }
set
{
color1Transparent = value;
if (color1Transparent > 255)
{
color1Transparent = 255;
Invalidate();
}
else
Invalidate();
}
}
public int Transparent2
{
get { return color2Transparent; }
set
{
color2Transparent = value;
if (color2Transparent > 255)
{
color2Transparent = 255;
Invalidate();
}
else
Invalidate();
}
}
public int GradientAngle
{
get { return angle; }
set { angle = value; Invalidate(); }
}
public int TextLocation_X
{
get { return textX; }
set { textX = value; Invalidate(); }
}
public int TextLocation_Y
{
get { return textY; }
set { textY = value; Invalidate(); }
}
public Boolean ShowButtontext
{
get { return showButtonText; }
set { showButtonText = value; Invalidate(); }
}
public ButtonZ()
{
this.Size = new Size(100, 40);
this.BackColor = Color.Transparent;
this.FlatStyle = FlatStyle.Flat;
this.FlatAppearance.BorderSize = 0;
this.FlatAppearance.MouseOverBackColor = Color.Transparent;
this.FlatAppearance.MouseDownBackColor = Color.Transparent;
text = this.Text;
}
//method mouse enter
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
clr1 = color1;
clr2 = color2;
color1 = m_hovercolor1;
color2 = m_hovercolor2;
}
//method mouse leave
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
color1 = clr1;
color2 = clr2;
SetBorderColor(borderColor);
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
base.OnMouseDown(mevent);
color1 = clickcolor1;
color2 = clickcolor2;
int red = borderColor.R - 40;
int green = borderColor.G - 40;
int blue = borderColor.B - 40;
if (red < 0)
red = 0;
if (green < 0)
green = 0;
if (blue < 0)
blue = 0;
buttonborder_2 = Color.FromArgb(red, green, blue);
buttonborder_1 = borderColor;
this.Invalidate();
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
OnMouseLeave(mevent);
color1 = clr1;
color2 = clr2;
SetBorderColor(borderColor);
this.Invalidate();
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
color1 = clr1;
color2 = clr2;
this.Invalidate();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
textX = (int)((this.Width / 3) - 1);
textY = (int)((this.Height / 3) + 5);
}
//draw circular button function
void DrawCircularButton(Graphics g)
{
Color c1 = Color.FromArgb(color1Transparent, color1);
Color c2 = Color.FromArgb(color2Transparent, color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
g.FillEllipse(b, 5, 5, this.Width - 10, this.Height - 10);
for (int i = 0; i < borderWidth; i++)
{
g.DrawArc(new Pen(new SolidBrush(buttonborder_1)), 5 + i, 5, this.Width - 10, this.Height - 10, 120, 180);
g.DrawArc(new Pen(new SolidBrush(buttonborder_2)), 5, 5, this.Width - (10 + i), this.Height - 10, 300, 180);
}
if (showButtonText)
{
Point p = new Point(textX, textY);
SolidBrush frcolor = new SolidBrush(this.ForeColor);
g.DrawString(text, this.Font, frcolor, p);
}
b.Dispose();
}
//draw rectangular button function
void DrawRectangularButton(Graphics g)
{
Color c1 = Color.FromArgb(color1Transparent, color1);
Color c2 = Color.FromArgb(color2Transparent, color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
g.FillRectangle(b, 0, 0, this.Width, this.Height);
for (int i = 0; i < borderWidth; i++)
{
g.DrawLine(new Pen(new SolidBrush(buttonborder_1)), this.Width - i, 0, this.Width - i, this.Height);
g.DrawLine(new Pen(new SolidBrush(buttonborder_1)), 0, this.Height - i, this.Width, this.Height - i);
g.DrawLine(new Pen(new SolidBrush(buttonborder_2)), 0 + i, 0, 0 + i, this.Height);
g.DrawLine(new Pen(new SolidBrush(buttonborder_2)), 0, 0 + i, this.Width, i);
}
if (showButtonText)
{
Point p = new Point(textX, textY);
SolidBrush frcolor = new SolidBrush(this.ForeColor);
g.DrawString(text, this.Font, frcolor, p);
}
b.Dispose();
}
//draw round rectangular button function
void DrawRoundRectangularButton(Graphics g)
{
Color c1 = Color.FromArgb(color1Transparent, color1);
Color c2 = Color.FromArgb(color2Transparent, color2);
Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
Region region = new System.Drawing.Region(new Rectangle(5, 5, this.Width, this.Height));
GraphicsPath grp = new GraphicsPath();
grp.AddArc(5, 5, 40, 40, 180, 90);
grp.AddLine(25, 5, this.Width - 25, 5);
grp.AddArc(this.Width - 45, 5, 40, 40, 270, 90);
grp.AddLine(this.Width - 5, 25, this.Width - 5, this.Height - 25);
grp.AddArc(this.Width - 45, this.Height - 45, 40, 40, 0, 90);
grp.AddLine(25, this.Height - 5, this.Width - 25, this.Height - 5);
grp.AddArc(5, this.Height - 45, 40, 40, 90, 90);
grp.AddLine(5, 25, 5, this.Height - 25);
region.Intersect(grp);
g.FillRegion(b, region);
for (int i = 0; i < borderWidth; i++)
{
g.DrawArc(new Pen(buttonborder_1), 5 + i, 5 + i, 40, 40, 180, 90);
g.DrawLine(new Pen(buttonborder_1), 25, 5 + i, this.Width - 25, 5 + i);
g.DrawArc(new Pen(buttonborder_1), this.Width - 45 - i, 5 + i, 40, 40, 270, 90);
g.DrawLine(new Pen(buttonborder_1), 5 + i, 25, 5 + i, this.Height - 25);
g.DrawLine(new Pen(buttonborder_2), this.Width - 5 - i, 25, this.Width - 5 - i, this.Height - 25);
g.DrawArc(new Pen(buttonborder_2), this.Width - 45 - i, this.Height - 45 - i, 40, 40, 0, 90);
g.DrawLine(new Pen(buttonborder_2), 25, this.Height - 5 - i, this.Width - 25, this.Height - 5 - i);
g.DrawArc(new Pen(buttonborder_2), 5 + i, this.Height - 45 - i, 40, 40, 90, 90);
}
if (showButtonText)
{
Point p = new Point(textX, textY);
SolidBrush frcolor = new SolidBrush(this.ForeColor);
g.DrawString(text, this.Font, frcolor, p);
}
b.Dispose();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
switch (buttonShape)
{
case ButtonsShapes.Circle:
this.DrawCircularButton(e.Graphics);
break;
case ButtonsShapes.Rect:
this.DrawRectangularButton(e.Graphics);
break;
case ButtonsShapes.RoundRect:
this.DrawRoundRectangularButton(e.Graphics);
break;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用系统图;
使用系统数据;
使用System.Windows.Forms;
使用System.Drawing.Drawing2D;
命名空间自定义\u控件\u在\u CS中
{
公共类按钮nz:System.Windows.Forms.Button
{
颜色clr1,clr2;
专用颜色color1=Color.DodgerBlue;
专用颜色color2=Color.MidnightBlue;
私有颜色m_hovercolor1=颜色。绿松石色;
私有颜色m_hovercolor2=Color.DarkSlateGray;
私有int color1Transparent=250;
私有int color2Transparent=250;
私有颜色clickcolor1=Color.Yellow;
私有颜色clickcolor2=Color.Red;
私人内角=90;
私有int textX=100;
私有int textY=25;
私有字符串text=“”;
公共颜色按钮顺序_1=Color.FromArgb(220,220,220);
公共颜色按钮顺序_2=Color.FromArgb(150150150);
公共布尔值showButtonText=true;
公共int-borderWidth=2;
公共颜色边框颜色=颜色。透明;
公共枚举按钮形状
{
直肠,
RoundRect,
圆圈
}
钮扣形状钮扣形状;
公共按钮形状按钮形状
{
获取{return buttonShape;}
设置
{
buttonShape=值;无效();
}
}
公共字符串按钮文本
{
获取{返回文本;}
设置{text=value;Invalidate();}
}
公共整数边框宽度
{
获取{return borderWidth;}
设置{borderWidth=value;Invalidate();}
}
无效颜色(颜色bdrColor)
{
int red=bdrColor.R-40;
int绿色=bdrColor.G-40;
int blue=bdrColor.B-40;
如果(红色<0)
红色=0;
如果(绿色<0)
绿色=0;
如果(蓝色<0)
蓝色=0;
ButtonOrder_1=颜色。来自argb(红色、绿色、蓝色);
按钮顺序_2=BDR颜色;
}
公共颜色边框颜色
{
获取{return borderColor;}
设置
{
边框颜色=值;
if(borderColor==Color.Transparent)
{
ButtonOrder_1=颜色。来自argb(220、220、220);
ButtonOrder_2=颜色。来自argb(150、150、150);
}
其他的
{
颜色(边框颜色);
}
}
}
公共色星色
{
获取{return color1;}
设置{color1=value;Invalidate();}
}
公共色底色
{
获取{return color2;}
设置{color2=value;Invalidate();}
}
公共颜色鼠标套颜色1
{
获取{return m_hovercolor1;}
设置{m_hovercolor1=value;Invalidate();}
}
公共颜色鼠标套颜色2
{
获取{return m_hovercolor2;}
设置{m_hovercolor2=value;使无效();}
}
公共色鼠标颜色1
{
获取{return clickcolor1;}
设置{clickcolor1=value;无效();}
}
公共色鼠标颜色2
{
获取{return clickcolor2;}
设置{clickcolor2=值;无效();}
}
公共int透明1
{
获取{return color1Transparent;}
设置
{
Color1透明度=数值;
如果(颜色1透明度>255)
{
彩色1透明