资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#实现创建自定义控件-创新互联

C#实现创建自定义控件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联建站专注于牧野网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供牧野营销型网站建设,牧野网站制作、牧野网页设计、牧野网站官网定制、微信小程序定制开发服务,打造牧野网络公司原创品牌,更为您提供牧野网站排名全网营销落地服务。

1.创建自定义控件

C#实现创建自定义控件

2.添加控件,组合成一个新的控件

自定义控件功能:打开一张图片,将图片展示在pictureBox控件中,并将图片的名称、大小、尺寸显示出来

控件如下:

pictureBox1:命名为picBox

label1~label6 :左边三个显示文字,右边三个命名为:lblName lblLength lblSize

button1:命名为btnOpen

C#实现创建自定义控件

代码如下:

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

网站名称:C#实现创建自定义控件-创新互联
网页地址:http://cdkjz.cn/article/dsogso.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220