收藏 分享(赏)

VB使用的几个技巧.doc

上传人:11xg27ws 文档编号:8729253 上传时间:2019-07-09 格式:DOC 页数:11 大小:38KB
下载 相关 举报
VB使用的几个技巧.doc_第1页
第1页 / 共11页
VB使用的几个技巧.doc_第2页
第2页 / 共11页
VB使用的几个技巧.doc_第3页
第3页 / 共11页
VB使用的几个技巧.doc_第4页
第4页 / 共11页
VB使用的几个技巧.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、VB 使用的几个技巧敬请留意:VB 使用的几个技巧收藏把 VB 标准的工具栏变成平面式 平面式的工具栏好象显得很酷!但 VB5 只提供了普通的凸起的工具栏。你是否想把它变成平面的?这似乎很不容易。但事实并非如此,试试: BAS: Public Const WM_USER = &H400Public Const TB_SETSTYLE = WM_USER + 56Public Const TB_GETSTYLE = WM_USER + 57Public Const TBSTYLE_FLAT = &H800Public Declare Function SendMessageLong Lib “u

2、ser32“ Alias “SendMessageA“ _(ByVal hwnd As Long, _ByVal wMsg As Long, _ByVal wParam As Long, _ByVal lParam As Long) As LongPublic Declare Function FindWindowEx Lib “user32“ Alias “FindWindowExA“ _(ByVal hWnd1 As Long, _ByVal hWnd2 As Long, _ByVal lpsz1 As String, _ByVal lpsz2 As String) As LongSUB:

3、 Private Sub MakeFlat()Dim style As LongDim hToolbar As LongDim r As LonghToolbar = FindWindowEx(Toolbar1.hwnd, 0&, “ToolbarWindow32“, vbNullString)style = SendMessageLong(hToolbar, TB_GETSTYLE, 0&, 0&)If style And TBSTYLE_FLAT Thenstyle = style Xor TBSTYLE_FLATElse: style = style Or TBSTYLE_FLATEnd

4、 Ifr = SendMessageLong(hToolbar, TB_SETSTYLE, 0, style)Toolbar1.RefreshEnd Sub注意:需要 4.70 或其以上版本的 comctl32.dll 支持。-在 Caption 中显示 & 符号 大家知道,& 符号是 Windows 的快捷键表示符号,如果要在 Caption 中显示 & ,方法很简单,连续输入两个 & 符号即可。如在 Caption 中输入 Save & Exit,则显示 Save & Exit。 返回技巧索引 -让窗口一直在上面 很多流行软件都有这样一个选项:Always on Top。它可以让窗口在最上

5、面,别的窗口不能覆盖它。我们在 VB 中,可以使用下面的方法来实现: Private Const SWP_NOSIZE = &H1Private Const SWP_NOMOVE = &H2Private Const SWP_NOZORDER = &H4Private Const SWP_NOREDRAW = &H8Private Const SWP_NOACTIVATE = &H10Private Const SWP_FRAMECHANGED = &H20Private Const SWP_SHOWWINDOW = &H40Private Const SWP_NOCOPYBITS = &H8

6、0Private Const SWP_NOOWNERZORDER = &H200Private Const SWP_DRAWFRAME = SWP_FRAMECHANGEDPrivate Const SWP_NOREPOSITION = SWP_NOOWNERZORDER Private Const HWND_TOP = 0Private Const HWND_BOTTOM = 1Private Const HWND_TOPMOST = -1Private Const HWND_NOTOPMOST = -2 Private Declare Function SetWindowPos Lib “

7、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 LongPrivate mbOnTop As BooleanPrivate Property Let OnTop(Setting As Boolean)if Setting ThenSetWindowPos hwnd, -1, 0, 0, 0, 0, SWP_NOMOVE

8、Or SWP_NOSIZEElseSetWindowPos hwnd, -2, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZEEnd IfmbOnTop = SettingEnd Property Private Property Get OnTop() As BooleanReturn the private variable set in Property LetOnTop = mbOnTopEnd Property 调用 OnTop=True 即可让窗口 Always OnTop。 此技巧由 eaboy 提供。 返回技巧索引 -播放资源文件文件中的声音 VB 提

9、供的方法使我们可以很容易地使用资源文件中的字符、图片等资源。我们可以用以下方法播放资源文件中的 wav 声音: 首先,在你的资源文件的源文件 (RC) 文件加入下面一行: MySound WAVE c:musicvanhalen.wav 然后将其编译为 RES 文件。最后使用下面的声明及代码: Private Declare Function PlaySound Lib _ “winmm.dll“ Alias “PlaySoundA“ ( _ ByVal lpszName As String, _ ByVal hModule As Long, _ ByVal dwFlags As Long)

10、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 +

11、 _ SND_NODEFAULT lRet = PlaySound(sSoundName, hInst, lFlags) End Sub 返回技巧索引 -使用枚举变量 VB5 引入枚举变量,使用它,我们可以显著地改变应用程序的易读性: Public Enum TimeOfDayMorning = 0Afternoon = 1Evening = 2End EnumSub Main()Dim RightNow As TimeOfDayIf Time = #12:00:00 AM# And Time = #12:00:00 PM# And Time = #6:00:00 PM# ThenRightN

