★★★敬请留意★★★:VB使用的几个技巧

原创 2001年05月24日 15:46:00

 

把 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)

Android Studio 小技巧合集

写在前面 本文翻译自 Android Studio Tips by Philippe Breault,一共收集了62个 Android Studio 使用小技巧和快捷键。 根据这些小技巧的使...
  • jdsjlzx
  • jdsjlzx
  • 2016年02月18日 22:38
  • 7697

掌握PL/SQL小技巧,提高工作效率

用分号隔开不同的SQL语句 在同一窗口输入,可以在一次执行,PL/SQL将自动新建SQL窗口,分别执行不同的SQL语句(PL/SQL 7.1版本有次功能,低版本的可能会提示出错) 用&符号引入...
  • piaoxuan1987
  • piaoxuan1987
  • 2013年02月01日 19:16
  • 1390

正确使用日志的10个技巧

做一个苦逼的Java攻城师, 我们除了关心系统的架构这种high level的问题, 还需要了解一些语言的陷阱, 异常的处理, 以及日志的输出, 这些"鸡毛蒜皮"的细节. 这篇文章是JCP成员, To...
  • kobejayandy
  • kobejayandy
  • 2013年12月15日 16:41
  • 4048

《Excel专家案例与技巧金典》★源文件

  • 2009年08月06日 20:32
  • 887KB
  • 下载

★10 个实用技巧,让Finder带你飞~

个实用技巧,让Finder带你飞~" title="★10 个实用技巧,让Finder带你飞~"> ...
  • github_38885296
  • github_38885296
  • 2018年01月05日 11:05
  • 33

★如何引导客户需求?几个经典的案例分析!

如何引导客户需求?几个经典的案例分析! (第三个医患案例仅作为启发,不倡导医生为之,在此声明)        导读:客户的需求往往是多方面的、不确定的,需要去分析和引导。客户的需求是指通...
  • github_38885296
  • github_38885296
  • 2018年01月05日 11:02
  • 53

★春林文件批量改名系统7.0.3

  • 2014年10月28日 13:02
  • 600KB
  • 下载

★ Jblend 可自动联机游戏 自动开启蓝牙听歌

  • 2010年04月28日 09:14
  • 2.27MB
  • 下载

★C51语言配套程序

  • 2010年08月08日 20:05
  • 523KB
  • 下载

★PowerQuest PartitionMagic 8.0 (Build 1242)简装汉化版

  • 2008年08月26日 16:20
  • 6.17MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:★★★敬请留意★★★:VB使用的几个技巧
举报原因:
原因补充:

(最多只允许输入30个字)