vb常用代码大全
- 格式:doc
- 大小:69.50 KB
- 文档页数:16
- 1 -
移动无标题栏的窗体
dim m(borderstyle=none)
ouseX as integer
dim mouseY as integer
dim moveX as integer
dim moveY as integer
dim down as boolean
form_mousedown: ’mousedown事件
down=true
mouseX=x
mouseY=y
form_mouseup: ’mouseup事件
down=false
form_mousemove
if down=true then
moveX=me。left-mouseX+X
moveY=me.top—mouseY+Y
me.move moveX,moveY
end if
*******************************************闪烁控件
比如要闪烁一个label(标签)
添加一个时钟控件 间隔请根据实际需要设置 enabled属性设为true
代码为:label1。visible=not label1。visible
*******************************************
禁止使用 Alt+F4 关闭窗口
Private Declare Function DeleteMenu Lib
"user32" (ByVal hMenu As Long, ByVal
nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetMenuItemCount Lib ”user32” (ByVal hMenu As Long) As Long
Private Const MF_BYPOSITION = &H400&
Private Sub Form_Load()
Dim hwndMenu As Long
Dim c As Long
hwndMenu = GetSystemMenu(Me。hwnd, 0)
c = GetMenuItemCount(hwndMenu)
DeleteMenu hwndMenu, c - 1, MF_BYPOSITION
c = GetMenuItemCount(hwndMenu)
DeleteMenu hwndMenu, c - 1, MF_BYPOSITION
End Sub
启动控制面板大全
'打开控制面板
Call Shell("rundll32.exe shell32。dll,Control_RunDLL”, 9)
'辅助选项 属性-键盘
Call Shell("rundll32。exe shell32.dll,Control_RunDLL access.cpl,,1”, 9)
'辅助选项 属性—声音
Call Shell("rundll32。exe shell32。dll,Control_RunDLL access.cpl,,2”, 9)
’辅助选项 属性—显示
Call Shell("rundll32.exe shell32.dll,Control_RunDLL access.cpl,,3", 9)
'辅助选项 属性—鼠标
Call Shell(”rundll32.exe shell32.dll,Control_RunDLL access。cpl,,4”, 9)
'辅助选项 属性-常规
Call Shell(”rundll32。exe shell32.dll,Control_RunDLL access。cpl,,5”, 9)
’添加/删除程序 属性-安装/卸载
Call Shell(”rundll32.exe shell32。dll,Control_RunDLL Appwiz。cpl,,1”, 9)
’添加/删除程序 属性-Windows安装程序
Call Shell(”rundll32。exe shell32。dll,Control_RunDLL Appwiz.cpl,,2”, 9)
’添加/删除程序 属性—启动盘
Call Shell("rundll32.exe shell32.dll,Control_RunDLL Appwiz。cpl,,3”, 9)
'显示 属性—背景
Call Shell(”rundll32.exe shell32。dll,Control_RunDLL desk.cpl,,0”, 9)
’显示 属性—屏幕保护程序
Call Shell(”rundll32。exe shell32。dll,Control_RunDLL desk.cpl,,1", 9)
’显示 属性-外观
Call Shell("rundll32.exe shell32.dll,Control_RunDLL desk。cpl,,2", 9) VB常用代码 - 2 -
’显示 属性—设置
Call Shell(”rundll32。exe shell32.dll,Control_RunDLL desk.cpl,,3”, 9)
'Internet 属性-常规
Call Shell("rundll32.exe shell32.dll,Control_RunDLL Inetcpl.cpl,,0”, 9)
’Internet 属性—安全
Call Shell("rundll32。exe shell32。dll,Control_RunDLL Inetcpl。cpl,,1", 9)
'Internet 属性-内容
Call Shell(”rundll32.exe shell32.dll,Control_RunDLL Inetcpl.cpl,,2", 9)
'Internet 属性-连接
Call Shell(”rundll32.exe shell32.dll,Control_RunDLL Inetcpl。cpl,,3”, 9)
*******************************************
怎样关闭一个程序
你可以使用API函数FindWindow和PostMessage来寻找一个窗口并且关闭它。下面的范例演示如何关闭一个标题为”Calculator"的窗口.
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString,
"Calculator")
Debug.Print winHwnd
If winHwnd 〈〉 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "Error posting message。”
End If
Else
MsgBox ”The Calculator is not open。"
End If
For this code to work, you must have declared the API functions in a module in your project. You must put the following in
the declarations section of the module。
Declare Function FindWindow Lib ”user32” Alias _
”FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long Declare Function PostMessage Lib ”user32” Alias _
”PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any)
As Long
Public Const WM_CLOSE = &H10
*******************************************
如何使Form的背景图随Form大小改变
单纯显示图形用Image即可,而且用Image也正好可解决你的问题
设定Image的Stretch=true
在加入以下的code
Private Sub Form_Resize()
Image1。Move 0, 0, ScaleWidth, ScaleHeight
End Sub
或者使用以下的方式来做也可以
Private Sub Form_Paint()
Me。PaintPicture Me.Picture, 0, 0, ScaleWidth, ScaleHeight
End Sub
*******************************************
软件的注册
可用注册表简单地保存已用的天数或次数
'次数限制(如30次)如下:
Private Sub Form_Load()
Dim RemainDay As Long
RemainDay = GetSetting(”MyApp", "set", ”times”, 0)
If RemainDay = 30 Then
MsgBox ”试用次数已满,请注册”
Unload Me
End If
MsgBox "现在剩下:" & 30 - RemainDay & "试用次数,好好珍惜!”
RemainDay = RemainDay + 1
SaveSetting "MyApp", "set", "times”, RemainDay