用於。net緊湊框架的顏色按鈕
介紹 我寫這篇文章有兩個原因。首先,雖然標準的。net框架允許你改變按鈕的顏色,但是這個特性在緊湊的框架中是缺失的。按鈕在緊湊的框架是無聊的黑色上的灰色。寫這篇文章的第二個原因是,雖然編寫標準控制元件相對簡單,但是為緊湊框架編寫控制元件就有點困難了,因為在Visual Studio中沒有用於緊湊框架控制元件的專案模板。我可以想象這兩個問題都將被微軟及時解決,但就目前而言,需要一個使用者控制元件,而且這個控制元件編寫起來並不簡單。 建立控制 已經有幾篇文章描述瞭如何為compact框架建立控制元件,所以我在這裡不再重複這些文章。在我看來,在MSDN網站上有一篇比較好的文章:為。net緊湊框架建立自定義控制元件,作者Chris Kinsman, Vergent Software, 2002年9月。這花了我一段時間來讓它工作,但結果是,因為有相當多的步驟要遵循,你必須確保你沒有錯過任何一個! 的程式碼 建立了一個空白控制元件後,我開始將其更改為按鈕。我想建立一個與標準。net CF按鈕行為相同的按鈕,但是顏色更多。我決定給按鈕4個新的顏色屬性。這些是:隱藏,複製Code
Color m_NormalBtnColour = Color.LightYellow; Color m_NormalTxtColour = Color.Blue; Color m_PushedBtnColour = Color.Blue; Color m_PushedTxtColour = Color.Yellow;
這些屬性由控制元件使用以下程式碼公開(顯示四個屬性中的一個,其他屬性相同,但有不同的名稱和描述):複製Code
#if NETCFDESIGNTIME [Category("Appearance")] [DefaultValue(3)] [Description("The normal colour of the button.")] #endif public Color NormalBtnColour { set { m_NormalBtnColour = value; Invalidate(); } get { return m_NormalBtnColour; } }
注意,我在顏色改變後使控制元件無效。這樣在設計窗體時,當顏色發生變化時,控制元件就會重新繪製。我還必須刪除兩個標準的顏色屬性:背景色和前景色。我使用以下程式碼完成了此操作(所示兩段程式碼中的一段):複製Code
#if NETCFDESIGNTIME [Browsable(false)] [EditorBrowsable(EditorBrowsableState.Never)] #endif public override Color BackColor { set {;} get { return new Color(); } }
Browsable(false)從屬性視窗中刪除該項,EditorBrowsable(Never)阻止intellisense顯示該屬性。最後,對於屬性,我添加了按鈕狀態,可以是正常狀態,也可以是按下狀態。隱藏,複製Code
public enum States { Normal, Pushed } States m_state;
這在建構函式中設定為normal,在滑鼠下推事件中設定為push,在滑鼠上推事件中設定為normal。另外,為了在設計模式下正確繪製按鈕,當按鈕被調整大小時,OnResize被覆蓋以使控制元件無效。隱藏,複製Code
protected override void OnMouseDown( System.Windows.Forms.MouseEventArgs e) { m_state = States.Pushed; // button receives input focus Focus(); base.OnMouseDown(e); Invalidate(); } protected override void OnMouseUp( System.Windows.Forms.MouseEventArgs e) { m_state = States.Normal; base.OnMouseUp(e); Invalidate(); } protected override void OnResize(EventArgs e) { base.OnResize(e); Invalidate(); }
剩下唯一要做的是OnPaint方法。如果按鈕處於正常狀態,則使用兩種正常顏色繪製按鈕;如果按鈕處於按下狀態,則使用兩種按下顏色繪製按鈕。隱藏,收縮,複製Code
protected override void OnPaint(PaintEventArgs e) { Graphics graphics = e.Graphics; Pen pen; SolidBrush brush; SolidBrush textBrush; //Work out the colours that we should be using // for the text and background if (m_state == States.Normal) { brush = new SolidBrush(m_NormalBtnColour); textBrush = new SolidBrush(m_NormalTxtColour); pen = new Pen(m_NormalTxtColour); } else { brush = new SolidBrush(m_PushedBtnColour); textBrush = new SolidBrush(m_PushedTxtColour); pen = new Pen(m_PushedTxtColour); } //Draw a rectangle and fill the inside graphics.FillRectangle(brush, 0, 0, Width, Height); graphics.DrawRectangle(pen, 0, 0, Width-1, Height-1); //Create a font based on the default font int fontHeight = 10; Font font = new Font(FontFamily.GenericSerif, fontHeight, FontStyle.Bold); //Find out the size of the text SizeF textSize = new SizeF(); textSize = e.Graphics.MeasureString(Text, font); //Work out how to position the text centrally float x=0,y=0; if (textSize.Width < Width) x = (Width - textSize.Width) /2; if (textSize.Height < Height) y = (Height - textSize.Height) /2; //Draw the text in the centre of the button using // the default font graphics.DrawString(Text, font, textBrush, x, y); }
如“構建控制元件”文章中所述,有兩種解決方案,一種用於實際控制元件,另一種用於設計器中的控制元件。我編寫了一個批處理檔案來構建這兩個解決方案並將程式集複製到正確的位置,以便Visual Studio能夠獲取它們。一旦控制元件被新增到“我的使用者控制元件”中,它們就可以被拖放到表單中,並像操縱其他控制元件一樣進行操作。 歷史 2004年1月5日-初始版本。 本文轉載於:http://www.diyabc.com/frontweb/news522.html