网站首页站长博客下载中心域名交易站长论坛域名主机免费电邮免费域名中文排行排名查询站长书库书籍教程下载
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
学院首页 网络编程 网页设计 图形图象 数 据 库 服 务 器 网络媒体 网络安全 个人专栏 站长CLUB 业界新闻 信息公告
 当前位置:首页 >> 网络编程 >> NET专区 >> 正文
公告通知
返回上级列表
资料搜索
相关文章
用户自定义控件的应用
c#.net常用函数和方法集
在VB中使用水晶报表的一种简易编
C#调用父类的父类的方法
浏览.NET Framework 2.0 类型库中
为.Text Blog 添加 计数器
编程实现邮件地址有效性检测 
VB/VB.NET/C#导出到Excel的方法
c#高性能在WEB端产生验证图片
用System.Web.Caching.Cache保存
自定义控件--图片按钮
[ 来源:CSDN | 作者:未知 | 时间:2006-4-15 6:21:28 | 浏览:人次 ]
收藏到新浪ViVi 收藏到365KEY 收藏到我摘  字号选择〖    〗/ 双击滚屏 单击停止  
 

图片按钮:

思路:很简单,就是在一个picturebox控件上放置一个button控件,然后将这个button添加进picturebox上(确保先拖拽picturebox,后拖拽button,设置这个button的背景色(这个时候是相对于picturebox)为透明。

 

Imports System.ComponentModel

Public Class picturebutton

    Inherits System.Windows.Forms.UserControl

 

#Region " Windows 窗体设计器生成的代码 "

 

    'UserControl 重写 dispose 以清理组件列表。

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing Then

            If Not (components Is Nothing) Then

                components.Dispose()

            End If

        End If

        MyBase.Dispose(disposing)

    End Sub

 

    'Windows 窗体设计器所必需的

    Private components As System.ComponentModel.IContainer

 

    '注意:以下过程是 Windows 窗体设计器所必需的

    '可以使用 Windows 窗体设计器修改此过程。

    '不要使用代码编辑器修改它。

    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

    Friend WithEvents Button1 As System.Windows.Forms.Button

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        Me.PictureBox1 = New System.Windows.Forms.PictureBox()

        Me.Button1 = New System.Windows.Forms.Button()

        Me.SuspendLayout()

        '

        'PictureBox1

        '

        Me.PictureBox1.Name = "PictureBox1"

        Me.PictureBox1.Size = New System.Drawing.Size(136, 40)

        Me.PictureBox1.TabIndex = 0

        Me.PictureBox1.TabStop = False

        '

        'Button1

        '

        Me.Button1.Name = "Button1"

        Me.Button1.TabIndex = 1

        Me.Button1.Text = "Button1"

        '

        'picturebutton

        '

        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.PictureBox1})

        Me.Name = "picturebutton"

        Me.ResumeLayout(False)

 

    End Sub

 

#End Region

    Public Sub New()

        MyBase.New()

 

        '该调用是 Windows 窗体设计器所必需的。

        InitializeComponent()

 

        ' InitializeComponent() 调用之后添加任何初始化

        Me.Button1.Width = 100 ‘设置按钮的初始大小

        Me.Button1.Height = 23

      

 

        Me.Button1.BackColor = Color.Transparent 背景色透明

        Me.Button1.ForeColor = Color.Black

        Me.PictureBox1.Controls.Add(Me.Button1)

    End Sub

    Private m_text As String ‘设置按钮标题

    Private a As Integer

 

    'Private m_image As Image

    <Description("picturebox图片。")> _

    Public Property image() As image

        Get

            Return Me.PictureBox1.Image

        End Get

        Set(ByVal Value As image)

            Me.PictureBox1.Image = Value

            Invalidate()

        End Set

    End Property

 

    Shadows Property forecolor() As Color

        Get

            Return Me.Button1.ForeColor

        End Get

        Set(ByVal Value As Color)

            Me.Button1.ForeColor = Value

            Invalidate()

        End Set

    End Property

    Shadows Sub ResetForeColor()

        Me.Button1.ForeColor = SystemColors.ControlText

    End Sub

    '////

    '按钮的单击事件

    Event BtnClick(ByVal sender As Object, ByVal e As System.EventArgs)

 

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        RaiseEvent BtnClick(Me, e)

    End Sub

    '////

 

    '控件改变大小时,需重绘控件,以使子控件排位美观

    Private Sub FileTextBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize

        RedrawControls()

    End Sub

 

    '子控件会自动继续容器的Font属性,所以改变容器的Font属性时也要重绘控件

    Protected Overrides Sub OnFontChanged(ByVal e As System.EventArgs)

        '让基控件更新文本框

        MyBase.OnFontChanged(e)

        '重绘控件

        RedrawControls()

    End Sub

 

    '重绘控件

    Private Sub RedrawControls()

        '控件宽度

        Dim width As Integer = Me.ClientRectangle.Width '获得工作区宽

        '以按钮的高度来确定控件高度

        Dim btnSide As Integer = Button1.Height

        Dim btnwidth As Integer = Button1.Width

        If Me.ClientRectangle.Height <> btnSide Then

            '设置控件工作区的大小

            'Me.SetClientSizeCore(btnwidth, btnSide)

            Me.SetClientSizeCore(width, btnSide) '这里使用工作区的宽是因为:按钮和picturebox可以调整宽度

            '上面的语句激发了嵌套的Resize事件,因此需要立即退出,如果不退出,就会反复调用进入死循环

            Exit Sub

        End If

 

        '调整子控件的大小

        'Txt.SetBounds(0, 0, width, btnSide)

        'Btn.SetBounds(width - 19, 2, 17, btnSide - 4)

 

        Me.PictureBox1.SetBounds(0, 0, width, btnSide)

        Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

        Me.Button1.SetBounds(0, 0, width, btnSide)

 

    End Sub

End Class


[发送给好友]  [打印本页]  [关闭窗口]  [返回顶部]   转载请注明来源:http://edu.chinaz.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
栏目编辑: 设计风 责任编辑: keke
原始作者: 未知 录入时间: 2006-4-15 6:21:28
信息来源: CSDN 投稿信箱: Edu#chinaz.com
设为首页 - 加入收藏 - 关于我们 - 广告服务 - 版权申明 - 友情链接 - 联系方式 - 总编信箱 - 会员投稿