怎么在VB的状态栏中加如进度条?

gxmark 2003-06-29 10:39:15
如题!
...全文
119 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gpo2002 2003-06-29
  • 打赏
  • 举报
回复
Add a progress bar (ProgressBar1), statusbar (StatusBar1), and two command buttons (Command1, Command2) to the form along with the following code:

--------------------------------------------------------------------------------

Option Explicit

Private defProgBarHwnd As Long

Private Declare Function SetParent Lib "user32" _
(ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long

'used to change progressbar colour
Private Const WM_USER = &H400
Private Const CCM_FIRST As Long = &H2000&
Private Const CCM_SETBKCOLOR As Long = (CCM_FIRST + 1)

'set progressbar backcolor in IE3 or later
Private Const PBM_SETBKCOLOR As Long = CCM_SETBKCOLOR

'set progressbar barcolor in IE4 or later
Private Const PBM_SETBARCOLOR As Long = (WM_USER + 9)

Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long



Private Sub Form_Load()

Dim pnl As Panel
Dim btn As Button
Dim x As Long

'create statusbar
With StatusBar1
For x = 1 To 3
Set pnl = .Panels.Add(, , "", sbrText)
pnl.Alignment = sbrLeft
pnl.Width = 1800
pnl.Bevel = sbrInset
If x = 3 Then pnl.AutoSize = sbrSpring
If x = 1 Then pnl.Text = "Status/Progbar Demo"
Next
End With

Command1.Caption = "Set Progbar"
Command2.Caption = "Run Progbar"

With ProgressBar1
.Min = 0
.Max = 10000
.Value = .Max
End With

End Sub


Private Sub Form_Unload(Cancel As Integer)

If defProgBarHwnd <> 0 Then
SetParent ProgressBar1.hwnd, defProgBarHwnd
End If

End Sub


Private Sub Command1_Click()

Dim pading As Long


'parent the progress bar in the status bar
pading = 40
AttachProgBar ProgressBar1, StatusBar1, 2, pading

'change the bar colour
Call SendMessage(ProgressBar1.hwnd, _
PBM_SETBARCOLOR, _
0&, _
ByVal RGB(205, 0, 205))

ProgressBar1.Value = 0

End Sub


Private Sub Command2_Click()

Dim cnt As Long
Dim tmp As String

tmp = StatusBar1.Panels(1).Text
StatusBar1.Panels(1).Text = "Processing ..."

For cnt = 1 To ProgressBar1.Max

ProgressBar1.Value = cnt

'needed to trap cancel click
DoEvents

Next

StatusBar1.Panels(1).Text = tmp
ProgressBar1.Value = 0

End Sub


Private Function AttachProgBar(pb As ProgressBar, _
sb As StatusBar, _
nPanel As Long, _
pading As Long)

If defProgBarHwnd = 0 Then

'change the parent
defProgBarHwnd = SetParent(pb.hwnd, sb.hwnd)

With sb

'adjust statusbar. Doing it this way
'relieves the necessity of calculating
'the statusbar position relative to the
'top of the form. It happens so fast
'the change is not seen.
.Align = vbAlignTop
.Visible = False

'change, move, set size and re-show
'the progress bar in the new parent
With pb
.Visible = False
.Align = vbAlignNone
.Appearance = ccFlat
.BorderStyle = ccNone
.Width = sb.Panels(nPanel).Width
.Move (sb.Panels(nPanel).Left + pading), _
(sb.Top + pading), _
(sb.Panels(nPanel).Width - (pading * 2)), _
(sb.Height - (pading))

.Visible = True
.ZOrder 0
End With

'restore the statusbar to the
'bottom of the form and show
.Panels(nPanel).AutoSize = sbrNoAutoSize
.Align = vbAlignBottom
.Visible = True

End With

End If

End Function

gpo2002 2003-06-29
  • 打赏
  • 举报
回复
SetParent
The SetParent function changes the parent window of the specified child window.

HWND SetParent(
HWND hWndChild, // handle to window
HWND hWndNewParent // new parent window
);
Parameters
hWndChild
[in] Handle to the child window.
hWndNewParent
[in] Handle to the new parent window. If this parameter is NULL, the desktop window becomes the new parent window.
Windows 2000/XP: If this parameter is HWND_MESSAGE, the child window becomes a message-only window.

Return Values
If the function succeeds, the return value is a handle to the previous parent window.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