句柄是 Windows 系統中的概念,和VB.NET無關。
成都創新互聯專注于定遠企業網站建設,響應式網站設計,商城網站制作。定遠網站建設公司,為定遠等地區提供建站服務。全流程定制網站制作,專業設計,全程項目跟蹤,成都創新互聯專業和態度為您提供的服務
通俗地講,句柄就是一個數字,也就是一個編號。
比如說,你電腦中有10個窗口,每個窗口都會有一個編號,這是操作系統區分各個窗口的依據
Imports?System.Text
Imports?System.Runtime.InteropServices
Public?Class?Form1
'?相關API函數聲明,注釋掉的這里沒用到,但是也比較常用吧,這些函數的功能都能搜到。
Private?Declare?Function?FindWindow?Lib?"user32"?Alias?"FindWindowA"?(ByVal?lpClassName?As?String,?ByVal?lpWindowName?As?String)?As?IntPtr
Private?Declare?Function?FindWindowEx?Lib?"user32"?Alias?"FindWindowExA"?(ByVal?hWnd1?As?IntPtr,?ByVal?hWnd2?As?IntPtr,?ByVal?lpsz1?As?String,?ByVal?lpsz2?As?String)?As?IntPtr
Private?Delegate?Function?EnumChildProc(ByVal?hWnd?As?IntPtr,?ByVal?lParam?As?Integer)?As?Boolean
Private?Declare?Function?EnumChildWindows?Lib?"user32.dll"?(ByVal?hWndParent?As?IntPtr,?ByVal?lpEnumFunc?As?EnumChildProc,?ByVal?lParam?As?Integer)?As?Boolean
Private?Declare?Auto?Function?SendMessage?Lib?"User32.dll"?(ByVal?hWnd?As?IntPtr,?ByVal?Msg?As?Integer,?ByVal?wParam?As?Integer,?ByVal?lParam?As?String)?As?Integer
'Private?Declare?Function?CheckDlgButton?Lib?"user32"?Alias?"CheckDLGButtonA"?(ByVal?hDlg?As?IntPtr,?ByVal?nIDButton?As?IntPtr,?ByVal?wCheck?As?Integer)?As?Integer
Private?Declare?Function?GetClassName?Lib?"user32"?Alias?"GetClassNameA"?(ByVal?hWnd?As?IntPtr,?ByVal?lpClassName?As?StringBuilder,?ByVal?nMaxCount?As?Integer)?As?Integer
'Private?Declare?Function?GetWindowThreadProcessId?Lib?"user32"?Alias?"GetWindowThreadProcessId"?(ByVal?hwnd?As?IntPtr,?ByVal?lpdwProcessId?As?Long)?As?Integer
Private?Declare?Auto?Function?GetWindowTextLength?Lib?"user32"?Alias?"GetWindowTextLength"?(ByVal?hwnd?As?IntPtr)?As?Integer
Private?Declare?Function?GetWindowText?Lib?"user32"?Alias?"GetWindowTextA"?(ByVal?hwnd?As?IntPtr,?ByVal?lpString?As?StringBuilder,?ByVal?cch?As?Integer)?As?Integer
'?相關消息定義,也有沒用到的
Const?WM_SETTEXT?=?HC
Const?WM_GETTEXT?=?HD
'Const?WM_SETFOCUS?=?H7
'Const?WM_KILLFOCUS?=?H8
'Const?WM_CLOSE?=?H10
'Const?WM_SYSCOMMAND?=?H112
'Const?SC_CLOSE?=?HF060
'Const?SC_MINIMIZE?=?HF020
Const?BM_GETCHECK?=?HF0
Const?BM_SETCHECK?=?HF1
Const?BM_GETSTATE?=?HF2
Const?BM_SETSTATE?=?HF3
Const?BM_SETSTYLE?=?HF4
Const?BM_CLICK?=?HF5
'Const?BM_GETIMAGE?=?HF6
'Const?BM_SETIMAGE?=?HF7
Const?BST_UNCHECKED?=?O0
Const?BST_CHECKED?=?O1
Const?BST_INDETERMINATE?=?O2
'?儲存窗口句柄
Dim?WindowHandle?As?IntPtr
'?儲存兩個(或者多個)編輯框句柄
Dim?EditHandle?As?New?List(Of?IntPtr)
Dim?EditWindowsText?As?List(Of?String)
'?儲存復選框句柄
Dim?CheckHandle?As?IntPtr?=?0
Private?Sub?Form1_Load(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?MyBase.Load
Button1_Click(sender,?e)
End?Sub
'?EnumChildWindows?回調函數,該函數名作為API函數EnumChildWindows?的一個參數
'?該函數實現了枚舉各個子窗口,找出編輯框屬性的功能
Public?Function?EnumChildProcC(ByVal?hwnd?As?IntPtr,?ByVal?lParam?As?Integer)?As?Boolean
Dim?dwWindowClass?As?StringBuilder?=?New?StringBuilder(100)
'?獲得某一個句柄的類名
GetClassName(hwnd,?dwWindowClass,?100)
If?dwWindowClass.ToString.Contains("EDIT")?Or?dwWindowClass.ToString.Contains("Edit")?Then?????'?類名包含EDIT的為編輯框
EditHandle.Add(hwnd)????????????????????????'?存儲該句柄
End?If
'?返回?True?一直枚舉完
Return?True
End?Function
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
WindowHandle?=?FindWindow(vbNullString,?"登陸")
If?WindowHandle.ToInt32?=?0?Then
MsgBox("未捕獲到窗口"?+?"登陸")
Return
End?If
'?枚舉所有主窗口的子窗口(控件),枚舉時自動調用回調函數,完成編輯框句柄的獲取
EnumChildWindows(WindowHandle,?AddressOf?EnumChildProcC,?0)
'?尋找復選框
CheckHandle?=?FindWindowEx(WindowHandle,?IntPtr.Zero,?vbNullString,?"記住密碼")
Dim?str?As?New?StringBuilder
Dim?j?As?Integer?=?0
'?對編輯框文本賦值
For?j?=?0?To?EditHandle.Count?-?1
SendMessage(EditHandle(j),?WM_SETTEXT,?0,?"Text")
'GetWindowText(EditHandle(j),?str,?20)
'EditWindowsText.Add(Str.ToString)
'Str.Clear()
Next
If?EditHandle.Count?=?0?Then
MsgBox("未找到輸入框!")
End?If????????
If?CheckHandle.ToInt32??0?Then
'CheckDlgButton(WindowHandle,?id,?1)
'?對復選框進行鼠標單擊操作
SendMessage(CheckHandle,?BM_CLICK,?0,?0)
'SendMessage(CheckHandle,?BM_SETCHECK,?True,?0)
End?If
End?Sub
End?Class
vb中得到窗口句柄(HANDLE)了可以對窗口進行那些操作???當然可以得到其它參數信息,比如按鈕。 關閉是可以播放歌曲的話,你得找到按鈕的句柄。希望對你有幫助 本答案由 友情提供
名稱欄目:vb.net句柄原理 vb獲取句柄
當前路徑:http://vcdvsql.cn/article18/doodigp.html
成都網站建設公司_創新互聯,為您提供企業建站、網站排名、網站制作、域名注冊、軟件開發、網站改版
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