本文以一個用戶控件【User Control】實現(xiàn)溫度計的小例子,簡述用戶控件的相關(guān)知識,以供學(xué)習(xí)分享使用,如有不足之處,還請指正。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比溫縣網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式溫縣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋溫縣地區(qū)。費用合理售后完善,10年實體公司更值得信賴。
概述
一般而言,用戶控件【User Control】,是在Visual Studio提供的默認(rèn)控件不能滿足實際的工作需要,才需要在現(xiàn)有控件的基礎(chǔ)之上進行新的擴展,來實現(xiàn)自己的功能。用戶控件有自己特有的屬性,事件,方法來支撐特定的功能。用戶控件封裝實現(xiàn)的細節(jié),可以進行方便的復(fù)用。
用戶控件分類
用戶控件主要有以下三種分類,本例采用的是第三種。
涉及知識點
本例中主要涉及以下知識點:
示例效果圖
本例中示例效果圖如下所示:
關(guān)鍵代碼
本例中的關(guān)鍵代碼如下:
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; namespace DemoThermometer { public partial class Thermometer : UserControl { #region 屬性與構(gòu)造函數(shù) private int interval = 10; /// <summary> /// 刻度間隔 /// </summary> public int Interval { get { return interval; } set { interval = value; } } private int minValue = -50; /// <summary> /// 最低溫度 /// </summary> public int MinValue { get { return minValue; } set { minValue = value; } } private int maxValue = 50; /// <summary> /// 最高溫度 /// </summary> public int MaxValue { get { return maxValue; } set { maxValue = value; } } private float curValue = 0; /// <summary> /// 當(dāng)前溫度 /// </summary> public float CurValue { get { return curValue; } set { curValue = value; } } private Color thermoColor = Color.Red; /// <summary> /// 溫度條顏色 /// </summary> public Color ThermoColor { get { return thermoColor; } set { thermoColor = value; } } private Color backGroundColor = Color.SkyBlue; /// <summary> /// 溫度計背景色 /// </summary> public Color BackGroundColor { get { return backGroundColor; } set { backGroundColor = value; } } private Font thermoFont=new Font("宋體",10,FontStyle.Regular); /// <summary> /// 溫度計上字體 /// </summary> public Font ThermoFont { get { return thermoFont; } set { thermoFont = value; } } private string thermoTitle = "溫度計"; /// <summary> /// 標(biāo)題 /// </summary> public string ThermoTitle { get { return this.thermoTitle; } set { this.thermoTitle = value; } } private bool showTip = false; /// <summary> /// 是否顯示提示 /// </summary> public bool ShowTip { get { return showTip; } set { showTip = value; } } private ToolTip tip=new ToolTip(); public Thermometer() { InitializeComponent(); } #endregion protected override void OnPaint(PaintEventArgs e) { //base.OnPaint(e); //this.BackColor = this.backGroundColor; int width = this.Width; int height = this.Height - 50 ; Graphics g = e.Graphics; int c_x = width / 2; int c_y = height / 2; int padding = this.Padding.All;//空白 int r = (width - 2 * padding)/2;//半徑 int d = 2 * r;//直徑 int dis = 2;//兩個半圓之間的間隔 int dis2 = 2 * dis;//填充與邊框之間的距離 int startAngle1 = -180; int startAngle2 = 0; int sweepAngle1 = 180; //首先畫頂端一個半圓 g.DrawPie(Pens.Black, new Rectangle(padding, padding, d, d),startAngle1, sweepAngle1); g.DrawPie(Pens.Black, new Rectangle(padding+ dis, padding+ dis, d-2* dis, d-2* dis), startAngle1, sweepAngle1); //填充背景色 g.FillPie(new SolidBrush(this.backGroundColor), new Rectangle(padding + dis2, padding + dis2, d - 2*dis2, d - 2*dis2), startAngle1, sweepAngle1); //畫底端一個半圓 g.DrawPie(Pens.Black, new Rectangle(padding, height-d-padding, d, d), startAngle2, sweepAngle1); g.DrawPie(Pens.Black, new Rectangle(padding + dis, height-d-padding + dis, d - 2*dis, d - 2*dis), startAngle2, sweepAngle1); g.FillPie(new SolidBrush(this.backGroundColor), new Rectangle(padding + dis2, height - d - padding+dis2, d - 2*dis2, d - 2*dis2), startAngle2, sweepAngle1); //畫一個矩形 g.DrawRectangle(Pens.Black, new Rectangle(padding, padding + r, d, height - d - 2 * padding)); g.DrawRectangle(Pens.Black, new Rectangle(padding+dis, padding + r+dis, d-2*dis, height - d - 2 * padding-2*dis)); //背景色填充,去掉邊界線 g.FillRectangle(new SolidBrush(this.backGroundColor), new Rectangle(padding+3, padding + r-2, 2 * r-6, 6)); g.FillRectangle(new SolidBrush(this.backGroundColor), new Rectangle(padding + 3, height-r-padding-4, 2 * r - 6, 8)); //背景色填充中間部分 g.FillRectangle(new SolidBrush(this.backGroundColor), new Rectangle(padding + dis2, padding + r + dis2, d - 2*dis2, height -d - 2 * padding - 2*dis2)); //畫刻度 int s_s_x_1 = padding + r - 20; int s_s_x_2 = width-padding - r + 20; int s_s_y = padding + r+4; int total = this.maxValue - this.minValue; int scale_width = 5;//刻度寬度 int scale = total / this.interval; int pscale = (height - 2 * r - 2 * padding) / this.interval;//像素間隔 //豎線 g.DrawLine(Pens.Black, new Point(s_s_x_1, s_s_y ), new Point(s_s_x_1, s_s_y + this.interval* pscale)); g.DrawLine(Pens.Black, new Point(s_s_x_2, s_s_y), new Point(s_s_x_2, s_s_y + this.interval * pscale)); for (int i = 0; i <= this.interval; i++) { //橫線刻度 g.DrawLine(Pens.Black, new Point(s_s_x_1- scale_width, s_s_y + i * pscale), new Point(s_s_x_1, s_s_y + i * pscale)); g.DrawLine(Pens.Black, new Point(s_s_x_2, s_s_y + i * pscale), new Point(s_s_x_2 + scale_width, s_s_y + i * pscale)); //畫刻度數(shù)字 g.DrawString((this.maxValue - (scale * i)).ToString(), this.thermoFont, new SolidBrush( this.ForeColor), new Point(s_s_x_1-35, s_s_y + i * pscale-10)); g.DrawString((this.maxValue - (scale * i)).ToString(), this.thermoFont, new SolidBrush(this.ForeColor), new Point(s_s_x_2 + 10, s_s_y + i * pscale-10)); } int white_width = 3;//中間白色線寬度 //畫條白色細線 g.FillRectangle(Brushes.White, new Rectangle(c_x- white_width, r/2, white_width*2, height-r)); //在底部畫一個圓球 g.FillPie(new SolidBrush(this.thermoColor), new Rectangle(c_x-r/2+5, height - r - padding, r-10, r-10), 0, 360); //根據(jù)當(dāng)前溫度畫紅色線 int red_width = 5;//紅色溫度線寬度 float ii = ( this.curValue-this.minValue) / this.interval; g.FillRectangle(new SolidBrush(this.thermoColor), new RectangleF(c_x - red_width, height - r - padding- (ii * pscale)-4, 2* red_width, ii * pscale+5));//此處有一像素的誤差 //畫標(biāo)志字符單℃位 g.DrawString("℃", this.thermoFont, new SolidBrush(this.ForeColor), new Point(c_x - 30, r / 2 - 10)); Font titleFont = new Font("宋體", 13, FontStyle.Bold); //繪制標(biāo)題 SizeF tsize = g.MeasureString(this.thermoTitle, titleFont); g.DrawString(this.thermoTitle, titleFont, new SolidBrush(this.ForeColor), new PointF(c_x- (tsize.Width/2), height + 5)); string cur = string.Format("當(dāng)前溫度:{0}℃", this.curValue); SizeF tsize2 = g.MeasureString(cur, this.thermoFont); g.DrawString(cur, this.thermoFont, new SolidBrush(this.thermoColor), new PointF(c_x - (tsize2.Width / 2), height + 10+tsize.Height)); } /// <summary> /// 當(dāng)鼠標(biāo)覆蓋進去時 /// </summary> /// <param name="e"></param> protected override void OnMouseHover(EventArgs e) { this.showTip = true; //需要顯示的內(nèi)容 int x = this.Width / 2; int y = (this.Height-50) / 2; StringBuilder sbTips = new StringBuilder(); //sbTips.AppendLine(this.ThermoTitle); sbTips.AppendLine(string.Format("當(dāng)前溫度:{0}", this.curValue)); sbTips.AppendLine("單位:℃"); tip.ToolTipTitle = this.ThermoTitle; tip.IsBalloon = true; tip.UseFading = true; //t.SetToolTip(this, sbTips.ToString()); tip.Show(sbTips.ToString(), this, x, y); } protected override void OnMouseLeave(EventArgs e) { this.showTip = false; tip.Hide(this); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文題目:C#用戶控件之溫度計設(shè)計
新聞來源:http://jinyejixie.com/article8/ghhhip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、App開發(fā)、標(biāo)簽優(yōu)化、Google、企業(yè)建站、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)