Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
If (IsNormalRegion) Then
''构造一个GraphicsPath对象实例
Dim Graphics As New System.Drawing.Drawing2D.GraphicsPath()
Dim intHeight As Integer = Me.Size.Height
Dim intWidth As Integer = Me.Size.Width
''定义内矩形的左上角坐标
Dim RectTop As Integer = 100
''在窗体上绘制一个大椭圆,左上角的坐标取为(0,0)
Graphics.AddEllipse(0, 0, intWidth, intHeight)
''再绘制一个小矩形
Dim AddRect As New Rectangle(RectTop, RectTop, intHeight - (RectTop * 2), intHeight - (RectTop * 2))
Graphics.AddRectangle(AddRect)
''设置窗口的可见区域
Me.Region = New Region(Graphics)
Else
Me.Region = Nothing
End If
IsNormalRegion = Not IsNormalRegion
End Sub
程序运行的结果如下:
2、使窗体在其他所有窗体之上(Allway On Top)
VB6中实现(借助API函数SetWindowPos)
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
---- hWnd变元是窗口的句柄;x,y是窗口的左上角的坐标;cx、cy是窗口宽度和高度;hWndInsertAfter变元是窗口清单中hWnd窗口前面的窗口句柄,有四个可选值:
序号 可 选 值 作 用
1 HWND_BOTTOM 把窗口放在窗口清单的底部
2 HWND_TOP 把窗口放在窗口清单的字符顺序的顶部
3 HWND_TOPMOST 把窗口放在窗口清单的顶部
4 HWND_NOTOPMOST 把窗口放在窗口清单的顶部,最上层窗口之下
---- WFlags变元为整型值,有八个可选值:
序号 可 选 值 作用
1 SWP_DRAWFRAME 在窗口周围画一个方框
2 SWP_HIDEWINDOW 隐藏窗口
3 SWP_NOACTIVATE 不激活窗口
4 SWP_NOMOVE 保持窗口当前位置
5 SWP_NOREDRAW 窗口不自动重画
6 SWP_NOSIZE 保持窗口当前尺寸
7 SWP_NOZORDER 保持窗口在窗口清单中的当前位置
8 SWP_SHOWWINDOW 显示窗口
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, _
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const SWP_NOMOVE = 2
Private Const SWP_NOSIZE = 1
Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Sub Command1_Click()
''把窗体放在最前面:
res% = SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
End Sub
Private Sub Command2_Click()
''使窗体恢复普通模式:
res% = SetWindowPos(Form1.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
End Sub
在VB.NET中,太简单了!系统为窗体提供了TopMost属性,我们将TopMost属性设置为True,就实现了Allways On Top 的功能,要取消此功能,设置为False即可。
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TopMost = True
End Sub
3、窗体透明度渐变效果
我们还是来看一下在VB6中的实现,VB6中实现(借助API函数SetLayeredWindowAttributes)
使用这个函数,可以轻松的控制窗体的透明度。按照微软的要求,透明窗体在创建时应使用WS_EX_LAYERED参数(用CreateWindowEx),或者在创建后设置该参数(用SetWindowLong),我选用后者。
SetLayeredWindowAttributes函数,其中hwnd是透明窗体的句柄,crKey为颜