12、ow = EveningEnd IfEnd Sub 返回技巧索引 -动态改变屏幕设置 我们经常看到许多 Win95 的应用程序(尤其是游戏)在运行它的时候改变屏幕的设置,运行完后恢复,在 VB 中,我们可以用以下方法实现: - 定义Private Declare Function lstrcpy _Lib “kernel32“ Alias “lstrcpyA“ _(lpString1 As Any, lpString2 As Any) _As LongConst CCHDEVICENAME = 32Const CCHFORMNAME = 32Private Type DEVMODEdmDevi

13、ceName As String * CCHDEVICENAMEdmSpecVersion As IntegerdmDriverVersion As IntegerdmSize As IntegerdmDriverExtra As IntegerdmFields As LongdmOrientation As IntegerdmPaperSize As IntegerdmPaperLength As IntegerdmPaperWidth As IntegerdmScale As IntegerdmCopies As IntegerdmDefaultSource As IntegerdmPri

14、ntQuality As IntegerdmColor As IntegerdmDuplex As IntegerdmYResolution As IntegerdmTTOption As IntegerdmCollate As IntegerdmFormName As String * CCHFORMNAMEdmUnusedPadding As IntegerdmBitsPerPel As IntegerdmPelsWidth As LongdmPelsHeight As LongdmDisplayFlags As LongdmDisplayFrequency As LongEnd Type

15、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 LongConst DM_PELSWIDTH = &H80000Const DM_PELSHEIGHT = &

16、H100000Const DM_BITSPERPEL = &H40000Dim NewDevMode As DEVMODEDim pDevmode As LongWith NewDevMode.dmSize = 122If Color = -1 Then.dmFields = DM_PELSWIDTH Or DM_PELSHEIGHTElse.dmFields = DM_PELSWIDTH Or _DM_PELSHEIGHT Or DM_BITSPERPELEnd If.dmPelsWidth = Width.dmPelsHeight = HeightIf Color -1 Then.dmBi

17、tsPerPel = ColorEnd IfEnd WithpDevmode = lstrcpy(NewDevMode, NewDevMode)SetDisplayMode = ChangeDisplaySettings(pDevmode, 0)End Function例子调用:改变为 640x480x24 位: i = SetDisplayMode(640, 480, 24) 如果成功返回 0 。 返回技巧索引 -移动没有标题栏的窗口 我们一般是用鼠标按住窗口的标题栏,然后移动窗口,当窗口没有标题栏时,我们可以用下面的方法来移动窗口: 在 BAS 文件中声明: Declare Functio

18、n ReleaseCapture Lib “user32“ () As LongDeclare Function SendMessage Lib “user32“ _Alias “SendMessageA“ ( _ByVal hwnd As Long, ByVal wMsg As Long, _ByVal wParam As Long, lParam As Any) As LongPublic Const HTCAPTION = 2Public Const WM_NCLBUTTONDOWN = &HA1 然后,在 Form_MouseDown 事件中: Private Sub Form_Mou

19、seDown(Button As Integer, Shift As Integer, X As Single, Y As Single)ReleaseCaptureSendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&End Sub 返回技巧索引 -快速选择全部项目 我们在使用 List 控件时,经常需要全部选择其中的项目,在项目较少时,我们可以逐项设置 Selected 来选择全部的项目,但当项目较多时,这样做就比较费时,其实,我们可以用 API 函数来简单实现此功能: Dim nRet As Long Dim bState as Boolean

20、 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 LongPublic Const WM_USER = &H400Public Const LB_SETSEL = (WM_US

21、ER + 6) 返回技巧索引 -真正删除数据库的记录 大家知道,缺省情况下,VB 删除记录只是把记录作上个删除标志而已,并没有真正删除。要真正删除记录,你可以使用 VB 提供的以下方法:BeginTrans、CommitTrans、RollBack。其中,BeginTrans 方法开始记录数据库的变动,CommitTrans 方法确认数据库的变动,而 RollBack 方法则可以恢复被删除或修改的记录。它们可以嵌套使用。因此,要恢复被删除的记录,应该在使用 BeginTrans 方法之后及使用 CommiTrans 方法之前使用 RollBack 方法。 返回技巧索引 -显示动画鼠标图标 Wi

22、n95 的动画鼠标为应用程序增色不少,而 VB 则只提供一般的鼠标图标支持。要用 VB 显示动画鼠标形状,你可以使用以下方法: 函数声明: Public Const GCL_HCURSOR = -12Declare Function ClipCursor Lib “user32“ (lpRect As Any) As LongDeclare Function DestroyCursor Lib “user32“ (ByVal hCursor As Any) As LongDeclare Function LoadCursorFromFile Lib “user32“ Alias “LoadCu

23、rsorFromFileA“ (ByVal lpFileName As String) As LongDeclare Function SetClassLong Lib “user32“ Alias “SetClassLongA“ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongDeclare Function GetClassLong Lib “user32“ Alias “GetClassLongA“ (ByVal hwnd As Long, ByVal nIndex As Long) As Long使用: Dim mhBaseCursor As Long, mhAniCursor As LongDim lResult As LongmhAniCursor = LoadCursorFromFile(“c:windowscursorsappstart.ani“)lResult = SetClassLong(hwnd), GCL_HCURSOR, mhAniCursor)本文来自 CSDN 博客,转载请标明出处:http:/

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报