⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 27.txt

📁 介绍VB里的各种控件的使用方法,窗口控制,图像编程以及OCX等内容,还提供了一个API集供参考.
💻 TXT
字号:
不用OCX来创建自己的控件(一)
让我们以进度条Progress Bar这个通用控件(Common Control)为例。进度条是一个较为简单的Windows通用控件,它为一个操作的时间长短提供了一种图形化的显示方式。要使用该控件 一般是将Comctl32.ocx添加到工程中。Comctl32.ocx是一个超级 OCX,它本身包含了许多其它的控件。 
但是如果你只想用进度条而不想用Comctl32.ocx中的其它控件,通常有两种方法可供选择。一种是用VB的PictureBox或 Label控件做一个假的进度条;另一种是自己做一个进度条。

你可以通过引用Comctl32.dll来自己做一个进度条。该动态链接库包含所有的common controls的功能。用Comctl32.dll来创建进度条或其它的common control,控件的定义(它的window风格, API函数,数据结构以及window消息)必须包含进去。common control的定义被储存在Comctl32.dll的头文件Commctrl.h中,你可以用Visual C++读取这些信息或者到微软网站中的某个地方找到。找到这些定义.将其转换为VB风格的声明并应用它们,就是本文所要讲的东西。

下面的例子向你讲述如何在不利用Comctl32.ocx的情况下创建自己的进度条。将下面的代码分别粘贴到工程模块和窗体模块中,为完成该程序,你还需要在你的工程窗体中添加一个命令按钮,名为“Command1”和一个标签控件,名为“Label1”. 
Desclare

Declare Sub InitCommonControls Lib "comctl32.dll" ()

Public Const PROGRESS_CLASS = "msctls_progress32"

Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long

' Styles
' The PBS_SMOOTH & PBS_VERTICAL progress bar window styles
' are defined in Comctl32.dll that comes w/ IE3 and later (v4.70).

Public Const PBS_SMOOTH = &H1
Public Const PBS_VERTICAL = &H4
Public Const WS_VISIBLE = &H10000000
Public Const WS_CHILD = &H40000000

Declare Function IsWindow Lib "user32" (ByVal hWnd As Long) As Long

Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long

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

' Messages
Public Const WM_USER = &H400

' The PBM_SETRANGE message sets the minimum and maximum values
' for a progress bar and redraws the bar to reflect the new range.
' wParam = 0
' lParam = nMinRange Or (nMaxRange * &H10000)
' nMinRange: Minimum range value. By default, the minimum value is zero.
' nMaxRange: Maximum range value. By default, the maximum value is 100.
' Returns the previous range values if successful, or zero otherwise. The
' low-order word specifies the previous minimum value, and the high-order
' word specifies the previous maximum value.

Public Const PBM_SETRANGE = (WM_USER + 1)

' The PBM_SETSTEP message specifies the step increment for a progress bar.
' The step increment is the amount by which the progress bar increases its
' current position whenever it receives a PBM_STEPIT message. By default, the
' step increment is set to 10.
' wParam = New step increment.
' lParam = 0
' Returns the previous step increment.

Public Const PBM_SETSTEP = (WM_USER + 4)

' The PBM_STEPIT message advances the current position for a progress bar by
' the step increment and redraws the bar to reflect the new position. An application
' sets the step increment by sending the PBM_SETSTEP message.
' wParam = 0
' lParam = 0
' Returns the previous position.
' When the position exceeds the maximum range value, this message resets the
' current position so that the progress indicator starts over again from the beginning.

Public Const PBM_STEPIT = (WM_USER + 5)

' The five remaining progrss bar constants not included here (as well as all of the
' other common control definitions) can be found in the file Commctrl.h at:
' http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/headers/c35.htm
' (this URL was current as of 5/20/97)
' End Bas module declares

Code

' Begin Form module code:

Private Sub Form_Load()

    ScaleMode = vbPixels

    Command1.Caption = "Do stuff..."

End Sub

Private Sub Command1_Click()

' Dynamically creates a 15 pixel wide vertical progress bar and
' places it at the right of the form, does stuff that takes a while &
' shows the progress, then destroys the progress bar.

Dim hProgBar As Long
Dim dwIncrement As Long
Dim dwIdx As Long
Dim vDummy As Variant
Static bRunning As Boolean

On Error GoTo Out

    ' Allows this proc to cancel itself
    If bRunning Then bRunning = False: Exit Sub

    ' Make sure the library is loaded
    InitCommonControls

    ' Create the progress bar using the standard window styles
    ' and the two new IE3 progress bar styles, smooth & vertical.
    ' Addition standard or extended window styles can be specified
    ' to alter the default appearance of the progress bar.

    hProgBar = CreateWindowEx(0, PROGRESS_CLASS, vbNullString, WS_CHILD Or WS_VISIBLE Or     PBS_SMOOTH Or PBS_VERTICAL, ScaleWidth - 15, 0, 15, ScaleHeight, hWnd, 0, App.hInstance, ByVal 0)

    If hProgBar = 0 Then MsgBox "Uh oh...": Exit Sub

    ' Here we go...

    bRunning = True

    Command1.Caption = "Cancel"

    ' Set the range of the progess bar.

    ' 0 (lParam low word) to &H4FFF (lParam high word).

    SendMessage hProgBar, PBM_SETRANGE, 0, ByVal (&H4FFF * &H10000)

    ' Set the value of the highlight increment. We''ll go w/ 100 itins here.
    ' The width and number of progress blocks are determined by the
    ' progress bar's width & height.

    dwIncrement = &H4FFF \ 100

    SendMessage hProgBar, PBM_SETSTEP, ByVal dwIncrement, 0

    ' Let's do some stuff...

    For dwIdx = 1 To &H4FFF

        DoEvents

        If Not bRunning Then Exit For

        vDummy = vDummy & Format(Chr(Asc(Trim(Left("a", InStr(1, "a", "a", 1)))))) '\___/'

        If dwIdx Mod dwIncrement = 0 Then

            ' Advance the current position of the progress bar by the step increment.

            SendMessage hProgBar, PBM_STEPIT, 0, 0

            Label1 = dwIdx \ dwIncrement & "%"

        End If

    Next

Out:

    ' Frees all resources associated with the progress bar.

    If IsWindow(hProgBar) Then DestroyWindow hProgBar

        bRunning = False

    Command1.Caption = "Do stuff..."

    Label1 = ""

End Sub

' End Form module code

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -