C#用戶(hù)控件之溫度計(jì)設(shè)計(jì)
本文以一個(gè)用戶(hù)控件【User Control】實(shí)現(xiàn)溫度計(jì)的小例子,簡(jiǎn)述用戶(hù)控件的相關(guān)知識(shí),以供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正。
概述
一般而言,用戶(hù)控件【User Control】,是在Visual Studio提供的默認(rèn)控件不能滿(mǎn)足實(shí)際的工作需要,才需要在現(xiàn)有控件的基礎(chǔ)之上進(jìn)行新的擴(kuò)展,來(lái)實(shí)現(xiàn)自己的功能。用戶(hù)控件有自己特有的屬性,事件,方法來(lái)支撐特定的功能。用戶(hù)控件封裝實(shí)現(xiàn)的細(xì)節(jié),可以進(jìn)行方便的復(fù)用。
用戶(hù)控件分類(lèi)
用戶(hù)控件主要有以下三種分類(lèi),本例采用的是第三種。
- 復(fù)合控件(Composite Controls):將現(xiàn)有的各種控件組合起來(lái),形成一個(gè)新的控件,來(lái)滿(mǎn)足用戶(hù)的需求。
- 擴(kuò)展控件(Extended Controls):就是在現(xiàn)有的控件基礎(chǔ)上,派生出一個(gè)新的控件,增加新的功能,或者修改原有功能,來(lái)滿(mǎn)足用戶(hù)需求。
- 自定義控件(Custom Controls):就是直接從UserControl類(lèi)派生,也就是說(shuō)完全由自己來(lái)設(shè)計(jì)、實(shí)現(xiàn)一個(gè)全新的控件,這是最靈活、最強(qiáng)大的方法。
涉及知識(shí)點(diǎn)
本例中主要涉及以下知識(shí)點(diǎn):
- UserControl : 提供一個(gè)可用來(lái)創(chuàng)建其他控件的空控件。用戶(hù)創(chuàng)建一個(gè)用戶(hù)控件,會(huì)默認(rèn)繼承這個(gè)類(lèi)。
- OnPaint :控件重繪方法,是protected修飾符,本例中需要重寫(xiě)此方法。
- Graphics : 密封類(lèi),不可被繼承,用于繪制圖形(包括矩形,扇形,直線等)。
- ToolTip : 表示一個(gè)長(zhǎng)方形的小彈出窗口,該窗口在用戶(hù)將指針懸停在一個(gè)控件上時(shí)顯示有關(guān)該控件用途的簡(jiǎn)短說(shuō)明。
- 鼠標(biāo)事件函數(shù):OnMouseHover 鼠標(biāo)懸停時(shí)觸發(fā)函數(shù),OnMouseLeave鼠標(biāo)離開(kāi)時(shí)觸發(fā)函數(shù)。
- DataGridView:在可自定義的網(wǎng)格中顯示數(shù)據(jù)。
示例效果圖
本例中示例效果圖如下所示:
關(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>
/// 溫度計(jì)背景色
/// </summary>
public Color BackGroundColor
{
get { return backGroundColor; }
set { backGroundColor = value; }
}
private Font thermoFont=new Font("宋體",10,FontStyle.Regular);
/// <summary>
/// 溫度計(jì)上字體
/// </summary>
public Font ThermoFont
{
get { return thermoFont; }
set { thermoFont = value; }
}
private string thermoTitle = "溫度計(jì)";
/// <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;//兩個(gè)半圓之間的間隔
int dis2 = 2 * dis;//填充與邊框之間的距離
int startAngle1 = -180;
int startAngle2 = 0;
int sweepAngle1 = 180;
//首先畫(huà)頂端一個(gè)半圓
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);
//畫(huà)底端一個(gè)半圓
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);
//畫(huà)一個(gè)矩形
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));
//畫(huà)刻度
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));
//畫(huà)刻度數(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;//中間白色線寬度
//畫(huà)條白色細(xì)線
g.FillRectangle(Brushes.White, new Rectangle(c_x- white_width, r/2, white_width*2, height-r));
//在底部畫(huà)一個(gè)圓球
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)前溫度畫(huà)紅色線
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));//此處有一像素的誤差
//畫(huà)標(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)覆蓋進(jìn)去時(shí)
/// </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);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#文件流讀寫(xiě)和進(jìn)度回調(diào)示例詳解
欄 目:C#教程
本文標(biāo)題:C#用戶(hù)控件之溫度計(jì)設(shè)計(jì)
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5247.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)判斷當(dāng)前操作用戶(hù)管理角色的方法
- 01-10WinForm判斷關(guān)閉事件來(lái)源于用戶(hù)點(diǎn)擊右上角“關(guān)閉”按鈕的方法
- 01-10C#一個(gè)簡(jiǎn)單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10C#實(shí)現(xiàn)ComboBox控件顯示出多個(gè)數(shù)據(jù)源屬性的方法
- 01-10微信開(kāi)放平臺(tái)之網(wǎng)站授權(quán)微信登錄功能
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類(lèi)型和變量二
- 01-10C#編程自學(xué)之開(kāi)篇介紹
- 01-10C#編程自學(xué)之?dāng)?shù)據(jù)類(lèi)型和變量三
- 01-10C#編程自學(xué)之運(yùn)算符和表達(dá)式
- 01-10C#編程自學(xué)之類(lèi)和對(duì)象


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實(shí)例總結(jié)


