Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Data Viewer - ClassBox.vb

ClassBox.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Reflection
  2. Public Class ClassBox
  3.     Public Class ItemClickedArgs
  4.         Inherits EventArgs
  5.  
  6.         Private _Item As ItemBox
  7.  
  8.         Public Property Item() As ItemBox
  9.             Get
  10.                 Return _Item
  11.             End Get
  12.             Set(ByVal value As ItemBox)
  13.                 _Item = value
  14.             End Set
  15.         End Property
  16.  
  17.         Sub New(ByVal Item As ItemBox)
  18.             Me.Item = Item
  19.         End Sub
  20.     End Class
  21.  
  22.     Public Delegate Sub ItemClickedHandler(ByVal sender As Object, ByVal e As ItemClickedArgs)
  23.     Public Event ItemClicked As ItemClickedHandler
  24.  
  25.     Private _Color As Color = Drawing.Color.Blue
  26.     Private _Collapsed As Boolean = True
  27.  
  28.     Private TypeName As String
  29.     Private IsMouseDown As Boolean = False
  30.     Private IsResizing As Boolean = False
  31.     Private MousePos As Point
  32.     Private IdealWidth, IdealHeight As Int32
  33.  
  34.     Public Property Color() As Color
  35.         Get
  36.             Return _Color
  37.         End Get
  38.         Set(ByVal value As Color)
  39.             _Color = value
  40.         End Set
  41.     End Property
  42.  
  43.     Public Property Collapsed() As Boolean
  44.         Get
  45.             Return _Collapsed
  46.         End Get
  47.         Set(ByVal value As Boolean)
  48.             _Collapsed = value
  49.             If value Then
  50.                 Dim StrSize As SizeF = Me.CreateGraphics.MeasureString(TypeName, Me.Font, 1000)
  51.                 Me.Size = New Size(StrSize.Width + 10, 75)
  52.                 If Me.Width < 60 Then
  53.                     Me.Width = 60
  54.                 End If
  55.             End If
  56.  
  57.             For Each C As Control In Me.Controls
  58.                 C.Visible = Not value
  59.             Next
  60.         End Set
  61.     End Property
  62.  
  63.     Private Function CreateItemBox(ByVal MI As MemberInfo) As ItemBox
  64.         Dim M As New ItemBox
  65.  
  66.         'M.Location = New Point(Me.Width * 6 / 100 - 1, 35 + Y)
  67.         'M.Size = New Size(Me.Width * 88 / 100, 35)
  68.         M.Color = M.DefaultColors(MI.MemberType)
  69.         M.BackColor = Drawing.Color.Transparent
  70.         M.ForeColor = Drawing.Color.Black
  71.         M.Text = MI.Name
  72.         M.Tag = MI
  73.         M.Name = MI.Name
  74.  
  75.         Return M
  76.     End Function
  77.  
  78.     Public Sub Minimize()
  79.         Me.Collapsed = True
  80.         Me.Refresh()
  81.     End Sub
  82.  
  83.     Public Sub Miniature()
  84.  
  85.     End Sub
  86.  
  87.     Public Sub ClearAll()
  88.         Me.Controls.Clear()
  89.         Me.Minimize()
  90.     End Sub
  91.  
  92.     Public Sub ScanClass(ByVal T As Type, Optional ByVal Maximized As Boolean = False)
  93.         Dim Properties As New ArrayList
  94.         Dim Methods As New ArrayList
  95.         Dim Events As New ArrayList
  96.         Dim Fields As New ArrayList
  97.         Dim Types As New ArrayList
  98.         Dim Members As MemberInfo() = T.GetMembers
  99.         Dim M As ItemBox
  100.  
  101.         Me.Controls.Clear()
  102.  
  103.         TypeName = AssemblyScanner.GetTypeName(T)
  104.         For Each MI As MemberInfo In Members
  105.             M = CreateItemBox(MI)
  106.             If MI.MemberType <> MemberTypes.Constructor And MI.MemberType <> MemberTypes.Custom And MI.MemberType <> MemberTypes.TypeInfo Then
  107.                 If Not Me.Controls.ContainsKey(M.Name) Then
  108.                     Me.Controls.Add(M)
  109.                 Else
  110.                     With DirectCast(Me.Controls(M.Name), ItemBox)
  111.                         .ExposedMembers.Add(MI)
  112.                     End With
  113.                 End If
  114.                 AddHandler M.Click, AddressOf ItemClickHandler
  115.             End If
  116.  
  117.             Select Case MI.MemberType
  118.                 Case MemberTypes.Event
  119.                     Events.Add(MI)
  120.                 Case MemberTypes.Field
  121.                     Fields.Add(MI)
  122.                 Case MemberTypes.Method
  123.                     Methods.Add(MI)
  124.                 Case MemberTypes.NestedType
  125.                     Types.Add(MI)
  126.                 Case MemberTypes.Property
  127.                     Properties.Add(MI)
  128.             End Select
  129.         Next
  130.  
  131.         Dim Entries As New ArrayList
  132.  
  133.         For Each L As ArrayList In New ArrayList() {Events, Fields, Methods, Types, Properties}
  134.             If L.Count >= 1 Then
  135.                 Entries.Add(L)
  136.             End If
  137.         Next
  138.  
  139.         If Entries.Count = 0 Then
  140.             Me.ClearAll()
  141.             Exit Sub
  142.         End If
  143.  
  144.         If Me.Width < Entries.Count * 70 Then
  145.             Me.Width = Entries.Count * 75
  146.         End If
  147.  
  148.         Dim AvgWidth As Int32 = (Me.Width - 10 - 5 * Entries.Count) / Entries.Count
  149.         Dim X, Y, YMax As Int32
  150.  
  151.         If True Then
  152.             AvgWidth = 100
  153.             Me.Width = Entries.Count * 105 + 10
  154.         End If
  155.  
  156.         X = 5
  157.         Y = 35
  158.  
  159.         For Each Entry As ArrayList In Entries
  160.             Y = 35
  161.             For Each MI As MemberInfo In Entry
  162.                 With DirectCast(Me.Controls(MI.Name), ItemBox)
  163.                     If Y > Me.Height - 50 Then .Visible = False
  164.                     If .Overloaded Then Continue For
  165.                     .Location = New Point(X, Y)
  166.                     .Size = New Size(AvgWidth, 35)
  167.                     .Overloaded = True
  168.                 End With
  169.                 Y += 36
  170.                 If Y > YMax Then YMax = Y
  171.             Next
  172.             X += AvgWidth + 5
  173.         Next
  174.  
  175.         IdealHeight = YMax + 40
  176.         IdealWidth = Entries.Count * 105 + 10
  177.         Me.Height = IdealHeight
  178.         Me.Width = IdealWidth
  179.         Me.Collapsed = False
  180.         Me.Refresh()
  181.     End Sub
  182.  
  183.     Public Sub SetTypeName(ByVal T As Type)
  184.         Dim Name As String = AssemblyScanner.GetTypeName(T)
  185.         Dim MinWidth As Int32 = Me.CreateGraphics.MeasureString(Name, Me.Font, 1000).Width
  186.  
  187.         TypeName = Name
  188.  
  189.         If T.IsInterface Then
  190.             Me.Color = Drawing.Color.Fuchsia
  191.         ElseIf T.IsEnum Then
  192.             Me.Color = Drawing.Color.DarkOrange
  193.         ElseIf T.IsValueType Then
  194.             Me.Color = Drawing.Color.DarkGreen
  195.         End If
  196.  
  197.         Me.Collapsed = True
  198.     End Sub
  199.  
  200.     Private Sub ClassBox_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  201.         Dim H As New Header
  202.         Dim F As New Footer
  203.         Dim B As New Block
  204.  
  205.         'Header, 20%
  206.         H.Location = New Point(0, 0)
  207.         H.Size = New Size(Me.Width - 1, Me.Height / 5)
  208.         H.Color = Me.Color
  209.  
  210.         'Footer, 20%
  211.         F.Location = New Point(0, Me.Height * 4 / 5 - 1)
  212.         F.Size = New Size(Me.Width - 1, Me.Height / 5)
  213.         F.Color = Me.Color
  214.  
  215.         'Block, 60%
  216.         B.Location = New Point(0, Me.Height / 5)
  217.         B.Size = New Size(Me.Width - 1, Me.Height * 3 / 5 + 1)
  218.         B.Color = Me.Color
  219.  
  220.         e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
  221.         H.Draw(e.Graphics)
  222.         B.Draw(e.Graphics)
  223.         F.Draw(e.Graphics)
  224.  
  225.         Dim TextSize As SizeF = e.Graphics.MeasureString(TypeName, Me.Font, 1000)
  226.         If Not Collapsed Then
  227.             Dim C As Color = Me.ForeColor
  228.             If Me.Height > 300 Then C = Drawing.Color.Black
  229.             e.Graphics.DrawString(TypeName, Me.Font, New SolidBrush(C), Me.Width / 2 - TextSize.Width / 2, 5)
  230.         Else
  231.             e.Graphics.DrawString(TypeName, Me.Font, New SolidBrush(Me.ForeColor), Me.Width / 2 - TextSize.Width / 2, Me.Height / 2 - TextSize.Height / 2)
  232.         End If
  233.     End Sub
  234.  
  235.     Private Sub ClassBox_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
  236.         MousePos = New Point(MousePosition.X - Me.Location.X, MousePosition.Y - Me.Location.Y)
  237.         IsMouseDown = True
  238.     End Sub
  239.  
  240.     Private Sub ClassBox_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
  241.         IsMouseDown = False
  242.     End Sub
  243.  
  244.     Private Sub ClassBox_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
  245.         If IsMouseDown Then
  246.             Me.Location = New Point(MousePosition.X - MousePos.X, MousePosition.Y - MousePos.Y)
  247.         End If
  248.     End Sub
  249.  
  250.     Private Sub ItemClickHandler(ByVal sender As Object, ByVal e As EventArgs)
  251.         RaiseEvent ItemClicked(Me, New ItemClickedArgs(DirectCast(sender, ItemBox)))
  252.     End Sub
  253. End Class