关闭

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

861人阅读 评论(0) 收藏 举报

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

''''#############     注意        #######################
''''
'''            转载请保留原版信息及源码       
''''
''''
'''    
http://blog.csdn.net/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了...........哈哈

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:36043次
    • 积分:641
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:3篇
    • 译文:0篇
    • 评论:12条
    最新评论
    KJ-NET