C#實現創(chuàng)建自定義控件-創(chuàng)新互聯

C#實現創(chuàng)建自定義控件?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

創(chuàng)新互聯建站專注于牧野網站建設服務及定制,我們擁有豐富的企業(yè)做網站經驗。 熱誠為您提供牧野營銷型網站建設,牧野網站制作、牧野網頁設計、牧野網站官網定制、微信小程序定制開發(fā)服務,打造牧野網絡公司原創(chuàng)品牌,更為您提供牧野網站排名全網營銷落地服務。

1.創(chuàng)建自定義控件

C#實現創(chuàng)建自定義控件

2.添加控件,組合成一個新的控件

自定義控件功能:打開一張圖片,將圖片展示在pictureBox控件中,并將圖片的名稱、大小、尺寸顯示出來

控件如下:

pictureBox1:命名為picBox

label1~label6 :左邊三個顯示文字,右邊三個命名為:lblName lblLength lblSize

button1:命名為btnOpen

C#實現創(chuàng)建自定義控件

代碼如下:

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#實現創(chuàng)建自定義控件-創(chuàng)新互聯
鏈接URL:http://www.muchs.cn/article30/dsogso.html

成都網站建設公司_創(chuàng)新互聯,為您提供虛擬主機、微信公眾號、做網站、App開發(fā)搜索引擎優(yōu)化、軟件開發(fā)

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯

小程序開發(fā)