?? 27.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 + -