elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.


Tema destacado:


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Librería de Snippets para VB.NET !! (Compartan aquí sus snippets)
0 Usuarios y 7 Visitantes están viendo este tema.
Páginas: 1 ... 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 [62] Ir Abajo Respuesta Imprimir
Autor Tema: Librería de Snippets para VB.NET !! (Compartan aquí sus snippets)  (Leído 618,792 veces)
Eleкtro
Ex-Staff
*
Conectado Conectado

Mensajes: 9.974



Ver Perfil
Re: Librería de Snippets para VB.NET !! (Compartan aquí sus snippets)
« Respuesta #610 en: Hoy a las 09:16 »

Un simple TypeConverter junto a un UITypeEditor (opcional) para representar un color en formato web.



Nota: los colores no se ven como deberían por la compresión de imagen del software que he usado para capturar el GIF animado xD

WebColorConverter.vb
Código
  1. #Region " Option Statements "
  2.  
  3. Option Strict On
  4. Option Explicit On
  5. Option Infer Off
  6.  
  7. #End Region
  8.  
  9. #Region " Imports "
  10.  
  11. Imports System.ComponentModel
  12. Imports System.Globalization
  13.  
  14. #End Region
  15.  
  16. Public Class WebColorConverter : Inherits ColorConverter
  17.  
  18.    <DebuggerStepThrough>
  19.    Public Overrides Function GetStandardValuesSupported(context As ITypeDescriptorContext) As Boolean
  20.  
  21.        Return True
  22.    End Function
  23.  
  24.    <DebuggerStepThrough>
  25.    Public Overrides Function GetStandardValues(context As ITypeDescriptorContext) As StandardValuesCollection
  26.  
  27.        Dim collection As New Collection()
  28.  
  29.        For Each kc As KnownColor In [Enum].GetValues(GetType(KnownColor))
  30.            Dim color As Color = Color.FromKnownColor(kc)
  31.  
  32.            If Not color.IsSystemColor AndAlso color.IsNamedColor Then
  33.                collection.Add(color)
  34.            End If
  35.        Next
  36.  
  37.        Return New StandardValuesCollection(collection)
  38.    End Function
  39.  
  40.    <DebuggerStepThrough>
  41.    Public Overrides Function ConvertTo(context As ITypeDescriptorContext,
  42.                                    culture As CultureInfo,
  43.                                    value As Object,
  44.                                    destinationType As Type) As Object
  45.  
  46.        If destinationType Is GetType(String) AndAlso TypeOf value Is Color Then
  47.            Dim color As Color = DirectCast(value, Color)
  48.            Dim html As String = If(color.A <> 255, $"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}",
  49.                                    ColorTranslator.ToHtml(Color.FromArgb(color.R, color.G, color.B)))
  50.  
  51.            Dim name As String = Nothing
  52.            For Each knownColor As KnownColor In [Enum].GetValues(GetType(KnownColor))
  53.                Dim k As Color = Color.FromKnownColor(knownColor)
  54.                If k.A = color.A AndAlso
  55.                   k.R = color.R AndAlso
  56.                   k.G = color.G AndAlso
  57.                   k.B = color.B AndAlso Not k.IsSystemColor Then
  58.  
  59.                    name = k.Name
  60.                    Exit For
  61.                End If
  62.            Next
  63.  
  64.            Return If(name IsNot Nothing, $"{html} ({name})", html)
  65.        End If
  66.  
  67.        Return MyBase.ConvertTo(context, culture, value, destinationType)
  68.    End Function
  69.  
  70.    <DebuggerStepThrough>
  71.    Public Overrides Function ConvertFrom(context As ITypeDescriptorContext,
  72.                                          culture As CultureInfo,
  73.                                          value As Object) As Object
  74.  
  75.        Dim s As String = TryCast(value, String)
  76.        If s IsNot Nothing Then
  77.            s = s.Trim()
  78.  
  79.            ' If input is like "xxx (Name)" remove the part in parentheses
  80.            Dim idx As Integer = s.IndexOf("("c)
  81.            If idx >= 0 Then
  82.                s = s.Substring(0, idx).Trim()
  83.            End If
  84.  
  85.            If s.StartsWith("#") Then
  86.                Dim hx As String = s.Substring(1).Trim()
  87.  
  88.                ' Support #RRGGBB and #AARRGGBB
  89.                If hx.Length = 6 Then
  90.                    Dim r As Integer = Convert.ToInt32(hx.Substring(0, 2), 16)
  91.                    Dim g As Integer = Convert.ToInt32(hx.Substring(2, 2), 16)
  92.                    Dim b As Integer = Convert.ToInt32(hx.Substring(4, 2), 16)
  93.                    Return Color.FromArgb(255, r, g, b)
  94.  
  95.                ElseIf hx.Length = 8 Then
  96.                    Dim a As Integer = Convert.ToInt32(hx.Substring(0, 2), 16)
  97.                    Dim r As Integer = Convert.ToInt32(hx.Substring(2, 2), 16)
  98.                    Dim g As Integer = Convert.ToInt32(hx.Substring(4, 2), 16)
  99.                    Dim b As Integer = Convert.ToInt32(hx.Substring(6, 2), 16)
  100.                    Return Color.FromArgb(a, r, g, b)
  101.  
  102.                Else
  103.                    ' Support short hex format (#RGB) by expanding to 6 digits
  104.                    If hx.Length = 3 Then
  105.                        Dim r As Integer = Convert.ToInt32(String.Concat(hx(0), hx(0)), 16)
  106.                        Dim g As Integer = Convert.ToInt32(String.Concat(hx(1), hx(1)), 16)
  107.                        Dim b As Integer = Convert.ToInt32(String.Concat(hx(2), hx(2)), 16)
  108.                        Return Color.FromArgb(255, r, g, b)
  109.                    End If
  110.                End If
  111.  
  112.            Else
  113.                ' Try ColorTranslator for standard names and "Transparent"
  114.                Try
  115.                    Dim c As Color = ColorTranslator.FromHtml(s)
  116.                    ' ColorTranslator.FromHtml returns A=255 for known names; A=0 for "Transparent"
  117.                    Return c
  118.  
  119.                Catch ex As Exception
  120.                    ' Try KnownColor enum (e.g. user types "White" or "WhiteSmoke")
  121.                    Try
  122.                        Dim kc As KnownColor
  123.                        If [Enum].TryParse(s, True, kc) Then
  124.                            Return Color.FromKnownColor(kc)
  125.                        End If
  126.                    Catch
  127.                        ' Continue to fallback
  128.                    End Try
  129.                End Try
  130.            End If
  131.  
  132.            ' If everything fails, fallback to the base converter (throws or defaults)
  133.        End If
  134.  
  135.        Return MyBase.ConvertFrom(context, culture, value)
  136.    End Function
  137.  
  138. End Class
  139.  

