Author Topic: 微調 Windows 桌面的程序函數庫  (Read 8301 times)

admin

  • Administrator
  • *****
  • Posts: 0
    • View Profile
微調 Windows 桌面的程序函數庫
« on: October 18, 2010, 02:38:41 AM »
Code: [Select]
Attribute VB_Name = "modDesktop"
Option Explicit
Public gintHideTaskbar As Integer
Public gintHideMouseCursor As Integer

Private Const SWP_HIDEWINDOW = &H80  '*** TaskBar
Private Const SWP_SHOWWINDOW = &H40  '*** TaskBar

Private Const WM_CLOSE = &H10
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const flags = SWP_NOMOVE Or SWP_NOSIZE

Private Const ABM_GETTASKBARPOS = &H5

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Private Type APPBARDATA
    cbSize As Long
    hWnd As Long
    uCallbackMessage As Long
    uEdge As Long
    rc As RECT
    lParam As Long
End Type


Private Declare Function ShowCursor Lib "user32" (ByVal BShow As Long) As Long '隱藏Mouse宣告

Private Declare Function SHAppBarMessage Lib "shell32.dll" (ByVal dwMessage As Long, pData As APPBARDATA) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private 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 Declare Function GetTickCount Lib "kernel32" () As Long
Public 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
Public Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Sub HideMouseCursor()
    Dim tmpResult As Long
    Dim intX As Integer
    intX = 0
    tmpResult = 0
    Do
        intX = intX + 1
        tmpResult = ShowCursor(0)
        If intX > 10 Then Exit Do
    Loop Until tmpResult < 0
   
End Sub

Public Sub ShowMouseCursor()
    Dim tmpResult As Long
   tmpResult = ShowCursor(1)
End Sub

Public Function GetTaskBarHeight() As Long
    Dim ABD As APPBARDATA

    SHAppBarMessage ABM_GETTASKBARPOS, ABD
    GetTaskBarHeight = ABD.rc.Bottom - ABD.rc.Top

'    MsgBox "Width:" & ABD.rc.Right - ABD.rc.Left
'    MsgBox "Height:" &ABD.rc.Bottom - ABD.rc.Top

End Function

Public Function ApplicationTerminate(lHwnd As Long) As Boolean
    Dim lPid As Long, lReturn As Long, lhwndProcess As Long
    Const PROCESS_ALL_ACCESS = &H1F0FFF
    'Get the PID (process ID) from the application handle
    lReturn = GetWindowThreadProcessId(lHwnd, lPid)
    'Terminate the application
    lhwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
    ApplicationTerminate = (TerminateProcess(lhwndProcess, 0&) <> 0)
    lReturn = CloseHandle(lhwndProcess)
End Function

'不能刪除自己前一個程序
Public Sub KillAppPrev(app_name As String)
    Dim hWindow As Long
    Dim lProcessId As Long
    hWindow = FindWindow(vbNullString, app_name)
    GetWindowThreadProcessId hWindow, lProcessId
    Call Shell("kill -f  " & CStr(lProcessId), vbHide)    '先清除前一個未關閉的程式

End Sub

Public Function SetTopMostWindow(hWnd As Long, TopMost As Boolean) As Long
     If TopMost = True Then 'Make the window topmost
        SetTopMostWindow = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, flags)
     Else
        SetTopMostWindow = SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, flags)
        SetTopMostWindow = False
     End If
  End Function

Public Function IsFormLoaded(tmpFormName As String) As Boolean
   Dim tmpForm As Form
   Dim tmpResult As Boolean
   tmpResult = False
   For Each tmpForm In Forms
      If LCase(tmpForm.name) = LCase(tmpFormName) Then
         tmpResult = True
         Exit For
      End If
   Next tmpForm
    IsFormLoaded = tmpResult
End Function

Public Sub HideTaskBar()        'hide the taskbar
    Dim rtn As Long
    rtn = FindWindow("Shell_traywnd", "") 'get the Window
    Call SetWindowPos(rtn, 0, 0, 0, 0, 0, SWP_HIDEWINDOW) 'hide the Tasbar
End Sub

Public Sub ShowTaskBar()        'show th taskbar
    Dim rtn As Long
    rtn = FindWindow("Shell_traywnd", "") 'get the Window
    Call SetWindowPos(rtn, 0, 0, 0, 0, 0, SWP_SHOWWINDOW) 'show the Taskbar
End Sub

Public Sub MscommWait(Milliseconds As Long)
    Static StartTime As Long
    StartTime = GetTickCount
    Do While GetTickCount - StartTime < Milliseconds
        'DoEvents
        If GetTickCount < StartTime Then Exit Do
    Loop
End Sub

Public Sub VB_Wait(Milliseconds As Long)
    Static StartTime As Long
    StartTime = GetTickCount
    Do While GetTickCount - StartTime < Milliseconds
        DoEvents
        If GetTickCount < StartTime Then Exit Do
    Loop
End Sub

Public Sub GetControlTop(tmpControl As Object, ByRef tmpTop As Long, ByRef tmpLeft As Long, ByRef tmpHeight As Long, ByRef tmpWidth As Long)
    Dim rectUserControl As RECT
    GetWindowRect tmpControl.hWnd, rectUserControl
    tmpTop = rectUserControl.Top
    tmpLeft = rectUserControl.Left
    tmpHeight = rectUserControl.Bottom - rectUserControl.Top
    tmpWidth = rectUserControl.Right - rectUserControl.Left
End Sub