标签forecolor不透明? [英] Label forecolor opacity?
本文介绍了标签forecolor不透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
寻找标签forecolor opacity(不透明)...
任何关于如何存储此类控件类或句柄的链接(或代码)存在一个人?
感谢一个人...
oron: - )
解决方案
>
我做了一些实验......现在我有了一个解决方案。
也许你做了一些改动 - 但它就像你问的那样......
公开 类 customLabel
继承标签
Sub 新()
我 .SetStyle(ControlStyles.Opaque, True )
Me .SetStyle(ControlStyles.OptimizedDoubleBuffer, False )
我 .SetStyle(ControlStyles.ResizeRedraw, True )
字体= 新 System.Drawing.Font( Microsoft Sans Serif, 28 .2!)
结束 Sub
受保护的 覆盖 ReadOnly 属性 CreateParams() As System.Windows.Forms.CreateParams
获取
Dim cp 作为 CreateParams = MyBase .CreateParams
cp.ExStyle = cp.ExStyle 或& H20 '< /跨度> <跨度class =code-comment>启用WS_EX_TRANSPARENT
返回 cp
结束 获取
结束 属性
属性不透明度作为 整数
获取
返回 myOpacity
结束 获取
设置( value 作为 整数)
如果 value> = 0 和值< = 255 T.母鸡 myOpacity = value
Invalidate()
结束 设置
结束 属性
私有 myOpacity 作为 整数 = 0
受保护的 覆盖 Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
Dim myBitmap 作为 新位图( Me .Width, Me .Height)
Dim gr As Graphics = Graphics.FromImage(myBitmap)
Dim pBackColor As Color = myBackColor
如果 myBackColor = Color.Transparent 然后
如果 myParent IsNot Nothing 然后 pBackColor = myParent。 BackColor
结束 如果
Dim pe 作为 新 System.Windows.Forms.PaintEventArgs(gr,e .ClipRectangle)
MyBase .OnPaint(pe)
用于 y 作为 整数 = 0 要高度 - 1
对于 x 作为 整数 = 0 要宽度 - 1
Dim PixelColor As Color = myBitmap.GetPixel(x,y)
如果 PixelColor.A<> 0 然后
myBitmap.SetPixel(x,y,Color.FromArgb(myOpacity,PixelColor) )
Else
myBitmap.SetPixel(x,y,Color.FromArgb( 255 ,pBackColor))
结束 如果
下一步 x
下一步 y
e.Graphics.DrawImage(myBitmap, 0 , 0 )
结束 Sub
私有 Sub me_HandleCreated(发件人作为 对象,e 作为 System.Ev entArgs)句柄 我 .HandleCreated
myParent = Parent
如果 myParent IsNot Nothing 然后 myParent_Enabled = myParent.Enabled
结束 Sub
< span class =code-keyword>私有 WithEvents myParent 作为控制
覆盖 属性 BackColor 作为颜色
获取
返回 myBackColor
结束 获取
设置( ByVal value As Color)
myBackColor = value
我 .Invalidate()
结束 设置
结束 属性
私有 myBackColor As Color = Color.Blue
End 类
标签ForeColor属性的类型为Color ... Color类内置了alpha的功能频道处理......
所以不要像设置ForeColor那样:
Label1.ForeColor = Color.Red;
使用FormArgb
Label1.ForeColor = Color.FromArgb( 150 ,Color.Red);
你将拥有透明色......(根据你的需要调整alpha值)
https://msdn.microsoft.com/en-us/library/1hstcth9%28v=vs.110%29.aspx [ ^ ]
hey CP,
looking for label forecolor opacity (not transparent)...
any link (or code) on how to builed such control class or handle exist one?
thanks a head...
oron :-)
解决方案
Hi,
I made some experiments ... and now I have a solution for you.
Perhaps you do some Changes - but it works like you asked ...
Public Class customLabel Inherits Label Sub New() Me.SetStyle(ControlStyles.Opaque, True) Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False) Me.SetStyle(ControlStyles.ResizeRedraw, True) Font = New System.Drawing.Font("Microsoft Sans Serif", 28.2!) End Sub Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams Get Dim cp As CreateParams = MyBase.CreateParams cp.ExStyle = cp.ExStyle Or &H20 ' Turn on WS_EX_TRANSPARENT Return cp End Get End Property Property Opacity As Integer Get Return myOpacity End Get Set(value As Integer) If value >= 0 And value <= 255 Then myOpacity = value Invalidate() End Set End Property Private myOpacity As Integer = 0 Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs) Dim myBitmap As New Bitmap(Me.Width, Me.Height) Dim gr As Graphics = Graphics.FromImage(myBitmap) Dim pBackColor As Color = myBackColor If myBackColor = Color.Transparent Then If myParent IsNot Nothing Then pBackColor = myParent.BackColor End If Dim pe As New System.Windows.Forms.PaintEventArgs(gr, e.ClipRectangle) MyBase.OnPaint(pe) For y As Integer = 0 To Height - 1 For x As Integer = 0 To Width - 1 Dim PixelColor As Color = myBitmap.GetPixel(x, y) If PixelColor.A <> 0 Then myBitmap.SetPixel(x, y, Color.FromArgb(myOpacity, PixelColor)) Else myBitmap.SetPixel(x, y, Color.FromArgb(255, pBackColor)) End If Next x Next y e.Graphics.DrawImage(myBitmap, 0, 0) End Sub Private Sub me_HandleCreated(sender As Object, e As System.EventArgs) Handles Me.HandleCreated myParent = Parent If myParent IsNot Nothing Then myParent_Enabled = myParent.Enabled End Sub Private WithEvents myParent As Control Overrides Property BackColor As Color Get Return myBackColor End Get Set(ByVal value As Color) myBackColor = value Me.Invalidate() End Set End Property Private myBackColor As Color = Color.Blue End Class
Labels ForeColor property has the type of Color...Color class has built in capabilities of alpha channel handling...
So instead of setting ForeColor like:
Label1.ForeColor = Color.Red;
Use FormArgb
Label1.ForeColor = Color.FromArgb(150, Color.Red);
And you will have a transparent color...(adjust the alpha value to your needs)
https://msdn.microsoft.com/en-us/library/1hstcth9%28v=vs.110%29.aspx[^]
这篇关于标签forecolor不透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文