2012/12/05

WinAPI備忘

    ''''''''''''''''''''''
    '注意Long / Integer型別的差別
    ''''''''''''''''''''''
    'FindWindow(要搜尋視窗的Class name, 要搜尋視窗的標題) 任一有即可搜尋
    Public Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    'FindWindowEx(要搜尋視窗的Parent class name,由這個IntPtr之後的子視窗開始搜尋要從頭開始設IntPtr.zero, 要搜尋視窗的Class name, 要搜尋視窗的標題)
    Public Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
    'SetForegroundWindow(要設為最前的視窗IntPtr)
    Public Declare Auto Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
    'PostMessage(要傳送的視窗IntPtr, 設定傳送的動作方式, 要傳送的按鍵 or button ID, Null or button intptr)
    Public Declare Auto Function PostMessage Lib "user32.dll" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    '類似PosetMessage但會等Stack執行到才Return
    Public Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    'GetWindowText(要獲得內容的視窗IntPtr, 儲存內容的字串, 長度)
    Public Declare Auto Function GetWindowText Lib "user32.dll" (ByVal hwnd As Integer, ByVal lpString As String, ByVal cch As Integer) As Integer
    'GetClassName(要獲得Class name的視窗IntPtr, 儲存class name的字串, 長度)
    Public Declare Auto Function GetClassName Lib "user32.dll" (ByVal hwnd As Integer, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
    Public Declare Auto Function SetWindowText Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean
    '傳送string,lparam宣告為string,注意函式為vb.net的宣告
     _
    Public Function SendMessageString(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    End Function

    Public Declare Auto Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As Integer, ByVal lpEnumFunc As Integer, ByVal lParam As Integer) As Integer

    Public Const WM_KEYDOWN As Integer = &H100
    Public Const WM_KEYUP As Integer = &H101
    Public Const WM_COMMAND As Integer = &H111
    Public Const WM_SETTEXT As Integer = &HC
    Public Const WM_GETTEXT As Integer = &HD
    Public Const VK_ENTER As Integer = &HD
    Public Const WM_RMOUSE As Integer = &H2
    Public Const VK_SPACE As Integer = &H20
    Public Const CB_FINDSTRING As Integer = &H14C
    Public Const CB_SETCURSEL As Integer = &H14E
    Public Const CB_SELECTSTRING As Integer = &H14

    'GetWindowThreadProcessId(要獲得process id的視窗IntPtr, 儲存process id的integer)
    Public Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
    'GetDlgCtrlID(傳Intptr取得button的ID)
    Public Declare Function GetDlgCtrlID Lib "user32.dll" (ByVal hwnd As Integer) As Integer
    'http://msdn.microsoft.com/zh-tw/library/windows/desktop/ms684320%28v=vs.85%29.aspx
    '使用後要用CloseHandle(handle)來結束
    Public Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
    Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Integer) As Integer

    ''''''''''''''''''''''''''''
    '注意VB6預設為傳值,VB.NET為傳址,注意宣告的ByVal / ByRef
    ''''''''''''''''''''''''''''
    'http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545%28v=vs.85%29.aspx 
    Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    'GetCursorPos(滑鼠x,y位置,要宣告包含x,y的structure)
    Public Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Integer
    'WindowFromPoint(滑鼠x軸,滑鼠y軸) 獲得在這滑鼠座標下最上層的視窗Intptr
    Public Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Integer, ByVal yPoint As Integer) As Integer

    Public Structure POINTAPI
        Dim x As Integer
        Dim y As Integer
    End Structure

沒有留言: