试试下面的代码:
'This sample show you how to cut the caption title exactly and create controls with code.
'add your menu to the form
'add a new form ,do nothing but copy this codes for it.
Option Explicit
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal x1 As Long, ByVal y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private WithEvents cmd1 As CommandButton
Private WithEvents cmd2 As CommandButton
Private Sub cmd1_Click() '******************
Dim capheight As Long, area As Long
Me.ScaleMode = 2
Me.ForeColor = vbRed
Me.Line (1, 0)-(Me.Width, 0) '紧贴标题栏画线做标记
capheight = GetSystemMetrics(33) + GetSystemMetrics(4) ' 边框宽度+标题栏高度
area = CreateRectRgn(0, capheight, Me.Width, Me.Height) '画无标题栏的矩形框
area = SetWindowRgn(Me.hWnd, area, True) '裁剪标题栏
End Sub
Private Sub cmd2_Click() '*******************
Me.ScaleMode = 2
Dim area As Long
area = CreateRectRgn(0, 0, Me.Width, Me.Height) '画含标题栏的矩形框
area = SetWindowRgn(Me.hWnd, area, True) '恢复标题栏
End Sub
Private Sub Form_Load()
Me.WindowState = 0
Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2, 8000, 5000'居中及设置窗体大小
Set cmd1 = Controls.Add("VB.CommandButton", "cmd1", Me)'添加cmd1按纽
Set cmd2 = Controls.Add("VB.CommandButton", "cmd2", Me)'添加cmd2按纽
cmd1.Move 1000, 1000, 1500, 500
cmd2.Move 3000, 1000, 1500, 500
cmd1.Caption = "裁剪标题栏"
cmd2.Caption = "恢复标题栏"
cmd1.Visible = True
cmd2.Visible = True
End Sub
'模块中写入
Public Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Public Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long ''以下3个切除窗口的边框
Public Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Public Type POINTAPI
X As Long
Y As Long
End Type
Public Type rect
left As Long
top As Long
right As Long
bottom As Long
End Type
Public Sub MakeNoBorderForm(frm As Form)
'切除窗口的边框
Dim rctClient As rect, rctFrame As rect
Dim hRgn As Long
Dim lRes As Long
ReDim XY(3) As POINTAPI
Dim lpTL As POINTAPI, lpBR As POINTAPI
'获得窗口矩形区域
'将窗口矩形坐标转换为屏幕坐标
lpTL.X = frm.left / 15
lpTL.Y = frm.top / 15
ScreenToClient frm.hWnd, lpTL
rctClient.left = Abs(lpTL.X)
rctClient.top = Abs(lpTL.Y)
frm.ScaleMode = 1 'Twip
rctClient.right = frm.ScaleWidth / 15 + Abs(lpTL.X)
rctClient.bottom = frm.ScaleHeight / 15 + Abs(lpTL.Y)
'建立要切割的数组
XY(0).X = rctClient.left
XY(0).Y = rctClient.top
XY(1).X = rctClient.right
XY(1).Y = rctClient.top
XY(2).X = rctClient.right
XY(2).Y = rctClient.bottom
XY(3).X = rctClient.left
XY(3).Y = rctClient.bottom
hRgn = CreatePolygonRgn(XY(0), 4, 2)
lRes = SetWindowRgn(frm.hWnd, hRgn, True)
End Sub
'窗体加载
Private Sub Form_Initialize()
MakeNoBorderForm Me '创建没有任务栏的窗体
End Sub