天天看點

★★★敬請留意★★★:VB使用的幾個技巧

把 VB 标準的工具欄變成平面式  

平面式的工具欄好象顯得很酷!但 VB5 隻提供了普通的凸起的工具欄。你是否想把它變成平面的?這似乎很不容易。但事實并非如此,試試:

BAS:

Public Const WM_USER = &H400

Public Const TB_SETSTYLE = WM_USER + 56

Public Const TB_GETSTYLE = WM_USER + 57

Public Const TBSTYLE_FLAT = &H800

Public Declare Function SendMessageLong Lib "user32" Alias "SendMessageA" _

  (ByVal hwnd As Long, _

   ByVal wMsg As Long, _

   ByVal wParam As Long, _

   ByVal lParam As Long) As Long

Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _

  (ByVal hWnd1 As Long, _

   ByVal hWnd2 As Long, _

   ByVal lpsz1 As String, _

   ByVal lpsz2 As String) As Long

SUB:

Private Sub MakeFlat()

   Dim style As Long

   Dim hToolbar As Long

   Dim r As Long

   hToolbar = FindWindowEx(Toolbar1.hwnd, 0&, "ToolbarWindow32", vbNullString)

   style = SendMessageLong(hToolbar, TB_GETSTYLE, 0&, 0&)

   If style And TBSTYLE_FLAT Then

         style = style Xor TBSTYLE_FLAT

   Else: style = style Or TBSTYLE_FLAT

   End If

   r = SendMessageLong(hToolbar, TB_SETSTYLE, 0, style)

   Toolbar1.Refresh

End Sub

注意:需要 4.70 或其以上版本的 comctl32.dll 支援。

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

在 Caption 中顯示 & 符号  

大家知道,& 符号是 Windows 的快捷鍵表示符号,如果要在 Caption 中顯示 & ,方法很簡單,連續輸入兩個 & 符号即可。如在 Caption 中輸入 Save && Exit,則顯示 Save & Exit。

[傳回技巧索引]

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

讓視窗一直在上面  

很多流行軟體都有這樣一個選項:Always on Top。它可以讓視窗在最上面,别的視窗不能覆寫它。我們在 VB 中,可以使用下面的方法來實作:

Private Const SWP_NOSIZE = &H1

Private Const SWP_NOMOVE = &H2

Private Const SWP_NOZORDER = &H4

Private Const SWP_NOREDRAW = &H8

Private Const SWP_NOACTIVATE = &H10

Private Const SWP_FRAMECHANGED = &H20

Private Const SWP_SHOWWINDOW = &H40

Private Const SWP_NOCOPYBITS = &H80

Private Const SWP_NOOWNERZORDER = &H200

Private Const SWP_DRAWFRAME = SWP_FRAMECHANGED

Private Const SWP_NOREPOSITION = SWP_NOOWNERZORDER   Private Const HWND_TOP = 0

Private Const HWND_BOTTOM = 1

Private Const HWND_TOPMOST = -1

Private Const HWND_NOTOPMOST = -2  

Private Declare Function SetWindowPos Lib "user32" ( _

              ByVal hwnd As Long, _

              ByVal hWndInsertAfter As Long, _

              ByVal X As Long, _

              ByVal Y As Long, _

              ByVal cx As Long, _

              ByVal cy As Long, _

              ByVal wFlags As Long) As Long

Private mbOnTop As Boolean

  Private Property Let OnTop(Setting As Boolean)

    if Setting Then

         SetWindowPos hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE

      Else

         SetWindowPos hwnd, -2, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE

      End If

      mbOnTop = Setting

End Property  

Private Property Get OnTop() As Boolean

     'Return the private variable set in Property Let

     OnTop = mbOnTop

End Property  

調用 OnTop=True 即可讓視窗 Always OnTop。

此技巧由 eaboy 提供。

[傳回技巧索引]

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

播放資源檔案檔案中的聲音  

VB 提供的方法使我們可以很容易地使用資源檔案中的字元、圖檔等資源。我們可以用以下方法播放資源檔案中的 wav 聲音:

首先,在你的資源檔案的源檔案 (RC) 檔案加入下面一行:

MySound WAVE c:/music/vanhalen.wav

然後将其編譯為 RES 檔案。最後使用下面的聲明及代碼:

Private Declare Function PlaySound Lib _ "winmm.dll" Alias "PlaySoundA" ( _ ByVal lpszName As String, _ ByVal hModule As Long, _ ByVal dwFlags As Long) As Long

Private Const SND_ASYNC& = &H1

Private Const SND_NODEFAULT& = &H2

Private Const SND_RESOURCE& = &H40004

Dim hInst As Long

Dim sSoundName As String

Dim lFlags As Long

Dim lRet As Long

Private Sub Command1_Click()

    hInst = App.hInstance

    sSoundName = "MySound"

    lFlags = SND_RESOURCE + SND_ASYNC + _ SND_NODEFAULT

    lRet = PlaySound(sSoundName, hInst, lFlags)

End Sub

[傳回技巧索引]

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

使用枚舉變量  

VB5 引入枚舉變量,使用它,我們可以顯著地改變應用程式的易讀性:

Public Enum TimeOfDay

    Morning = 0

    Afternoon = 1

    Evening = 2

End Enum

Sub Main()

    Dim RightNow As TimeOfDay

    If Time >= #12:00:00 AM# And Time < #12:00:00 PM# Then

        RightNow = Morning

    ElseIf Time >= #12:00:00 PM# And Time < #6:00:00 PM# Then

        RightNow = Afternoon

    ElseIf Time >= #6:00:00 PM# Then

        RightNow = Evening

    End If

End Sub

[傳回技巧索引]

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

