本文介紹下電腦設備關機的情況下如何通過網路喚醒設備,之前電源S狀態 電腦Power電源狀態- 唐宋元明清2188 - 博客園 (cnblogs.com) 有介紹過遠程喚醒設備,後面這倆天瞭解多了點所以單獨加個隨筆 設備關機的情況下,使用網路喚醒的前提條件: 1. 被喚醒設備需要支持這WakeOnL ...
C#用戶控制項之轉換開關
如何自定義一個轉換鍵(Toggle)?
三步繪製一個精美控制項:
- 定義屬性;
- 畫布重繪;
- 添加事件;
主要技能:
- 如何自定義屬性;
- 畫布重繪的一般格式;
- 控制項的事件觸發過程;
- 技能擴展
-
轉換按鈕使能時添加二次確認彈框?
-
在From窗體中應用控制項時,點擊事件沒有觸發?
-
屬性名稱在控制項屬性樹中的排列如何定義?
-
添加一個字體更改屬性?
-
1.定義屬性
- 字體(Font)
- 顏色(Color)
- 字元串(String)
- 枚舉(Enum)
- 屬性說明 [Browsable(true)]
#region 屬性
private Font displayFont = new Font("Segoe UI", 12);
[Browsable(true)]
[Category("佈局_G")]
[Description("字體格式")]
public Font DisplayFont
{
get { return displayFont; }
set
{
if (value != null)
{
displayFont = value;
this.Invalidate(); // Trigger redraw
}
}
}
private bool _checked = false;
[Browsable(true)] //說明(需放在屬性前邊):是否選中
[Category("佈局_G")]
[Description("是否選中")]
public bool Checked
{
get { return _checked; }
set
{
_checked = value;
this.Invalidate();
//激活觸發事件
this.MouseDown_G?.Invoke(this, null);
}
}
private string falseText = "關閉";
[Browsable(true)] //說明:文本關閉
[Category("佈局_G")]
[Description("文本關閉")]
public string FalseText
{
get { return falseText; }
set { falseText = value; this.Invalidate(); }
}
//樣式切換
public enum SwType
{
Ellipse, //橢圓
Rectangle, //矩形
}
private SwType switchType = SwType.Ellipse;
[Browsable(true)] //說明:切換樣式
[Category("佈局_G")]
[Description("切換樣式")]
public SwType SwitchType
{
get { return switchType; }
set { switchType = value; this.Invalidate(); }
}
private Color sliderColor = Color.White; //Color.White
[Browsable(true)] //說明:滑塊顏色
[Category("佈局_G")]
[Description("滑塊顏色")]
public Color SliderColor
{
get { return sliderColor; }
set { sliderColor = value; this.Invalidate(); }
}
#endregion
屬性名稱在控制項屬性樹中的排列如何定義?
答:根據屬性說明安裝A~Z的字母順序排列
2.畫布重繪
- 畫帶四角圓弧的矩形、滑塊、文本;
- 畫橢圓、滑塊、文本
#region 畫布
private Graphics graphics;
private int width;
private int height;
//矩形繪製
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
graphics = e.Graphics;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
this.width = this.Width;
this.height = this.Height;
if (this.switchType == SwType.Rectangle) //欄位選擇為矩形時
{
//填充色
Color fillColor = this._checked ? trueColor : falseColor;
//帶四角圓弧的矩形
GraphicsPath path = new GraphicsPath();
int diameter = 10; //預設圓弧直徑
//左上角圓弧:起始坐標,寬,高,開始角度,掃描角度
path.AddArc(0, 0, diameter, diameter, 180f, 90f);
path.AddArc(this.width - diameter, 0, diameter, diameter, 270f, 90f); //右上角
path.AddArc(this.width - diameter, this.height - diameter, diameter, diameter, 0f, 90f); //右下角
path.AddArc(0, this.height - diameter, diameter, diameter, 90f, 90f); //左下角
graphics.FillPath(new SolidBrush(fillColor), path); //填充色
//文本
string strText = this._checked ? trueText : falseText;
//滑塊(true\false 兩種形態)
if (_checked)
{
//繪製滑塊
path = new GraphicsPath();
int sliderwidth = this.height - 4;
path.AddArc(this.width - sliderwidth - 2, 2, diameter, diameter, 180f, 90f);
path.AddArc(this.width - diameter - 2, 2, diameter, diameter, 270f, 90f);
path.AddArc(this.width - diameter - 2, this.height - diameter - 2, diameter, diameter, 0f, 90f);
path.AddArc(this.width - sliderwidth - 2, this.height - diameter - 2, diameter, diameter, 90f, 90f);
graphics.FillPath(new SolidBrush(sliderColor), path);
//繪製文本
Rectangle rec = new Rectangle(0, 0, this.width - sliderwidth - 2, this.height);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
graphics.DrawString(strText, DisplayFont, new SolidBrush(sliderColor), rec, sf);
}
else
{
//繪製滑塊
path = new GraphicsPath();
int sliderwidth = this.height - 4;
path.AddArc(2, 2, diameter, diameter, 180f, 90f);
path.AddArc(sliderwidth - diameter + 2, 2, diameter, diameter, 270f, 90f);
path.AddArc(sliderwidth - diameter + 2, sliderwidth - diameter + 2, diameter, diameter, 0f, 90f);
path.AddArc(2, sliderwidth - diameter + 2, diameter, diameter, 90f, 90f);
graphics.FillPath(new SolidBrush(sliderColor), path);
//繪製文本
Rectangle rec = new Rectangle(sliderwidth + 2, 0, this.width - sliderwidth - 2, this.height);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
graphics.DrawString(strText, DisplayFont, new SolidBrush(sliderColor), rec, sf);
}
}
else if (this.switchType == SwType.Ellipse) //欄位選擇為橢圓時
{
//填充色
Color fillColor = this._checked ? trueColor : falseColor;
//橢圓形
GraphicsPath path = new GraphicsPath();
path.AddArc(1, 1, this.height - 2, this.height - 2, 90f, 180f);
path.AddArc(this.width - (this.height - 2) - 1, 1, this.height - 2, this.height - 2, 270f, 180f);
graphics.FillPath(new SolidBrush(fillColor), path); //填充色
//文本
string strText = this._checked ? TrueText : falseText;
//滑塊(true\false 兩種形態)
if (_checked)
{
//繪製滑塊
int ciclewidth = this.height - 6;
graphics.FillEllipse(new SolidBrush(sliderColor), new Rectangle(this.width - ciclewidth - 3, 3, ciclewidth, ciclewidth));
//繪製文本
Rectangle rec = new Rectangle(0, 0, this.width - ciclewidth - 3, this.height);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
graphics.DrawString(strText, DisplayFont, new SolidBrush(sliderColor), rec, sf);
}
else
{
//繪製滑塊
int ciclewidth = this.height - 6;
graphics.FillEllipse(new SolidBrush(sliderColor), new Rectangle(3, 3, ciclewidth, ciclewidth));
//繪製文本
Rectangle rec = new Rectangle(ciclewidth + 3, 0, this.width - ciclewidth - 3, this.height);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
graphics.DrawString(strText, DisplayFont, new SolidBrush(sliderColor), rec, sf);
}
}
}
#endregion
3.添加事件
理解 :在From中控制項的滑鼠點擊事件的執行過程
先執行控制項內部的MouseDown方法——其方法內部的屬性——其屬性中的觸發事件——最後執行From後臺的點擊生成的自定義方法(可自定義)
其他技巧: 應用控制項時拉出來雙擊即可生成自定義方法
<From中雙擊控制項預設是Load方法,想讓他自動生成自己設置的事件需要在控制項代碼中添加:[DefaultEvent("MouseDown_G")]>。
//指定預設事件(雙擊控制項進入)
[DefaultEvent("MouseDown_G")]
關鍵代碼:點擊控制項彈出框確認後再執行From的事件中的代碼
//事件聲明 public event EventHandler MouseDown_G;
//激活事件 this.MouseDown_G?.Invoke(this, null); ——執行完成跳轉到From中的雙擊事件方法中
//構造函數添加滑鼠點擊事件——點擊控制項事件處理
this.MouseDown += Toggle_MouseDown;
//構造函數添加事件處理後自動生成此方法(無需在控制項的屬性中雙擊)
private void Toggle_MouseDown(object sender, MouseEventArgs e)
{
DialogResult dr = MessageBox.Show("二次確認操作?", "提示您", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
Checked = !Checked;
}
else return;
}
發現一個錯誤,當應用該控制項時,在From中點擊無效;經過兩天的查詢(斷點查詢)才發現問題所在,在Form的設計器中的From屬性的Enable為False,改為true才可使能
原因:控制項更新後沒有及時生成導致From崩盤後(空視窗後沒在意),導致Designer的預設代碼更改。
End
討論交流請留言