分享
 
 
 

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

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有