WebColorEditor.vb
Código
  1. #Region " Option Statements "
  2.  
  3. Option Strict On
  4. Option Explicit On
  5. Option Infer Off
  6.  
  7. #End Region
  8.  
  9. #Region " Imports "
  10.  
  11. Imports System.ComponentModel
  12. Imports System.Drawing.Design
  13.  
  14. #End Region
  15.  
  16. Public Class WebColorEditor : Inherits UITypeEditor
  17.  
  18.    <DebuggerStepThrough>
  19.    Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle
  20.  
  21.        Return UITypeEditorEditStyle.None
  22.    End Function
  23.  
  24.    <DebuggerStepThrough>
  25.    Public Overrides Function GetPaintValueSupported(context As ITypeDescriptorContext) As Boolean
  26.  
  27.        Return True
  28.    End Function
  29.  
  30.    <DebuggerStepThrough>
  31.    Public Overrides Sub PaintValue(e As PaintValueEventArgs)
  32.  
  33.        Dim color As Color
  34.  
  35.        If TypeOf e.Value Is Color Then
  36.            color = DirectCast(e.Value, Color)
  37.        Else
  38.            Exit Sub
  39.        End If
  40.  
  41.        Using brush As New SolidBrush(color)
  42.            e.Graphics.FillRectangle(brush, e.Bounds)
  43.        End Using
  44.    End Sub
  45.  
  46. End Class

Modo de empleo:
Código
  1. Imports System.ComponentModel
  2. Imports System.Drawing.Design
  3.  
  4. Public Class Form1
  5.  
  6.    Private ReadOnly ClassTest As New MyClassTest
  7.  
  8.    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  9.  
  10.        Me.PropertyGrid1.SelectedObject = Me.ClassTest
  11.    End Sub
  12.  
  13.    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  14.  
  15.        Me.ClassTest.MyColor = Color.FromArgb(255, 116, 222, 4)
  16.        Me.PropertyGrid1.Refresh()
  17.    End Sub
  18. End Class
  19.  
  20. Friend NotInheritable Class MyClassTest
  21.  
  22.    <TypeConverter(GetType(WebColorConverter))>
  23.    <Editor(GetType(WebColorEditor), GetType(UITypeEditor))> ' El editor es opcional.
  24.    Public Property MyColor As Color = Color.Transparent
  25.  
  26. End Class

Ejemplo de utilidad en la vida real: un programa open-source para mostrar en tiempo real el color del pixel actual en la pantalla (entre otras cosas):



« Última modificación: Hoy a las 09:30 por Eleкtro » En línea



Páginas: 1 ... 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 [62] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines