当我们做c#开发时候,我们可以将常用到的功能直接开发成组件,当下次需要相同的功能时候,我们直接调用自己开发好的控件库即可,方便省事。入门资料可以参考这位博主的 https://www.cnblogs.com/yangfengwu/p/5956841.html。
这里我介绍我简单的几个自定义控件:
1、时间控件:
主要代码实现如下:
public void LetThinkGetSysTime()
{
dt = DateTime.Today.DayOfWeek.ToString();
switch (dt)
{
case "Monday":
week = "星期一";
break;
case "Tuesday":
week = "星期二";
break;
case "Wednesday":
week = "星期三";
break;
case "Thursday":
week = "星期四";
break;
case "Friday":
week = "星期五";
break;
case "Saturday":
week = "星期六";
break;
case "Sunday":
week = "星期日";
break;
}
System.Threading.Thread P_thread = //创建线程
new System.Threading.Thread(
() =>//使用lambda表达式
{
while (true)//无限循环
{
this.Invoke(//操作窗体线程
(MethodInvoker)delegate ()//使用匿名方法
{
this.Refresh();//刷新窗体
Graphics P_Graphics = //创建绘图对象
CreateGraphics();
P_Graphics.DrawString(//在窗体中绘出系统时间
DateTime.Now.ToString(
"yyyy年MM月dd日 HH时mm分ss秒 ")+week,
new Font("宋体", 12),
Brushes.Blue,
new Point(5, 10));
});
System.Threading.Thread.Sleep(1000);//线程挂起1秒钟
}
});
P_thread.IsBackground = true;//将线程设置为后台线程
P_thread.Start();//线程开始执行
}
2、简单的Login
3、中文验证码
主要代码实现:
private string CheckCode() //此方法生成
{
int number;
char code;
string checkCode = String.Empty; //声明变量存储随机生成的4位英文或数字
Random random = new Random(); //生成随机数
for (int i = 0; i < 4; i++)
{
number = random.Next(); //返回非负随机数
if (number % 2 == 0) //判断数字是否为偶数
code = (char)('0' + (char)(number % 10));
else //如果不是偶数
code = (char)('A' + (char)(number % 26));
checkCode += " " + code.ToString(); //累加字符串
}
Code = checkCode;
return checkCode; //返回生成的字符串
}
private void CodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 9.5)), 22);
Graphics g = Graphics.FromImage(image); //创建Graphics对象
try
{
Random random = new Random(); //生成随机生成器
g.Clear(Color.White); //清空图片背景色
for (int i = 0; i < 3; i++) //画图片的背景噪音线
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold));
g.DrawString(checkCode, font, new SolidBrush(Color.Red), 2, 2);
for (int i = 0; i < 150; i++) //画图片的前景噪音点
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
this.pictureBox1.Width = image.Width; //设置PictureBox的宽度
this.pictureBox1.Height = image.Height; //设置PictureBox的高度
this.pictureBox1.BackgroundImage = image; //设置PictureBox的背景图像
}
catch
{ }
}
4、委托实现的一个接收框:
主要代码实现:
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public delegate void UpdateRecDelegate(string str);
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public void DisplayText(string str)
{
if (TextBox_ShowMessage.InvokeRequired)
{
UpdateRecDelegate md = new UpdateRecDelegate(DisplayText);
TextBox_ShowMessage.Invoke(md, new object[] { str });
}
else
{
TextBox_ShowMessage.Text += str + Environment.NewLine;
}
}
5、串口程序: