这篇文章主要介绍了C#创建自定义控件的示例,帮助大家更好的理解和学习c#,感兴趣的朋友可以了解下
在编程过程中,现有的c#控件远远不能满足我们的需要,这时候就需要我们自己来开发控件了。本人在开发自定义控件时走了一些弯路,写下此篇,希望能够给有需要的朋友一些帮助,也借此加深自己的印象。
1.创建自定义控件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsControlLibrary1
{
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofdPic = new OpenFileDialog();
ofdPic.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF);PNG(*.PNG)|*.jpg;*.jpeg;*.gif;*.png";
ofdPic.FilterIndex = 1;
ofdPic.RestoreDirectory = true;
ofdPic.FileName = "";
if (ofdPic.ShowDialog() == DialogResult.OK)
{
string sPicPaht = ofdPic.FileName.ToString();
FileInfo fiPicInfo = new FileInfo(sPicPaht);
long lPicLong = fiPicInfo.Length / 1024;
string sPicName = fiPicInfo.Name;
string sPicDirectory = fiPicInfo.Directory.ToString();
string sPicDirectoryPath = fiPicInfo.DirectoryName;
Bitmap bmPic = new Bitmap(sPicPaht);
if (lPicLong > 400)
{
MessageBox.Show("此文件大小為" + lPicLong + "K;已超過最大限制的K范圍!");
}
else
{
Point ptLoction = new Point(bmPic.Size);
if (ptLoction.X > picBox.Size.Width || ptLoction.Y > picBox.Size.Height)
{
picBox.SizeMode = PictureBoxSizeMode.Zoom;
}
else
{
picBox.SizeMode = PictureBoxSizeMode.CenterImage;
}
}
picBox.LoadAsync(sPicPaht);
lblName.Text = sPicName;
lblLength.Text = lPicLong.ToString() + " KB";
lblSize.Text = bmPic.Size.Width.ToString() + "×" + bmPic.Size.Height.ToString();
}
}
}
}
点击【解决方案】,右键弹出窗口,点击【生成解决方案】
至此,自定义控件的创建已经完成!
生成的控件路径在Debug文件夹下,dll文件
3.自定义控件测试
新建windows窗体应用程序
发现在左边的控件工具栏中并没有刚刚的自定义控件,不要急!!
选择工具下的【选择工具箱项】
以上就是C#创建自定义控件的示例的详细内容,更多关于C#创建自定义控件的资料请关注得得之家其它相关文章!
沃梦达教程
本文标题为:C#创建自定义控件的示例
猜你喜欢
- c# 模拟线性回归的示例 2023-03-14
- user32.dll 函数说明小结 2022-12-26
- C# 使用Aspose.Cells 导出Excel的步骤及问题记录 2023-05-16
- Oracle中for循环的使用方法 2023-07-04
- 在C# 8中如何使用默认接口方法详解 2023-03-29
- WPF使用DrawingContext实现绘制刻度条 2023-07-04
- Unity3D实现渐变颜色效果 2023-01-16
- 如何使用C# 捕获进程输出 2023-03-10
- .NET CORE DI 依赖注入 2023-09-27
- Unity Shader实现模糊效果 2023-04-27