打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
VB入门技巧N例(9)
userphoto

2011.01.31

关注
27.清空回收站

  1. Private Declare Function SHEmptyRecycleBin Lib "shell32.dll" Alias _
  2. "SHEmptyRecycleBinA" (ByVal hwnd As Long, ByVal pszRootPath As String, _
  3. ByVal dwFlags As Long) As Long
  4. Private Declare Function SHUpdateRecycleBinIcon Lib "shell32.dll" () As Long
  5. Private Const SHERB_NOCONFIRMATION = &H1
  6. Private Const SHERB_NOPROGRESSUI = &H2
  7. Private Const SHERB_NOSOUND = &H4
  8. Private Sub Command1_Click()
  9. Dim retval As Long  ' return value
  10.     retval = SHEmptyRecycleBin(RecycleBin.hwnd, "", SHERB_NOPROGRESSUI) ' 清空回收站, 确认
  11.     ' 若有错误出现,则返回回收站图示
  12.         If retval <> 0 Then  ' error
  13.         retval = SHUpdateRecycleBinIcon()
  14.     End If
  15. End Sub
  16. Private Sub Command2_Click()
  17.     Dim retval As Long  ' return value
  18.     ' 清空回收站, 不确认
  19.     retval = SHEmptyRecycleBin(RecycleBin.hwnd, "", SHERB_NOCONFIRMATION)
  20.       ' 若有错误出现,则返回回收站图示
  21.     If retval <> 0 Then  ' error
  22.         retval = SHUpdateRecycleBinIcon()
  23.     End If
  24.     Command1_Click
  25. End Sub
复制代码


28.获得系统文件夹的路径
  1. Private Declare Function GetSystemDirectory Lib "kernel32" Alias _
  2. "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
  3. Private Sub Command1_Click()
  4.    Dim syspath As String
  5.    Dim len5 As Long
  6.    syspath = String(255, 0)
  7.    len5 = GetSystemDirectory(syspath, 256)
  8.    syspath = Left(syspath, InStr(1, syspath, Chr(0)) - 1)
  9.    Debug.Print "System Path : "; syspath
  10. End Sub
复制代码

29.动态增加控件并响应事件
  1. Option Explicit
  2.     '通过使用WithEvents关键字声明一个对象变量为新的命令按钮
  3.     Private WithEvents NewButton As CommandButton
  4. '增加控件
  5.     Private Sub Command1_Click()
  6.      If NewButton Is Nothing Then
  7.      '增加新的按钮cmdNew
  8.      Set NewButton = Controls.Add("VB.CommandButton", "cmdNew", Me)
  9.      '确定新增按钮cmdNew的位置
  10.       NewButton.Move Command1.Left + Command1.Width + 240, Command1.Top
  11.       NewButton.Caption = "新增的按钮"
  12.       NewButton.Visible = True
  13.      End If
  14.     End Sub
  15.     '删除控件(注:只能删除动态增加的控件)
  16.     Private Sub Command2_Click()
  17.      If NewButton Is Nothing Then
  18.       Else
  19.       Controls.Remove NewButton
  20.         Set NewButton = Nothing
  21.        End If
  22.     End Sub
  23.     '新增控件的单击事件
  24.     Private Sub NewButton_Click()
  25.        MsgBox "您选中的是动态增加的按钮!"
  26.     End Sub
复制代码
  
30.得到磁盘序列号
  1. Function GetSerialNumber(strDrive As String) As Long
  2.   Dim SerialNum As Long
  3.   Dim Res As Long
  4.   Dim Temp1 As String
  5.   Dim Temp2 As String
  6.    Temp1 = String$(255, Chr$(0))
  7.    Temp2 = String$(255, Chr$(0))
  8.    Res = GetVolumeInformation(strDrive, Temp1, Len(Temp1), SerialNum, 0, 0, Temp2, _
  9. Len(Temp2))
  10.    GetSerialNumber = SerialNum
  11. End Function
  12. 调用形式   Label1.Caption = GetSerialNumber("c:\")
复制代码


31.打开屏幕保护
  1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd _
  2. As Long, ByVal wMsg As Long, ByVal wParam  

  3. As Long, lParam As Any) As Long
  4. '我们将要调用的那个消息,在MSDN中搜索WM_SYSCOMMAND就可以找到具体说明
  5. Const WM_SYSCOMMAND = &H112
  6. '这个参数指明了我们让系统启动屏幕保护
  7. Const SC_SCREENSAVE = &HF140&
  8. Private Sub Command1_Click()
  9. SendMessage Me.hwnd, WM_SYSCOMMAND, SC_SCREENSAVE, 0
  10. End Sub
复制代码


32.获得本机IP地址
方法一:利用Winsock控件
winsockip.localip
方法二:
  1. Private Const MAX_IP = 255
  2.     Private Type IPINFO
  3.      dwAddr As Long
  4.      dwIndex As Long
  5.      dwMask As Long
  6.      dwBCastAddr As Long
  7.      dwReasmSize As Long
  8.      unused1 As Integer
  9.      unused2 As Integer
  10.     End Type
  11.     Private Type MIB_IPADDRTABLE
  12.      dEntrys As Long
  13.      mIPInfo(MAX_IP) As IPINFO
  14.     End Type
  15.     Private Type IP_Array
  16.      mBuffer As MIB_IPADDRTABLE
  17.      BufferLen As Long
  18.     End Type
  19.     Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination _
  20. As Any, Source As Any, ByVal Length As  

  21. Long)
  22.     Private Declare Function GetIpAddrTable Lib "IPHlpApi" (pIPAdrTable As Byte, _
  23. pdwSize As Long, ByVal Sort As Long) As Long
  24.     Dim strIP As String
  25.     Private Function ConvertAddressToString(longAddr As Long) As String
  26.      Dim myByte(3) As Byte
  27.      Dim Cnt As Long
  28.      CopyMemory myByte(0), longAddr, 4
  29.      For Cnt = 0 To 3
  30.      ConvertAddressToString = ConvertAddressToString + CStr(myByte(Cnt)) + "."
  31.      Next Cnt
  32.      ConvertAddressToString = Left$(ConvertAddressToString, Len(ConvertAddressToString) - 1)
  33.     End Function
  34.       
  35.     Public Sub Start()
  36.      Dim Ret As Long, Tel As Long
  37.      Dim bBytes() As Byte
  38.      Dim Listing As MIB_IPADDRTABLE
  39.      On Error GoTo END1
  40.      GetIpAddrTable ByVal 0&, Ret, True
  41.      If Ret <= 0 Then Exit Sub
  42.      ReDim bBytes(0 To Ret - 1) As Byte
  43.      GetIpAddrTable bBytes(0), Ret, False

  44. CopyMemory Listing.dEntrys, bBytes(0), 4
  45.      strIP = "你机子上有 " & Listing.dEntrys & " 个 IP 地址。" & vbCrLf
  46.      strIP = strIP & "------------------------------------------------" & vbCrLf & vbCrLf
  47.      For Tel = 0 To Listing.dEntrys - 1
  48.      CopyMemory Listing.mIPInfo(Tel), bBytes(4 + (Tel * Len(Listing.mIPInfo(0)))), Len _(Listing.mIPInfo(Tel))
  49.      strIP = strIP & "IP 地址 : " & ConvertAddressToString(Listing.mIPInfo(Tel).dwAddr)  & vbCrLf
  50.      Next
  51.      Exit Sub
  52. END1:
  53.      MsgBox "ERROR"
  54.     End Sub
  55. Private Sub Form_Load()
  56.      Start
  57.      MsgBox strIP
  58. End Sub
复制代码
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
VB关于webbrowser相关操作大全
IE webbrowser技巧集
特殊网页爬虫——VBA开发文档
VB中获取exe自身所在路径的几种方法
VBA常用代码解析(第四十讲)
VB实用代码,收藏!!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服