| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 其他 >> 实例分析正文
最新推荐 更多内容
创建自定义绘制的用户控件(VB2010实例)
创建自定义绘制的用户控件(VB2010实例)
作者:Akyao    来源:乐博网收集     更新时间:2011-5-10

本文演示如何创建自定义绘制的用户控件、在窗体中使用它们、使控件能够对事件作出响应,以及定义在设计视图中使用的属性,来自乐博网。

如果你想下载本文的源代码RAR压缩集合包  请访问
VB2010源代码集合包(芋头糕)    http://www.lob.cn/code/utility/2795.shtml
特别感谢网友 芋头糕 将此资源提供乐博网分享,欢迎加入 40797788 的.Net超级qq群交流。

演示如何创建自定义绘制的用户控件、在窗体中使用它们、使控件能够对事件作出响应,以及定义在设计视图中使用的属性源代码如下:

' Copyright (c) Microsoft Corporation. All rights reserved.
Imports System.Drawing.Drawing2D

Public Class BeadedScoreBoard
    Inherits System.Windows.Forms.UserControl

#Region "Variables and Enumerations"

    Private totalBeads As Integer = 10
    Private totalScore As Integer = 0
    Private beadDiameter As Integer = CInt(Me.Width / (totalBeads + 2))
    Private privateBeadColor As System.Drawing.Color = Color.Black
    Private outlineColor As System.Drawing.Color = Color.Black
    Private isClickable As Boolean = True
    Private hasOutline As Boolean = True

#End Region

#Region "Control Properties"
    ' The color property of the beads.
    Public Property BeadColor() As System.Drawing.Color
        Get
            Return privateBeadColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            privateBeadColor = value
            Me.Invalidate()
        End Set
    End Property

    Public Property BeadOutlineColor() As System.Drawing.Color
        Get
            Return outlineColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            outlineColor = value
            Me.Invalidate()
        End Set
    End Property

    Public Property Clickable() As Boolean
        Get
            Return isClickable
        End Get
        Set(ByVal value As Boolean)
            isClickable = value
            Me.Invalidate()
        End Set
    End Property

    Public Property Outline() As Boolean
        Get
            Return hasOutline
        End Get
        Set(ByVal value As Boolean)
            hasOutline = value
            Me.Invalidate()
        End Set
    End Property

    ' The number of beads on the control.
    Public Property BeadCount() As Integer
        Get
            Return totalBeads
        End Get
        Set(ByVal value As Integer)
            If value > 0 Then
                totalBeads = value
            Else
                totalBeads = 10
            End If
            beadDiameter = CInt(Me.Width / (totalBeads + 2))
            Me.Invalidate()
        End Set
    End Property

    ' The score displayed by the control.
    Public Property Score() As Integer
        Get
            Return totalScore
        End Get
        Set(ByVal value As Integer)
            If value >= 0 Then
                If value < totalBeads Then
                    totalScore = value
                Else
                    totalScore = totalBeads
                End If
            Else
                totalScore = 0
            End If
            Me.Invalidate()
        End Set
    End Property

#End Region

#Region "Drawing Functions"
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        Dim rect As System.Drawing.Rectangle = e.ClipRectangle
        Dim g As Graphics = e.Graphics
        Dim mainPen As New Pen(Color.Black)

        ' Adjust the bead size depending on the chang in size of the control.
        beadDiameter = CInt(Me.Width / (totalBeads + 2))

        ' Draw Main Lines.
        mainPen.Width = CSng(System.Math.Ceiling(rect.Height / 100))
        g.DrawLine(mainPen, 0, CInt(rect.Height / 2), rect.Width, CInt(rect.Height / 2))
        mainPen.Width = 1
        g.DrawLine(mainPen, 0, CInt(rect.Height / 2 - beadDiameter / 2), 0, CInt(rect.Height / 2 + beadDiameter / 2))
        g.DrawLine(mainPen, rect.Width - 1, CInt(rect.Height / 2 - beadDiameter / 2), rect.Width - 1, CInt(rect.Height / 2 + beadDiameter / 2))

        ' Draw Left Beads.
        Dim i As Integer
        For i = 0 To totalScore - 1
            DrawBead(g, rect, CInt(i * beadDiameter + beadDiameter / 2))
        Next

        ' Draw Right Beads.
        For i = 0 To totalBeads - totalScore - 1
            DrawBead(g, rect, CInt(rect.Width - i * beadDiameter - beadDiameter / 2 - 1))
        Next

    End Sub

    ' Draws a bead on the line with x as the center.
    Private Sub DrawBead(ByVal g As Graphics, ByVal rect As System.Drawing.Rectangle, ByVal x As Integer)
        g.FillEllipse(New System.Drawing.Pen(privateBeadColor).Brush, x - CInt(beadDiameter / 2), CInt(rect.Height / 2 - beadDiameter / 2), beadDiameter, beadDiameter)
        If hasOutline Then
            g.DrawEllipse(New Pen(outlineColor), x - CInt(beadDiameter / 2), CInt(rect.Height / 2 - beadDiameter / 2), beadDiameter, beadDiameter)
        End If

    End Sub

#End Region

#Region "Event Handlers"

    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        If isClickable Then
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Score += 1
            Else
                Score -= 1
            End If
        End If
    End Sub

#End Region

End Class

[1] [2] 下一页

  • 上一篇:

  • 下一篇: 没有了
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    对文本、图像和树视图节点执行的拖放功能(VB2010实例)
    通用对话框处理文件、颜色和字体的方法(VB2010实例)
    演示如何以多种格式复制和检索剪贴板项(VB2010实例)
    语音API 和语音识别功能(VB2010实例)
    设置文本识别的内置识别器(VB2010实例)
    管理计算机电源状态的类(VB2010实例)
    Tablet PC 上可用的文本识别选项(VB2010实例)
    Tablet PC 2005 中对笔势的系统识别(VB2010实例)
    自定义数据控件(VB2010实例)
    Tablet PC 2005 的区分上下文功能(VB2010实例)
    响应数字化仪触笔背面的橡皮擦(VB2010实例)
    WMI编程实例(VB2010实例)
    系统服务管理实例(VB2010实例)
    进程管理(VB2010实例)
    显示进程组成模块(VB2010实例)
    任务管理器编程(VB2010实例)

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]