芬琳漆和立邦漆那个好:VB入门技巧N例(6)

来源:百度文库 编辑:九乡新闻网 时间:2024/07/07 15:57:18
16. 半透明窗体
  1. Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, _ ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
  2. Private Const WS_EX_LAYERED = &H80000
  3. Private Const LWA_ALPHA = &H2
  4. Private Const GWL_EXSTYLE = (-20)
  5. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal _
  6. hwnd As Long, ByVal nIndex As Long) As Long
  7. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal _
  8. hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  9. Private Sub Form_Load()
  10.    Dim rtn As Long
  11.    rtn = GetWindowLong(Me.hwnd, GWL_EXSTYLE)  '取的窗口原先的样式
  12.    rtn = rtn Or WS_EX_LAYERED    ' 使窗体添加上新的样式WS_EX_LAYERED
  13.    SetWindowLong Me.hwnd, GWL_EXSTYLE, rtn   ' 把新的样式赋给窗体
  14.    SetLayeredWindowAttributes Me.hwnd, 0, 200, LWA_ALPHA
  15. End Sub
复制代码
17.开机启动(函数及常数声明略)
  1. Private Sub Form_Load()
  2.    Dim hKey As Long, SubKey As String, Exe As String
  3.    SubKey = "Software\Microsoft\Windows\CurrentVersion\Run"
  4.    Exe = "可执行文件的路径"   
  5.    RegCreateKey HKEY_CURRENT_USER, SubKey, hKey
  6.    RegSetvalueEx hKey, "autorun", 0, REG_SZ, ByVal Exe,LenB(StrConv(Exe, vbFromUnicode)) + 1
  7.    RegCloseKey hKey
  8. End Sub
复制代码
18.关闭显示器
  1. Private Declare Function SendMessage Lib "user32" Alias  "SendMessageA" (ByVal hwnd _
  2. As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  3. Const WM_SYSCOMMAND = &H112&
  4. Const SC_MONITORPOWER = &HF170&
  5. Private Sub Command1_Click()
  6.     SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal 2& '关闭显示器
  7. End Sub
  8. Private Sub Command2_Click()
  9.     SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, ByVal -1& '打开显示器
  10. End Sub
复制代码
19. 在程序结束时自动关闭由SHELL打开的程序。
  1. Private Const PROCESS_QUERY_INFORMATION = &H400  '关闭由SHELL函数打开的文件
  2. Private Const PROCESS_TERMINATE = &H1
  3. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _ ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
  4. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
  5. ByVal uExitCode As Long) As Long
  6. Dim ProcessId As Long
  7. Private Sub Command1_Click()
  8.     ProcessId = Shell("notepad.exe.", vbNormalFocus)
  9. End Sub
  10. Private Sub Form_Unload(Cancel As Integer)
  11.     Dim hProcess  As Long
  12.     hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, _ ProcessId)
  13.     Call TerminateProcess(hProcess, 3838)
  14. End Sub
复制代码
20. 关闭、重启计算机
  1. Public Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal _
  2. uFlags As Long, ByVal dwReserved As Long) As Long
  3. ExitWindowsEx 1,0 关机
  4. ExitWindowsEx 0,1 重新启动
复制代码

21.显示关机提示框
  1. Private Declare Function SHRestartSystemMB Lib "shell32" Alias "#59" (ByVal hOwner _
  2. As Long, ByVal sExtraPrompt As String,  

  3. ByVal uFlags As Long) As Long
  4. Const EWX_LOGOFF = 0
  5. Const EWX_SHUTDOWN = 1
  6. Const EWX_REBOOT = 2
  7. Const EWX_FORCE = 4
  8. Const EWX_POWEROFF = 8
  9. Private Sub Command1_Click()
  10. SHRestartSystemMB Me.hWnd, PROMPT, EWX_LOGOFF
  11. End Sub
复制代码