VB中你想创建一个简单的状态栏也用上那个几MB的OCX吗?有没有方法可以不用呢,下面就是以API来创建
一个状态栏,VB让初学朋友学得很方便,但对你对VB有所了解的时候,却发现VB原来就是鸡肋......
先创建一个窗体,在窗体里添加两个Button,然后写下以下代码:
'-----------------------------------------------
'
' Create Status Bar Demo
'
' code by loveboom[DFCG][FCG][US]
' email:loveboom#163.com
' http://blog.csdn.net/bmd2chen
'
'-------------------------------------------------
Dim hWndBar As Long ;状态栏句柄
Private Const IDC_STATBAR As Long = &H2005 '状态栏ID
Private Sub Command1_Click()
Dim ret As Boolean
ret = CreateStatBar(Me.hwnd, IDC_STATBAR, hWndBar)
If ret = True Then
MsgBox "创建状态栏成功!"
Else
MsgBox "创建状态栏失败:-(!", 48
End If
End Sub
Private Sub Command2_Click()
SetBarText hWndBar, 1, "Create StatusBar Demo:-)!"
End Sub
'移动状态栏
Private Sub Form_Resize()
MoveStatWindow hWndBar
End Sub
然后添加一个模块,模块里写上代码:
Private Const WS_CHILD As Long = &H40000000 'WS_CHILD 和WS_VISIBLE是必需函数
Private Const WS_VISIBLE As Long = &H10000000
Private Const WM_USER As Long = &H400
Private Const SB_SETPARTS As Long = (WM_USER + 4) '这两个常数在VB自带的api查询器里没有,需要手工添加
Private Const SB_SETTEXTA As Long = (WM_USER + 1)
Private Declare Function CreateStatusWindow Lib "comctl32.dll" (ByVal style As Long, ByVal lpszText As String, ByVal hwndParent As Long, ByVal wID As Long) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
Private Declare Function MoveWindow Lib "user32.dll" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
'--------------------------------------------------
'
' 创建状态栏
'函数说明:
'ParenthWnd状态栏所属的句柄
'IDC_STATBAR 状态栏的ID号,用于对状态栏的单击之类的操作
'hBarWin 函数返回状态栏的句柄
'szText 要显示的信息
'
'---------------------------------------------------
Function CreateStatBar(ParenthWnd As Long, IDC_STATBAR As Long, hBarWin As Long, Optional szText As String = "Demo") As Boolean
Dim ret As Long '返回值
Dim bar(0 To 1) As Long '分栏的各项位置
Dim szbar As Long '分栏的数目
'-------------------------------------------------------
'定义数组
bar(0) = 235 '第一栏宽度为245
bar(1) = -1 '-1表示后面的分为一栏
'-------------------------------------------------------
ret = CreateStatusWindow(WS_CHILD Or WS_VISIBLE, ByVal szText, ParenthWnd, IDC_STATBAR) '创建状态栏
szbar = 2
If ret = 0 Then '如果创建失败则退出过程
CreateStatBar = False
Exit Function
End If
hBarWin = ret '返回状态栏的句柄
If szbar > 1 Then '因为默认就是分一栏所以,这里判断为大于1就是分栏
SendMessage hBarWin, SB_SETPARTS, szbar, bar(0) '分栏
End If
CreateStatBar = True '创建成功返回真值
End Function
'----------------------------
'移动状态栏
'----------------------------
Sub MoveStatWindow(hBar As Long)
If hBar Then '如果状态栏句柄不为0则移动
Call MoveWindow(hBar, 0, 0, 0, 0, True)
End If
End Sub
'------------------------------
'在指定栏上显示信息
'hBar 为状态栏的句柄
'szbar 指定要在哪一栏显示信息,从0开始计,也就是说,如果分两栏,我们要在第二栏里显示信息,szbar就设置为1
'szText 要显示的信息
'-------------------------------
Sub SetBarText(hBar As Long, szbar As Long, strText As String)
SendMessage hBar, SB_SETTEXTA, szbar, ByVal strText
End Sub
这样状态栏就基本可行了,当然,你还可以用SetWindowLong来处理状态栏信息.
程序在Win 2003 vb6 sp6下测试通过