動态改變螢幕設定  

我們經常看到許多 Win95 的應用程式(尤其是遊戲)在運作它的時候改變螢幕的設定,運作完後恢複,在 VB 中,我們可以用以下方法實作:

'- 定義

Private Declare Function lstrcpy _

    Lib "kernel32" Alias "lstrcpyA" _

    (lpString1 As Any, lpString2 As Any) _

    As Long

Const CCHDEVICENAME = 32

Const CCHFORMNAME = 32

Private Type DEVMODE

    dmDeviceName As String * CCHDEVICENAME

    dmSpecVersion As Integer

    dmDriverVersion As Integer

    dmSize As Integer

    dmDriverExtra As Integer

    dmFields As Long

    dmOrientation As Integer

    dmPaperSize As Integer

    dmPaperLength As Integer

    dmPaperWidth As Integer

    dmScale As Integer

    dmCopies As Integer

    dmDefaultSource As Integer

    dmPrintQuality As Integer

    dmColor As Integer

    dmDuplex As Integer

    dmYResolution As Integer

    dmTTOption As Integer

    dmCollate As Integer

    dmFormName As String * CCHFORMNAME

    dmUnusedPadding As Integer

    dmBitsPerPel As Integer

    dmPelsWidth As Long

    dmPelsHeight As Long

    dmDisplayFlags As Long

    dmDisplayFrequency As Long

End Type

Private Declare Function _

    ChangeDisplaySettings Lib _

    "User32" Alias "ChangeDisplaySettingsA" (_

    ByVal lpDevMode As Long, _

    ByVal dwflags As Long) As Long

'- 函數

Public Function SetDisplayMode(Width As _

    Integer,Height As Integer, Color As _

    Integer) As Long

Const DM_PELSWIDTH = &H80000

Const DM_PELSHEIGHT = &H100000

Const DM_BITSPERPEL = &H40000

Dim NewDevMode As DEVMODE

Dim pDevmode As Long

With NewDevMode

    .dmSize = 122

    If Color = -1 Then

        .dmFields = DM_PELSWIDTH Or DM_PELSHEIGHT

    Else

        .dmFields = DM_PELSWIDTH Or _

            DM_PELSHEIGHT Or DM_BITSPERPEL

    End If

    .dmPelsWidth = Width

    .dmPelsHeight = Height

    If Color <> -1 Then

        .dmBitsPerPel = Color

    End If

End With

pDevmode = lstrcpy(NewDevMode, NewDevMode)

SetDisplayMode = ChangeDisplaySettings(pDevmode, 0)

End Function

例子調用:改變為 640x480x24位:

i = SetDisplayMode(640, 480, 24)

如果成功傳回 0 。

[傳回技巧索引]

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

移動沒有标題欄的視窗  

我們一般是用滑鼠按住視窗的标題欄,然後移動視窗,當視窗沒有标題欄時,我們可以用下面的方法來移動視窗:

在 BAS 檔案中聲明:

Declare Function ReleaseCapture Lib "user32" () As Long

Declare Function SendMessage Lib "user32" _

Alias "SendMessageA" ( _

ByVal hwnd As Long, ByVal wMsg As Long, _

ByVal wParam As Long, lParam As Any) As Long

Public Const HTCAPTION = 2

Public Const WM_NCLBUTTONDOWN = &HA1

然後,在 Form_MouseDown 事件中:

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

ReleaseCapture

SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&

End Sub

[傳回技巧索引]

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

快速選擇全部項目  

我們在使用 List 控件時,經常需要全部選擇其中的項目,在項目較少時,我們可以逐項設定 Selected 來選擇全部的項目,但當項目較多時,這樣做就比較費時,其實,我們可以用 API 函數來簡單實作此功能:

Dim nRet As Long

Dim bState as Boolean

bState=True

nRet = SendMessage(lstList.hWnd, LB_SETSEL, bState, -1)

函數聲明:

Public Declare Function SendMessage Lib "User32" Alias "SendMessageA" ( ByVal hWnd As Long, ByVal wMsg As Integer, ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const WM_USER = &H400

Public Const LB_SETSEL = (WM_USER + 6)

[傳回技巧索引]

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

真正删除資料庫的記錄  

大家知道,預設情況下,VB 删除記錄隻是把記錄作上個删除标志而已,并沒有真正删除。要真正删除記錄,你可以使用 VB 提供的以下方法:BeginTrans、CommitTrans、RollBack。其中,BeginTrans 方法開始記錄資料庫的變動,CommitTrans 方法确認資料庫的變動,而 RollBack 方法則可以恢複被删除或修改的記錄。它們可以嵌套使用。是以,要恢複被删除的記錄,應該在使用 BeginTrans 方法之後及使用 CommiTrans 方法之前使用 RollBack 方法。

[傳回技巧索引]

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

顯示動畫滑鼠圖示  

Win95 的動畫滑鼠為應用程式增色不少,而 VB 則隻提供一般的滑鼠圖示支援。要用 VB 顯示動畫滑鼠形狀,你可以使用以下方法:

函數聲明:

Public Const GCL_HCURSOR = -12

Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long

Declare Function DestroyCursor Lib "user32" (ByVal hCursor As Any) As Long

Declare Function LoadCursorFromFile Lib "user32" Alias "LoadCursorFromFileA" (ByVal lpFileName As String) As Long

Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Declare Function GetClassLong Lib "user32" Alias "GetClassLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

使用:

Dim mhBaseCursor As Long, mhAniCursor As Long

Dim lResult As Long

mhAniCursor = LoadCursorFromFile("c:/windows/cursors/appstart.ani")

lResult = SetClassLong((hwnd), GCL_HCURSOR, mhAniCursor)