VB.Net创建不规则窗体
一般说来,应用程序的窗体都是规则的,即是矩形窗体。有时候为了某种特殊的用途,我们希望改变应用程序窗体的形状,比如做个个性十足的
mp3
播放器,小时钟等等,这就需要
"
定制
"
我们的应用程序。另外,特殊形状的窗体有时候也能吸引用户的注意力,使得他们格外注意你的程序。
在 VB6 里面,我们一般通过以下代码来创建不规则窗体。
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, _ ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, _ ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Sub Form_Activate()
Dim hndRgn As Long
hndRgn = CreateEllipticRgn(0, 0, 175, 175)
Call SetWindowRgn(Me.hWnd, hndRgn, True)
Call DeleteObject(hndRgn)
End Sub
首先用 Win32 API CreateEllipticRgn 创建一个圆形的区域,然后设置 Form 的区域为用户自定义的 Region ,这样我们就可以得到下面一个圆形的窗体
在 VB6 里面,我们一般通过以下代码来创建不规则窗体。
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, _ ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, _ ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Sub Form_Activate()
Dim hndRgn As Long
hndRgn = CreateEllipticRgn(0, 0, 175, 175)
Call SetWindowRgn(Me.hWnd, hndRgn, True)
Call DeleteObject(hndRgn)
End Sub
首先用 Win32 API CreateEllipticRgn 创建一个圆形的区域,然后设置 Form 的区域为用户自定义的 Region ,这样我们就可以得到下面一个圆形的窗体
为了更美观一点,我们可以设
Form
的
BorderStyle
为
None
,然后自己画一个
TitileBar
,就可以做成比较活泼的应用程序。
VB.Net 里面如何实现以上效果:
VB.NET 是一中跨平台的语言,更好的利用了面向对象机制。它的面向对象能力扩展了语言本身的通路:一切都是对象。这意味着比在以前的 VB 版本里,你获得了更多的内在功能,你将很少被迫使用 Windows API 。因此在 VB.Net 里面我们也只好放弃 VB6 里面利用 API 的观念,用 VB.Net 强大的对象机制来阐述以上话题。
在 VB.Net 里面, Form 有一个 Reigin 属性,我们通过创建自定义的 Reigin ,然后指定 Form 的 Reigin ,就可以得到不规则的窗体。而且 VB.Net 里面的 Reigin 对象功能强大,远超过了之前 VB 的限制,因此我们可以作出很多漂亮的界面。
VB.Net 里面如何实现以上效果:
VB.NET 是一中跨平台的语言,更好的利用了面向对象机制。它的面向对象能力扩展了语言本身的通路:一切都是对象。这意味着比在以前的 VB 版本里,你获得了更多的内在功能,你将很少被迫使用 Windows API 。因此在 VB.Net 里面我们也只好放弃 VB6 里面利用 API 的观念,用 VB.Net 强大的对象机制来阐述以上话题。
在 VB.Net 里面, Form 有一个 Reigin 属性,我们通过创建自定义的 Reigin ,然后指定 Form 的 Reigin ,就可以得到不规则的窗体。而且 VB.Net 里面的 Reigin 对象功能强大,远超过了之前 VB 的限制,因此我们可以作出很多漂亮的界面。
Regin 对象
System.Drawing 的一个对象,指示由矩形和由路径构成的图形形状的内部。因为区域的坐标是在全局坐标中指定的,所以可对它进行缩放。然而,在绘制表面上,它的内部取决于表示它的像素的大小和形状。应用程序可以使用若干区域来堆砌绘图操作的输出。窗口管理器则使用区域来定义窗口的绘制区域。这些区域被称为剪辑区域。应用程序还可以在命中检测的操作中使用区域,例如检查一个点或矩形是否与某个区域相交。应用程序可通过使用 Brush 对象来填充一个区域。
GraphicsPath 表示一系列相互连接的直线和曲线,应用程序使用路径来绘制形状的轮廓、填充形状内部和创建剪辑区域。图形引擎在全局坐标空间中维护路径内的几何形状的坐标。路径可由任意数目的图形(子路径)组成。每一图形都是由一系列相互连接的直线和曲线或几何形状基元构成的。图形的起始点是相互连接的一系列直线和曲线中的第一点。终结点是该序列中的最后一点。几何形状基元的起始点和终结点都是由基元规范定义的。
下面我们看两个具体的实例
1. 文字格式的窗体
在
Form
的
Load
事件加入以下代码:
Dim text_path As GraphicsPath
Dim text_region As Region
Me.BackColor = Color.Red
Me.Width = 600
' Create the text path.
text_path = New GraphicsPath( Drawing.Drawing2D.FillMode.Alternate)
text_path.AddString("CSDN", New FontFamily("Times New Roman"), FontStyle.Bold, 200,New Point(10, 10),
StringFormat.GenericDefault)
' Create a Region from the path.
text_region = New Region(text_path)
' Constrain the form to the region.
Me.Region = text_region
Dim text_path As GraphicsPath
Dim text_region As Region
Me.BackColor = Color.Red
Me.Width = 600
' Create the text path.
text_path = New GraphicsPath( Drawing.Drawing2D.FillMode.Alternate)
text_path.AddString("CSDN", New FontFamily("Times New Roman"), FontStyle.Bold, 200,New Point(10, 10),
StringFormat.GenericDefault)
' Create a Region from the path.
text_region = New Region(text_path)
' Constrain the form to the region.
Me.Region = text_region
运行将将得到如下形状的窗体,记住按
Shift
+
F5
中止程序。
2. 椭圆形状的窗体:
同样加入以下代码,得到如下窗体
Me.Width = 300
Me.Height = 220
Me.BackColor = Color.RoyalBlue
Dim m_path As GraphicsPath
m_path = New GraphicsPath(FillMode.Winding)
m_path.AddEllipse(1, 1, 200, 200)
Dim m_region As New Region(m_path)
Me.Region = m_region
Me.Height = 220
Me.BackColor = Color.RoyalBlue
Dim m_path As GraphicsPath
m_path = New GraphicsPath(FillMode.Winding)
m_path.AddEllipse(1, 1, 200, 200)
Dim m_region As New Region(m_path)
Me.Region = m_region
以上只是两个简单的例子,通过利用
Region
和
GraphicsPath
对象,你将得到更多有趣的效果。