不规则窗体或控件(想怎样不规则就怎样不规则)

不规则窗体或控件(想怎样不规则就怎样不规则)

''''#############     注意        #######################
''''
'''            转载请保留原版信息及源码       
''''
''''
'''    
http://blog.youkuaiyun.com/kevery_net/
'''      http://25hour.netyi.org
''''

         '''功能:显示出不规则窗体或控件
         '''源码出自:贰伍小时(25hour  或 kevery )(写)
         '''日期:2005-06-06
         '''开发工具:.NET  
         '''语言:VB.NET
''''####################################################

以下为实例:

实例工程下载:vbnetformskin

实例所需图片:

创建一个VB.NET桌面程序"vbnetformskin"并在工程中创建一个窗体"form1.vb"并设置窗体景图片为上面那图片,设置窗体样式:FormBorderStyle = None,再在窗体中创建一个按钮"Button1"以下为源码:

Option Strict Off
Imports System.ComponentModel

Imports System.Drawing

Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private components As System.ComponentModel.Container

    Friend WithEvents Button1 As System.Windows.Forms.Button

 
    Public Sub New()
        MyBase.New()

        InitializeComponent()
    End Sub

    Public Overloads Sub Dispose() '释放资源,终止程序

        MyBase.Dispose() '调用父类的方法

        components.Dispose() '释放组件占用的资源

    End Sub

    Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
        Me.Button1.Location = New System.Drawing.Point(198, 244)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(212, 38)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
        Me.ClientSize = New System.Drawing.Size(580, 298)
        Me.Controls.Add(Me.Button1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.ImeMode = System.Windows.Forms.ImeMode.Disable
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

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

        SkinRegion_Xor(Me)
        'SkinRegion_Union(Me)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        Button1.Text = GetRGB(Me, e.X, e.Y).ToString

    End Sub
End Class

1.原窗体图

2.不规则后窗体图

3.在工程中创建一个模块 "Module1.vb"  代码如下:

''''#############     注意        #######################
''''
'''            转载请保留原版信息及源码       
''''
''''
'''
'''     
http://25hour.netyi.org
''''
''''####################################################
'''功能:显示出不规则窗体或控件
'''源码出自:贰伍小时 25hour (写)
'''日期:2005-06-06
'''开发工具:.NET  
'''语言:VB.NET
'''本源码根中的SkinRegion_Union(..)方法模访[一鹤(hejianzhong)]写..
''''经我做适当修改并添加了一个方法SkinRegion_Xor(..)后,本人认为以比未修改之前精华多不少.
''''其中这两个方法实现的目的一至.第一个用了UNION并集,而第二个却相反相,用了XOR减去交集.
'''使用方法:
''第一,必须在要做不规则的控件上设置背景图片,若不设置的,在调用时,
''''''必须转参数skinFilePath文件名(如:参数值skinFilePath为skinimage.bmp,并该图片必须与本程序同一目录)
''第二为调用:如在窗体进入时进行不规则处理,则在窗体事件Private Sub Form1_Load(......)中直接调用,
''''源码为:SkinRegion_Union(me) 或 SkinRegion_Xor(me) 其中参数me为当窗体,即充当要做不规则的控件.

Public Module SKin

    Public Function SkinRegion_Union(ByVal SKControl As Control, Optional ByVal skinFilePath As String = Nothing) As Form
        Dim Rect As New Rectangle(0, 0, 0, 0)
        Dim RegIon As New System.Drawing.Region(Rect)

        Dim X, Y, Left_X, Right_X, BgWidth, BgHeight As Integer

        Dim RGB As Integer = GetRGB(SKControl, 0, 0)  ''获取某坐标的颜色(在此获取要透明的颜色)

        If skinFilePath <> Nothing Then SKControl.BackgroundImage = SKControl.BackgroundImage.FromFile(System.Windows.Forms.Application.StartupPath & "/" & skinFilePath)

        BgWidth = SKControl.BackgroundImage.Width

        BgHeight = SKControl.BackgroundImage.Height

        SKControl.Height = BgHeight

        SKControl.Width = BgWidth

        For Y = 0 To BgHeight

            X = 0

            Do

                While X <= BgWidth

                    If GetRGB(SKControl, X, Y) <> RGB Then
                        Exit While
                    End If

                    X = X + 1

                End While

                Left_X = X ''获取与指定坐标不同颜色的坐标开始位置

                While X <= BgWidth

                    If GetRGB(SKControl, X, Y) = RGB Then
                        Exit While
                    End If

                    X = X + 1

                End While

                Right_X = X - 1 ''获取与指定坐标不同颜色的坐标结束位置

                If Left_X <= Right_X Then

                    '创建一个具有指定边缘位置的 Rectangle 结构。
                    '此方法创建的新 Rectangle。

                    Rect = Rect.FromLTRB(Left_X, Y, Right_X, Y + 1) ''获取域窗体

                    '将此 Region 对象更新为其自身与指定 Rectangle 结构的并集。
                    '要与此 Region 对象合并的 Rectangle 结构。

                    RegIon.Union(Rect) ''并合域窗体

                End If


            Loop Until X >= BgWidth

        Next Y

        SKControl.Region = RegIon

        Rect = Nothing
        RegIon.Dispose()
        Return SKControl
    End Function

    Public Function SkinRegion_Xor(ByVal SKControl As Control, Optional ByVal skinFilePath As String = Nothing) As Region

        Dim Rect As New Rectangle(0, 0, 0, 0)

        Dim RegIon As Region

        Dim X, Y, Left_X, Right_X, BgWidth, BgHeight As Integer

        Dim RGB As Integer = -16777216 ''获取某坐标的颜色(-16777216为黑色)(在此获取要透明的颜色为黑色)
        Dim FirARGB2 As Integer = -1 ''获取某坐标的颜色(-1为白色)(在此获取要透明的颜色为红白色),也就是黑色和白色为透明色

        If skinFilePath <> Nothing Then SKControl.BackgroundImage = SKControl.BackgroundImage.FromFile(System.Windows.Forms.Application.StartupPath & "/" & skinFilePath)

        BgWidth = SKControl.BackgroundImage.Width

        BgHeight = SKControl.BackgroundImage.Height

        SKControl.Height = BgHeight

        SKControl.Width = BgWidth

        Rect = Rect.FromLTRB(0, 0, BgWidth, BgHeight)
        RegIon = New Region(Rect)


        For Y = 0 To BgHeight

            X = 0

            Do

                While X <= BgWidth

                    If GetRGB(SKControl, X, Y) = RGB Or GetRGB(SKControl, X, Y) = FirARGB2 Then
                        Exit While
                    End If
                    X = X + 1

                End While

                Left_X = X ''获取与指定坐标不同颜色的坐标开始位置

                While X <= BgWidth

                    If GetRGB(SKControl, X, Y) <> RGB And GetRGB(SKControl, X, Y) <> FirARGB2 Then
                        Exit While
                    End If

                    X = X + 1

                End While

                Right_X = X  ''获取与指定坐标不同颜色的坐标结束位置

                If Left_X <= Right_X Then

                    Rect = Rect.FromLTRB(Left_X, Y, Right_X, Y + 1)
                    RegIon.Xor(Rect)

                End If


            Loop Until X >= BgWidth

        Next Y

        SKControl.Region = RegIon

        Rect = Nothing
        RegIon.Dispose()

        Return SKControl.Region
    End Function

    Public Function GetRGB(ByVal SKControl As Control, ByVal x As Integer, ByVal y As Integer) As Integer

        Dim pm As Bitmap = SKControl.BackgroundImage

        Try

            Return pm.GetPixel(x, y).ToArgb

        Catch

            Exit Function

        End Try

    End Function

End Module

完成!

试运行看,是不是OK了...........哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值