AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VB编程

Visual Basic创建“五星”级控件

51自学网 http://www.wanshiok.com

   该解决方案并不会真正地解决问题,除非您有办法通过开发人员使用该控件了解该属性是否会设为默认值或设置为特定的颜色。跟踪该信息的系统开销可能不值得花费这么大的精力。作为替代,我决定使用默认的空值,并在属性例程自身中返回适当的默认值,如下 所示。 这样,就解决了到目前为止我所提出的问题,包括系统颜色更改的处理;了解它何时会返回默认值与用户何时对其进行设置;以及允许用户重新将该值设置为默认值(在 myControl.HoverControl = Color.Empty 时)。

Public Property HoverColor() As Color
  Get
   If m_HoverColor.Equals(Color.Empty) Then
    Return Color.FromKnownColor(KnownColor.Highlight)
   Else
    Return m_HoverColor
   End If
  End Get
  Set(ByVal Value As Color)
   If Not Value.Equals(m_HoverColor) Then
    m_HoverColor = Value
    Me.Invalidate()
   End If
  End Set
End Property

拟定自定义和标准图像

   在我正在构建的控件中,我决定允许两种主要的图像类别:标准图像和用户提供的图像。虽然最初的控件只支持两种标准图形(圆形和正方形),但是稍后我将讨论一种将自定义图形添加到该列表中的方法。 该控件的所有绘图在 OnPaint 例程中进行处理,我重写了该例程以提供我自己的渲染代码(参见下面的示例代码)。

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
e.Graphics.Clear(Me.BackColor)

  Dim imageWidth, imageHeight As Integer
  imageWidth = (Me.Width-(LeftMargin + RightMargin + ( _
    Me.m_ImageSpacing * (Me.m_ImageCount-1)))) / Me.m_ImageCount
    imageHeight = (Me.Height-(TopMargin + BottomMargin))

 Dim start As New Point(Me.LeftMargin, Me.TopMargin)

 For i As Integer = 0 To Me.ImageCount-1
   Me.ItemAreas(i).X = start.X-Me.ImageSpacing / 2
   Me.ItemAreas(i).Y = start.Y
   Me.ItemAreas(i).Width = imageWidth + Me.ImageSpacing / 2
   Me.ItemAreas(i).Height = imageHeight

  If Me.ImageToDraw = UserSuppliedImage Then
    DrawUserSuppliedImage(e.Graphics, _
    start.X, start.Y, imageWidth, imageHeight, i)
   Else
    DrawStandardImage(e.Graphics, Me.ImageToDraw, _
    start.X, start.Y, imageWidth, imageHeight, i)
   End If
    start.X += imageWidth + Me.ImageSpacing
  Next
  MyBase.OnPaint(e)
End Sub


Protected Overridable Sub DrawUserSuppliedImage( _
  ByVal g As Graphics, _
  ByVal x As Integer, ByVal y As Integer, _
  ByVal w As Integer, ByVal h As Integer, _
  ByVal currentPos As Integer)

 Dim img As Image
  If m_hovering And m_hoverItem > currentPos Then
   img = Me.HoverImage
  ElseIf Not m_hovering And m_selectedItem > currentPos Then
   img = Me.FilledImage
  Else
   img = Me.EmptyImage
  End If

 If Not img Is Nothing Then
   g.DrawImage(img, x, y, w, h)
  Else
   Me.DrawStandardImage(g, Me.Circle, x, y, w, h, currentPos)
  End If
End Sub

Protected Overridable Sub DrawStandardImage( _
  ByVal g As Graphics, ByVal ImageType As Integer, _
  ByVal x As Integer, ByVal y As Integer, _
  ByVal w As Integer, ByVal h As Integer, _
  ByVal currentPos As Integer)

 Dim fillBrush As Brush
  Dim outlinePen As Pen = New Pen(Me.OutlineColor, 1)

 If m_hovering And m_hoverItem > currentPos Then
   fillBrush = New SolidBrush(Me.HoverColor)
  ElseIf Not m_hovering And m_selectedItem > currentPos Then
   fillBrush = New SolidBrush(Me.SelectedColor)
  Else
   fillBrush = New SolidBrush(Me.EmptyColor)
  End If

 Select Case ImageType
   Case Me.Square
    g.FillRectangle(fillBrush, x, y, w, h)
    g.DrawRectangle(outlinePen, x, y, w, h)
   Case Me.Circle
    g.FillEllipse(fillBrush, x, y, w, h)
    g.DrawEllipse(outlinePen, x, y, w, h)
  End Select
End Sub


   在该例程中,计算每个图形的位置(使用 ImageCount 属性来确定应绘制的图形数量),然后调用 DrawStandardImage(绘制圆形或正方形)或 DrawUserSuppliedImage(绘制用户提供的图形)。 这些例程并不是最有效的(例如,我始终重新绘制完整的控件,而不是只将那些受特定更新影响的区域设置为无效),但是在必要时它们会考虑绘制适当的图形(或在标准选项情况下绘制适当的彩色图形)。在控件的其余代码中,每当属性或声明发生更改引起控件外观发生更改时,都会通过调用 Me.Invalidate 触发完整的重新绘图。OnMouseMove 的重写例程是这种类型代码的一个示例:

Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
  For i As Integer = 0 To Me.ImageCount-1
   If Me.ItemAreas(i).Contains(e.X, e.Y) Then
    Me.m_hoverItem = i + 1
    Me.Invalidate()
    Exit For
   End If
  Next
  MyBase.OnMouseMove(e)
End Sub


 

 
 

上一篇:VB中利用第三方控件实现QQ垂直菜单  下一篇:VB报表设计中使用天宇报表控件