当前位置: 开源爱好者 » 编程教程 » VB教程 » 正文

VB6.0 使用API方法创建窗体状态栏

VB6.0 使用API方法创建窗体状态栏,单击窗体按钮后激活程序,创建一个状态栏,使用了API函数:

VB6.0 使用API方法创建窗体状态栏

代码部分,窗体部分代码如下:

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "用API创建状态栏"
   ClientHeight    =   1680
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4500
   LinkTopic       =   "Form1"
   ScaleHeight     =   1680
   ScaleWidth      =   4500
   StartUpPosition =   3  '窗口缺省
   Begin VB.CommandButton Command2
      Caption         =   "添加状态信息"
      Height          =   495
      Left            =   2400
      TabIndex        =   1
      Top             =   480
      Width           =   1215
   End
   Begin VB.CommandButton Command1
      Caption         =   "创建状态栏"
      Height          =   495
      Left            =   720
      TabIndex        =   0
      Top             =   480
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim hWndBar As Long '状态栏句柄
'www.srcfans.com
Private Const IDC_STATBAR As Long = &H2005 '状态栏ID
Private Sub Command1_Click()
Dim r As Boolean
r = CreateStatBar(Me.hwnd, IDC_STATBAR, hWndBar) '调用函数添加状态栏
If r = True Then
MsgBox "用API创建状态栏成功!" '创建成功提示
Else
MsgBox "用API创建状态栏失败!", 48  '创建失败提示
End If
End Sub
Private Sub Command2_Click()
SetBarText hWndBar, 1, "当前时间为:" & Time   '给状态栏添加内容
End Sub
Private Sub Form_Resize()
MoveStatWindow hWndBar '移动状态栏
End Sub

这里需要一个API函数配合:

Attribute VB_Name = "Module1"
'API函数声明
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
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) '手工添加
Private Const SB_SETTEXTA As Long = (WM_USER + 1)
' 创建状态栏
'函数说明:
'ParenthWnd  状态栏所属的句柄
'IDC_STATBAR 状态栏的ID号,用于对状态栏的单击之类的操作
'hBarWin 函数返回状态栏的句柄
'szText 要显示的信息

Function CreateStatBar(ParenthWnd As Long, IDC_STATBAR As Long, hBarWin As Long, Optional szText As String) As Boolean
Dim r As Long '返回值
Dim bar(0 To 1) As Long '分栏的各项位置
Dim m As Long '分栏的数目
'定义数组
bar(0) = 135 '第一栏宽度为135
bar(1) = -1 '-1表示后面的分为一栏
szText = "状态信息"
r = CreateStatusWindow(WS_CHILD Or WS_VISIBLE, ByVal szText, ParenthWnd, IDC_STATBAR) '创建状态栏
m = 2
If r = 0 Then '如果创建失败则退出过程
CreateStatBar = False
Exit Function
End If
hBarWin = r '返回状态栏的句柄
If m > 1 Then '因为默认就是分一栏所以,这里判断为大于1就是分栏
SendMessage hBarWin, SB_SETPARTS, m, 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

Sub SetBarText(hBar As Long, m As Long, strText As String)  '子过程定义
SendMessage hBar, SB_SETTEXTA, m, ByVal strText '在指定栏上显示信息
     'hBar 为状态栏的句柄
     'm 指定要在哪一栏显示信息,从0开始计,也就是说,如果分两栏,我们要在第二栏里显示信息,m就设置为1
     'szText 要显示的信息
End Sub

 相关源码:VB6.0 用API创建状态栏

CopyRight 2018© 开源爱好者 All Rights Reserved 豫ICP备16030607号 隐私声明 网站地图