网络大杂烩首页

网络大杂烩

  • 网站首页
  • HTML/CSS
  • javascript
  • seo优化
  • PS美工
  • 系统安全
  • 漏洞预警
  • 编程学习
  • 原创专区
  • 【网络大杂烩】是一家综合技术门户,为您提供网站建设,编程开发,安全维护,漏洞预警等技术文章

    技术文章MAP

    文章

    vb基础知识之进度条控件与使用方法

    日期:2019/12/9 14:22:05来源分类:编程开发

    进度条如何放到状态栏(statusbar)

    很简单:用SetParent()将progressBar作为StatusBar的子
    procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;
      Panel: TStatusPanel; const Rect: TRect);
    begin
      Gauge1.Parent:=StatusBar1;
      Gauge1.Left:=Rect.Left;
      Gauge1.Top:=Rect.Top;
      Gauge1.Height:=Rect.Bottom-Rect.Top;
      Gauge1.Visible :=false;
    end;
    注:还需把Panels(2)的style属性设置为psOwnerDraw
    把gauge1替换为你的progressbar即可


    Private Sub ShowProgressInStatusBar(ByVal bShowProgressBar As Boolean)

        Dim tRC As RECT
        
        If bShowProgressBar Then
    '
    ' Get the size of the Panel (2) Rectangle from the status bar
    ' remember that Indexes in the API are always 0 based (well,
    ' nearly always) - therefore Panel(2) = Panel(1) to the api
    '
    '
            SendMessageAny StatusBar1.hwnd, SB_GETRECT, 1, tRC
    '
    ' and convert it to twips....
    '
            With tRC
                .Top = (.Top * Screen.TwipsPerPixelY)
                .Left = (.Left * Screen.TwipsPerPixelX)
                .Bottom = (.Bottom * Screen.TwipsPerPixelY) - .Top
                .Right = (.Right * Screen.TwipsPerPixelX) - .Left
            End With
    '
    ' Now Reparent the ProgressBar to the statusbar
    '
            With prgBar
                SetParent .hwnd, StatusBar1.hwnd
                .Move tRC.Left, tRC.Top, tRC.Right, tRC.Bottom
                .Visible = True
                .Value = 1
            End With
            
        Else
    '
    ' Reparent the progress bar back to the form and hide it
    '
            SetParent prgBar.hwnd, Me.hwnd
            prgBar.Visible = False
        End If

    End Sub

      这个就可以用了! 需要一个Form\Timer\CommandButton\ProgressBar\StatusBar

    Option Explicit


    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    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 Const WM_USER = &H400
    Private Const SB_GETRECT = (WM_USER + 10)
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type

    Private Sub Command1_Click()
        
        ShowProgressInStatusBar True
        Timer1.Enabled = True
        
        
    End Sub
    Private Sub ShowProgressInStatusBar(ByVal bShowProgressBar As Boolean)

        Dim tRC As RECT
        
        If bShowProgressBar Then
        
            SendMessage StatusBar1.hwnd, SB_GETRECT, 1, tRC
            With tRC
                .Top = (.Top * Screen.TwipsPerPixelY) - 5
                .Left = (.Left * Screen.TwipsPerPixelX)
                .Bottom = (.Bottom * Screen.TwipsPerPixelY) - .Top - 10
                .Right = (.Right * Screen.TwipsPerPixelX) - .Left
            End With
            
            With ProgressBar1
                SetParent .hwnd, StatusBar1.hwnd
                .Move tRC.Left, tRC.Top, tRC.Right, tRC.Bottom
                .Visible = True
                .Value = 1
            End With
            
        Else
            SetParent ProgressBar1.hwnd, Me.hwnd
            ProgressBar1.Visible = False
        End If

    End Sub

    Private Sub Form_Load()
        
        With Timer1
            .Enabled = False
            .Interval = 10
        End With
        
    End Sub

    Private Sub Timer1_Timer()
        
        If ProgressBar1.Value = 100 Then
            Timer1.Enabled = False
            ShowProgressInStatusBar False
        Else
            ProgressBar1.Value = ProgressBar1.Value + 1
        End If
    End Sub
    用VB实现状态栏上的进度条
      “进度条”是非常有用的Windows控件之一,它可以非常直观地显示程序运行的进度。而ACDSee、Winzip等软件还能够在状态栏上显示进度条,真是巧夺天工。要是在自已的程序中加上这种功能就好了!几经琢磨,我终于有所收获,现在写出来与大家交流交流。
       程序基本思路是这样的:先获取状态栏上想放置进度的那一栏的位置信息,然后改变进度条控件的容器,即从窗体改变为状态栏。要实现以上功能需用到两个Windows API函数:SendMessage和SetParent。SendMessage函数可以将一条消息发以某个窗口,除非消息处理完毕,否则该函数不会返回;SetParent用以指定一个控件的父控件作为它的容器,在VB里使用这个函数,可将控件从一个容器移至窗体中的另一个容器。需要注意的是,在关闭那个控件之前,需要再次调用SetParent将控件的父控件更改回来。另外,将控件移至另一个父控件后,它的位置将由新的坐标系统决定,所以,有必要重新规定控件的位置。
       此外,进度条控件和状态栏控件属于ActiveX控件,如你在控件工具箱里找不到这两个控件的话,你可以右键单击控件工具箱空白处,在弹出菜单中选择“部件”,然后在控件页面中选择Microsoft Windows Common Controls 6.0, 确定即可。现在你的控件工具箱应该和图1一样了吧?
       具体步骤如下:
    1. 新建一个VB工程,修改窗体的ScaleMode属性为3-Pixel。
    2. 添加一个定时器控件,用来模拟一项操作,以控制进度条的显示。
    3. 添加一个进度条控件。
    4. 添加一个状态栏控件,然后在Panels属性页把状态栏设置为2个Panels,其中Panels(1)用来显示时间,设置它的Style属性为5-sbrTime Panels(2)用来包容进度条,设置它的AutoSize 属性为1-sbrSpring。
    5. 添加一个命令按钮,Caption属性为ShowProgress。
    6. 选择菜单条中的“Project->Add Module”添加一个标准模块,并输入以下的代码:
       Option Explicit
       Public Declare Function SendMessage Lib “user32”Alias“SendMessageA”(ByVal hwnd As Long, ByVal wParam As Long, Iparam As Any)As Long Public Declare Function SetParent Lib “use32” (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

     Public Type RECT
         Left As Long 
         Top As Long 
         Roght As Long 
         Bottom As Long 
     End Type

     Public Const SB_GETRECT As Long = &H40A
    7. 添加如下的窗体代码:
    Option Explicit
    Private Sub Command1_Click()
      Command1.Enabled = False

      ProgressBar1.Min = 0
      ProgressBar1.Min = 100
     ‘设置进度条的最大和最小值

    ShowProgresslnStatusBar True
    Timer1 .Enabled = True
    ‘启动定时器模拟一项任务
    End Sub

    Private Sub ShowProgresslnStatusBar (ByVal bShowProgress As Boolean)
      Dim tRC As RECT

      If bShowProgress Then
    SendMessage StatusBar1.hwnd, SB_GETRECT,1, tRC
    ‘状态栏的第一栏为0,第二栏为1 
    With ProgressBar 1
      SetParent .hwnd, StatusBar1 .hwnd
      .Move tRC.Left, tRC. Top, tRC.Right – tRC.Left. tRC.Bottom – tRC.Top     ‘移动进度条到状态栏
      .Visible = True
      .Value = 0
    End With
    Else
      Setparent progressbar1 .hwnd, Me.hwnd
      ProgressBar1,Visible =false
    End if
    End sub

    Private sub form_load()
      ProgressBar1. visible = false
      Timer1.enabled=false
      Timer1.interval=20
    End sub

    Private sub form_Unload(Cancel As integer)
      Showprogressinstatusbar false
      ‘把进度条控件的容器还原为窗体
    end sub

    private sub timer1_timer()
      static inum as long
      inum =inum+5
    if inum>100 then
      timer1.enabled=false
    showprogressinstatusbar false
      command1.enable=true
    inum=0
    end if 
    progressbar1.value=inum
    end sub

    随机推荐

    • 该分类还没有添加任何内容!
    • 该分类还没有添加任何内容!

    Copyright 2005-2019 【网络大杂烩】 版权所有 黑ICP备16886888号

    声明:本站所有文章来自互联网 如有异议 请联系本站管理员