vb.net生成Wince下的图片文字混编工具栏

来源:互联网 发布:c语言如何求最小公倍数 编辑:程序博客网 时间:2024/04/29 06:35
Imports PictureButtonToolbar.PictureButtonToolbar
Public Class UserControl1
    Inherits System.Windows.Forms.UserControl
    ''' <summary>
    ''' 按钮的单击事件(供平台监听)
    ''' </summary>
    Public Event btnclick(ByVal id As Integer, ByVal AppName As String)
    '工具栏的横竖标志(false为横;true为竖)
    Public HorVerFlag As Boolean = True
    Public Property SetHorVerFlag() As Boolean
        Get
            Return HorVerFlag
        End Get
        Set(ByVal Value As Boolean)
            HorVerFlag = Value
        End Set
    End Property
    '工具栏中按钮的大小
    Public MaxBtnWidth As Integer = 0
    Public MaxBtnHeight As Integer = 0
    '整个控件的大小
    Public CWidth As Integer = 0
    Public CHeight As Integer = 0
    '当前控件中各个子控件的图像和文件的排列格式
    Public CurBtnLayout As Integer = 2
    ''' <summary>
    ''' 根据传入的用户菜单的参数动态创建工具栏按钮
    ''' </summary>
    ''' 生成工具栏
    ''' ListInfo中元素的组成是:菜单名,菜单快捷访问符,父菜单(true时表示为父菜单,false表示为子菜单;只提供两级菜单),父菜单名
    ''' 是否添加到工具栏显示(true为在工具栏中显示),Imagelist对象,Imagelist中的index
    Public Sub CreateToolbar(ByRef ListInfo As List(Of List(Of Object)), ByRef imagelist1 As System.Windows.Forms.ImageList, ByVal PicTextLayout As Integer)
        DisposeToolbar()
        Dim t As Integer = 0
        Dim i As Integer = 0
        CWidth = 0
        CHeight = 0
        MaxBtnWidth = 0
        MaxBtnHeight = 0
        If IsNothing(ListInfo) = False Then
            If ListInfo.Count > 0 Then
                For i = 0 To ListInfo.Count - 1
                    '是否父菜单
                    If CBool(ListInfo.Item(i).Item(3)) = False Then


                        '是否这个子菜单在工具栏上显示;是则生成工具栏
                        If CBool(ListInfo.Item(i).Item(5)) = True Then
                            '创建图片按钮
                            Me.Panel1.Controls.Add(New PictureButtonToolbar.PictureButtonToolbar)
                            '背景色
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).BackColor = System.Drawing.SystemColors.ControlLight
                            '边框样式
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).BorderStyle = Windows.Forms.BorderStyle.None
                            '是否自动适应大小
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).ControlSizeSetF = True
                            '图片按钮的图片文字排版格式
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).TextImageLayout = CType(PicTextLayout, PictureButtonToolbar.PictureButtonToolbar.Layout)
                            '添加图片按钮的图片
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).AddBmp(imagelist1.Images(CType(ListInfo.Item(i).Item(6), Integer)))
                            '记下当前按钮的外接应用程序名
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).SetAppName = ListInfo.Item(i).Item(7).ToString
                            '记下当前图片按钮的菜单号
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).SetTag = CType(ListInfo.Item(i).Item(1), Integer)
                            '按钮文本
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).Caption = CType(ListInfo.Item(i).Item(0), String)
                            '控件索引
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).Text = t.ToString
                            '重绘确定大小
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).RefreshPicBtn()
                            '是否自动适应大小
                            CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).ControlSizeSetF = False
                            '获取最大值,以便于统一所有工具栏上按钮的大小
                            If CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).Width > MaxBtnWidth Then
                                MaxBtnWidth = CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).Width
                            End If
                            If CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).Height > MaxBtnHeight Then
                                MaxBtnHeight = CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).Height
                            End If
                            '绑定单击事件
                            AddHandler CType(Me.Panel1.Controls.Item(t), PictureButtonToolbar.PictureButtonToolbar).Click, AddressOf ActiveEvent
                            t = t + 1
                        End If
                    End If
                Next
                '工具栏背景色
                ' Me.Panel1.BackColor = Me.BackColor
                '工具栏整个的大小
                If HorVerFlag = False Then
                    Me.Width = Me.Panel1.Controls.Count * (MaxBtnWidth + 1) - 1
                    Me.Height = MaxBtnHeight
                Else
                    Me.Width = MaxBtnWidth
                    Me.Height = Me.Panel1.Controls.Count * (MaxBtnHeight + 1) - 1
                End If
                CWidth = Me.Width
                CHeight = Me.Height
                Me.Panel1.Size = New System.Drawing.Size(CWidth, CHeight)
                Me.Panel1.Location = New Point(0, 0)
                For j As Integer = 0 To Me.Panel1.Controls.Count - 1
                    '大小重新设置
                    CType(Me.Panel1.Controls.Item(j), PictureButtonToolbar.PictureButtonToolbar).Size = New System.Drawing.Size(MaxBtnWidth, MaxBtnHeight)
                Next


                '工具栏中控件重新排版
                Dim xzb As Integer = 0
                Dim yzb As Integer = 0
                For k As Integer = 0 To Me.Panel1.Controls.Count - 1
                    If HorVerFlag = False Then
                        CType(Me.Panel1.Controls.Item(k), PictureButtonToolbar.PictureButtonToolbar).Location = New Point(xzb, 0)
                        xzb = (CType(Me.Panel1.Controls.Item(k), PictureButtonToolbar.PictureButtonToolbar).Width + 1) + xzb
                    Else
                        CType(Me.Panel1.Controls.Item(k), PictureButtonToolbar.PictureButtonToolbar).Location = New Point(0, yzb)
                        yzb = (CType(Me.Panel1.Controls.Item(k), PictureButtonToolbar.PictureButtonToolbar).Height + 1) + yzb
                    End If
                Next
            End If
        End If
        Me.Panel1.Refresh()
        Me.CurBtnLayout = PicTextLayout
        Me.Refresh()
    End Sub
    ''' <summary>
    ''' 销毁控件,以便于释放控件占用的资源
    ''' </summary>
    ''' 销毁按钮
    ''' 销毁工具栏
    Public Sub DisposeToolbar()
        For i As Integer = Me.Panel1.Controls.Count - 1 To 0 Step -1
            '销毁
            RemoveHandler CType(Me.Panel1.Controls.Item(i), PictureButtonToolbar.PictureButtonToolbar).Click, AddressOf ActiveEvent
            CType(Me.Panel1.Controls.Item(i), PictureButtonToolbar.PictureButtonToolbar).DisposePictureButton()
            CType(Me.Panel1.Controls.Item(i), PictureButtonToolbar.PictureButtonToolbar).Dispose()
        Next
        Me.Panel1.Controls.Clear()
        Me.Panel1.Refresh()
        Me.Refresh()
    End Sub
    ''' <summary>
    ''' 对此工具栏控件的按下状态进行重置;同时激发按钮的按下自定义事件
    ''' </summary>
    ''' 激发事件处理函数
    Private Sub ActiveEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim PicBtn As PictureButtonToolbar.PictureButtonToolbar
        PicBtn = CType(sender, PictureButtonToolbar.PictureButtonToolbar)
        RaiseEvent btnclick(CInt(PicBtn.Tag), CStr(PicBtn.BtnAppName))
    End Sub
   
    '重新排列工具栏图标
    Public Sub ReLayoutToolBar()
        '工具栏整个的大小
        If HorVerFlag = False Then
            Me.Width = Me.Panel1.Controls.Count * (MaxBtnWidth + 1) - 1
            Me.Height = MaxBtnHeight
        Else
            Me.Width = MaxBtnWidth
            Me.Height = Me.Panel1.Controls.Count * (MaxBtnHeight + 1) - 1
        End If
        CWidth = Me.Width
        CHeight = Me.Height
        Me.Panel1.Size = New System.Drawing.Size(CWidth, CHeight)
        Me.Panel1.Location = New Point(0, 0)
        '工具栏中控件重新排版
        Dim xzb As Integer = 0
        Dim yzb As Integer = 0
        For k As Integer = 0 To Me.Panel1.Controls.Count - 1
            If HorVerFlag = False Then
                CType(Me.Panel1.Controls.Item(k), PictureButtonToolbar.PictureButtonToolbar).Location = New Point(xzb, 0)
                xzb = (CType(Me.Panel1.Controls.Item(k), PictureButtonToolbar.PictureButtonToolbar).Width + 1) + xzb
            Else
                CType(Me.Panel1.Controls.Item(k), PictureButtonToolbar.PictureButtonToolbar).Location = New Point(0, yzb)
                yzb = (CType(Me.Panel1.Controls.Item(k), PictureButtonToolbar.PictureButtonToolbar).Height + 1) + yzb
            End If
        Next
    End Sub
End Class