首页 > 其他分享 >Winform CustomControl这样写

Winform CustomControl这样写

时间:2022-11-08 19:07:35浏览次数:40  
标签:Width Color CustomControl 40 int new public 这样 Winform


Winform CustomControl这样写

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ClassLibrary1
{
public partial class CustomControl1 : Button
{
public CustomControl1()
{
InitializeComponent();

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;
}

protected override void OnPaint(PaintEventArgs e)
{
// TODO: 在此处添加自定义绘制代码

// 调用基类 OnPaint
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;
}
}

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(); }
}

//method mouse enter
protected override void onm ouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
clr1 = color1;
clr2 = color2;
color1 = m_hovercolor1;
color2 = m_hovercolor2;
}
//method mouse leave
protected override void onm ouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
color1 = clr1;
color2 = clr2;
SetBorderColor(borderColor);
}

protected override void onm ouseDown(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 onm ouseUp(MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
onm ouseLeave(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();
}
}
}

运行之后效果是这样的

Winform CustomControl这样写_2d

那几个带颜色的按钮就是自定义的控件。


标签:Width,Color,CustomControl,40,int,new,public,这样,Winform
From: https://blog.51cto.com/u_13654233/5834432

相关文章

  • 一个超经典 WinForm 卡死问题的再反思
    一:背景1.讲故事这篇文章起源于昨天的一位朋友发给我的dump文件,说它的程序出现了卡死,看了下程序的主线程栈,居然又碰到了OnUserPreferenceChanged导致的挂死问题,真的是经......
  • winform对于菜单项禁用的理解
    1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;5usingSystem.Drawing;6usingSystem.Windows.Forms......
  • Winform窗体常用属性
    一、WinForm:客户端程序制作-C/S(B/S:服务器端)它是基于.NETFramework框架上运行,不是必须在windows系统上才能运行---------------------------------------------------......
  • 模板匹配(createTrackbar函数这样用)
    一、模板匹配模板匹配(TemplateMatching)就是在一幅图像中寻找和模板图像(template)最相似的区域,该方法原理简单计算速度快,能够应用于目标识别,目标跟踪等多个领域。二、原理......
  • 高手是这样排查问题的——两层使用存储过程批量生成单据和查询分析器生成单据都正确,使
    问题描述:在两层的情况下,使用存储过程批量生成单据时,正确在查询分析器中,直接使用代码执行存储过程时,正确在三层的情况下,使用存储过程批量生成单据时,错误 问题查找:1、......
  • 使用Winform绘制仿造微信的客户
    由于工作需要,开发一个聊天工具,安卓,IOS,Web都开发好了,还需要一个客户端形式的聊天工具,要求是简介大方好用,呵呵哒,好看我不知道,但是模仿一个到时可以的,说干就干,经过几天的时......
  • winform 窗口布局自适应分辨率
    网上相关的资料都比较多,我觉得太麻烦,忽然想起网页字体用px为单位则不会随分辨率大小变化,不知道winform是否也可以这样设置。于是就把窗体和设置过字体的控件的字体单位都改......
  • 手把手教你用DevExpress WinForm添加和自定义工具栏皮肤选择器
    DevExpressWinForm拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office......
  • c# winform使用NOPI读取Excel读取图片
    需求:在Winform使用NOPI做导入时候,需要导入数据的同时导入图片。虽然代码方面不适用(我好像也没仔细看过代码),但是感谢大佬给了灵感http://www.wjhsh.net/IT-Ramon-p-13100......
  • 如果你是项目主管,你会不会炒了这样刁难你的客户?
    文/王不留(微信公众号:程序员生存指南) 一位兄弟向我抱怨,不想干项目经理了,客户处处刁难他,天天找他麻烦。一天24小时不让他安宁。现在只想两耳不闻窗外事,一心只想写代码。 ......