[C#]WinForm Label 透明底色
C#的Winform不支援底色透明的Label(Button也一樣)
雖然在物件屬性上可以選擇底色為透明,但其透明仍以Winform底色為主(Label透明=Winform的顏色)
所以參考了人家的做法,在此做了紀錄,並加上詳細的使用方式
1.在專案上新增類別(類別名稱:TransparentLabel ),將下列程式碼貼上
2.在Form設計模式底下點選工具箱,即會出現該類別所產生的Label,將其拖拉到Form上
3.修改該物件的屬性(跟一般使用Label一樣)
4.注意一點,只要Label的背景有變,就要執行Refesh()
有空再來做範例
==========在專案上新增類別(類別名稱:TransparentLabel ),將下列程式碼貼上=======
雖然在物件屬性上可以選擇底色為透明,但其透明仍以Winform底色為主(Label透明=Winform的顏色)
所以參考了人家的做法,在此做了紀錄,並加上詳細的使用方式
1.在專案上新增類別(類別名稱:TransparentLabel ),將下列程式碼貼上
2.在Form設計模式底下點選工具箱,即會出現該類別所產生的Label,將其拖拉到Form上
3.修改該物件的屬性(跟一般使用Label一樣)
4.注意一點,只要Label的背景有變,就要執行Refesh()
有空再來做範例
==========在專案上新增類別(類別名稱:TransparentLabel ),將下列程式碼貼上=======
using System; using System.Drawing; using System.Windows.Forms; namespace WinFormsControls { ////// A label that can be transparent. /// public class TransparentLabel : Control { ////// Creates a new public TransparentLabel() { TabStop = false; } ///instance. /// /// Gets the creation parameters. /// protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x20; return cp; } } ////// Paints the background. /// /// E. protected override void OnPaintBackground(PaintEventArgs e) { // do nothing } ////// Paints the control. /// /// E. protected override void OnPaint(PaintEventArgs e) { DrawText(); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == 0x000F) { DrawText(); } } private void DrawText() { using (Graphics graphics = CreateGraphics()) using (SolidBrush brush = new SolidBrush(ForeColor)) { SizeF size = graphics.MeasureString(Text, Font); // first figure out the top float top = 0; switch (textAlign) { case ContentAlignment.MiddleLeft: case ContentAlignment.MiddleCenter: case ContentAlignment.MiddleRight: top = (Height - size.Height) / 2; break; case ContentAlignment.BottomLeft: case ContentAlignment.BottomCenter: case ContentAlignment.BottomRight: top = Height - size.Height; break; } float left = -1; switch (textAlign) { case ContentAlignment.TopLeft: case ContentAlignment.MiddleLeft: case ContentAlignment.BottomLeft: if (RightToLeft == RightToLeft.Yes) left = Width - size.Width; else left = -1; break; case ContentAlignment.TopCenter: case ContentAlignment.MiddleCenter: case ContentAlignment.BottomCenter: left = (Width - size.Width) / 2; break; case ContentAlignment.TopRight: case ContentAlignment.MiddleRight: case ContentAlignment.BottomRight: if (RightToLeft == RightToLeft.Yes) left = -1; else left = Width - size.Width; break; } graphics.DrawString(Text, Font, brush, left, top); } } ////// Gets or sets the text associated with this control. /// ////// The text associated with this control. /// public override string Text { get { return base.Text; } set { base.Text = value; RecreateHandle(); } } ////// Gets or sets a value indicating whether control's elements are aligned to support locales using right-to-left fonts. /// ////// /// One of the ///values. The default is . /// /// The assigned value is not one of the public override RightToLeft RightToLeft { get { return base.RightToLeft; } set { base.RightToLeft = value; RecreateHandle(); } } ///values. /// /// Gets or sets the font of the text displayed by the control. /// ////// /// The public override Font Font { get { return base.Font; } set { base.Font = value; RecreateHandle(); } } private ContentAlignment textAlign = ContentAlignment.TopLeft; ///to apply to the text displayed by the control. The default is the value of the property. /// /// Gets or sets the text alignment. /// public ContentAlignment TextAlign { get { return textAlign; } set { textAlign = value; RecreateHandle(); } } } }
留言
張貼留言