Chinaunix首页 | 论坛 | 博客
  • 博客访问: 523612
  • 博文数量: 576
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5020
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:47
文章分类

全部博文(576)

文章存档

2011年(1)

2008年(575)

我的朋友

分类:

2008-10-14 14:50:39

GraphicMenu类
Public Class GraphicMenu : Inherits Component

  ' Appearance Properties
  ' TODO:: List properties

  ' Behavior Properties
  ' TODO:: List properties

  Public Sub Init(ByVal menu As Menu)

     ' Return if the menu is null
     If menu Is Nothing Then
          Return
     End If

     ' Initialize the font object used to render the menu items
     ItemFont = New Font(FontName, FontSize)

     ' Initialize the hashtable used to hold bitmap/item bindings
     If menuItemIconCollection Is Nothing Then
         menuItemIconCollection = New Hashtable
     End If

     ' Context menu requires a different treatment
     If TypeOf menu Is ContextMenu Then
         HandleChildMenuItems(menu)
         Return
     End If

     ' Iterate on all top-level menus and handle their items
     For Each popup As MenuItem In menu.MenuItems
         HandleChildMenuItems(popup)
     Next

  End Sub

End Class

MeasureItemEventArgs

Property Description
Graphics 获得用以计算尺寸所需的Graphics对象
Index 读取或设置需要改变的高宽度的项索引
ItemHeight 读取或设置项的高度值
ItemWidth 读取或设置项的宽度值

定义对象们的大小
' ****************************************************************
' HELPER: StdMeasureItem
' INPUT : menu item, data used for measurement
' NOTES : Event handler for the MeasureItem event typical of 
' owner-drawn objects
       Private Sub StdMeasureItem(ByVal sender As Object, _
           ByVal e As MeasureItemEventArgs)

    ' Grab a reference to the menu item being measured
    Dim item As MenuItem = CType(sender, MenuItem)

    ' If it is a separator, handle differently
    If (item.Text = "-") Then
        e.ItemHeight = SeparatorHeight
        Return
    End If

    ' Measure the item text with the current font. The text to 
    ' measure includes keyboard shortcuts
    Dim stringSize As SizeF
    stringSize = e.Graphics.MeasureString(GetEffectiveText(item), ItemFont)

    ' Set the height and width of the item
    e.ItemHeight = MenuItemHeight
    e.ItemWidth = BitmapWidth + HorizontalTextOffset + _ 
        CInt(stringSize.Width) + RightOffset
End Sub

' ****************************************************************
' HELPER: StdDrawItem
' INPUT : menu item, ad hoc structure for custom drawing
' NOTES : Event handler for the DrawItem event typical of 
' owner-drawn objects
Private Sub StdDrawItem(ByVal sender As Object, ByVal e As _
    DrawItemEventArgs)

    ' Grab a reference to the item being drawn
    Dim item As MenuItem = CType(sender, MenuItem)

    ' Saves helper objects for easier reference
    Dim g As Graphics = e.Graphics
    Dim bounds As RectangleF = MakeRectangleF(e.Bounds)
    Dim itemText As String = item.Text
    Dim itemState As DrawItemState = e.State

    ' Define bounding rectangles to use later
    CreateLayout(bounds)

    ' Draw the menu item background and text
    DrawBackground(g, itemState)

    ' Draw the bitmap leftmost area
    DrawBitmap(g, item, itemState)

    ' Draw the text
    DrawText(g, item)
End Sub

DrawItemEventArgs

Property Description
BackColor 获取项的背景
Bounds 获取项将要占据的矩形
Font 获取项所使用的字体
ForeColor 获取项的前景色
Graphics 获取用以绘制该项的Graphics对象
Index 获取将要绘制的项的索引
State 获取该项的状态

灰化图像
If (disabled) Then
   Dim imageAttr As New ImageAttributes
   imageAttr.SetGamma(0.2F)
   Dim tmpRect = New Rectangle( BitmapBounds.X + 2, _
        BitmapBounds.Y + 2, BitmapBounds.Width - 2, _
        BitmapBounds.Right - 2)
   g.DrawImage(bmp, tmpRect, 0, 0, _
        bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr)
   ImageAttr.ClearGamma()
Else
   g.DrawImage(bmp, BitmapBounds.X + 2, BitmapBounds.Y + 2)
End If


--------------------next---------------------

阅读(189) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~