PDA

View Full Version : بدست آوردن هندل یک شئ از یک Application دیگر



YourName
جمعه 10 شهریور 1385, 12:13 عصر
سلام.من یه سوال دارم که تا حالا هر جا مطرح کردم نتونستن جواب درستی به من بدن حتی MCSD.NET های با تجربه هم من و پاس میکردن اینور و اونور.من میخوام هندل یه شیئ رو در یه پنجره بگیرم مثلا هندل TextBox Open در پنجره ی Run بدون اینکه ماوس رو روی این TextBox ببرم تا هندلش رو به من بده.اگه جوابمو بدین که خیلی خیلی ممنون.

titbasoft
جمعه 10 شهریور 1385, 17:02 عصر
اگر منظور شما رو درست متوجه شده باشم، شما می تونید از EnumChildWindows برای بدست آوردن هندل فرزندان یک Window استفاده کنید.

یه کد خیلی خیلی خوب در این رابطه یک جایی دیدم که حیفم اومد اینجا کپیش نکنم:

Imports System.Text

' a collection of top-level or child windows

Public Class WindowCollection
Inherits ReadOnlyCollectionBase

Private Delegate Function EnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As IntPtr) As Integer
Private Declare Function EnumChildWindows Lib "user32" Alias "EnumChildWindows" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As IntPtr) As Integer

' the handle of the parent window, or zero if top-level windows
Dim m_hWnd As IntPtr

' constructors

Sub New()
' create a collection of top-level windows
Me.New(IntPtr.Zero)
End Sub

Sub New(ByVal handle As IntPtr)
' create a collection of top-level or child-windows
m_hWnd = handle
If handle.Equals(IntPtr.Zero) Then
' top-level windows
EnumWindows(New EnumWindowsProc(AddressOf EnumWindows_CBK), IntPtr.Zero)
Else
' child windows
EnumChildWindows(m_hWnd, AddressOf EnumWindows_CBK, m_hWnd)
End If
End Sub

' the callback function

Private Function EnumWindows_CBK(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
' add a new window to the inner list
Me.InnerList.Add(New Window(hWnd))
' return True to continue enumeration
Return True
End Function

' the Item readonly property
Default ReadOnly Property Item(ByVal index As Integer) As Window
Get
Return Me.Item(index)
End Get
End Property

End Class

' a Windows's window

Public Class Window
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As String) As Integer
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer

' the handle of this window
Dim m_hWnd As IntPtr
' the collection of child windows
Dim m_ChildWindows As WindowCollection

' constructor
Sub New(ByVal handle As IntPtr)
m_hWnd = handle
End Sub

' the Handle property (readonly)
ReadOnly Property Handle() As IntPtr
Get
Return m_hWnd
End Get
End Property

' the Text property

Property Text() As String
Get
Dim sb As New StringBuilder(1024)
GetWindowText(m_hWnd, sb, sb.Capacity)
Return sb.ToString
End Get
Set(ByVal Value As String)
SetWindowText(m_hWnd, Value)
End Set
End Property

' the ClassName property (readonly)

ReadOnly Property ClassName() As String
Get
Dim sb As New StringBuilder(1024)
GetClassName(m_hWnd, sb, sb.Capacity)
Return sb.ToString
End Get
End Property

' the Description property (readonly)

ReadOnly Property Description() As String
Get
' return handle, classname, and Text
Return String.Format("[{0:X8}] {1} ""{2}""", m_hWnd, ClassName, Text)
End Get
End Property

' the ChildWindows collection
' (the collection is created the first time this property is queried)

ReadOnly Property ChildWindows() As WindowCollection
Get
If m_ChildWindows Is Nothing Then
m_ChildWindows = New WindowCollection(m_hWnd)
End If
Return m_ChildWindows
End Get
End Property

End Class


حالا با استفاده از 2 تا کلاس بالا فقط کافیه یک چنین چیزی رو استفاده کنید:

Dim topWindows As New WindowCollection

Dim w As Window
For Each w In topWindows
If w.Description.ToLower.IndexOf("""run""") > 0 Then
For Each c As Window In w.ChildWindows
ListBox1.Items.Add(c.Description)
Next
End If
Next
انشاء الله Enjoy ;)

S.Azish
جمعه 10 شهریور 1385, 17:48 عصر
روش ساده تر استفاده از FindWindowEx هست



[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string lpszWindow);

private IntPtr GetEditHandle()
{
IntPtr result = new IntPtr();

IntPtr desktopHandle = IntPtr.Zero;

IntPtr runWindowHandle = FindWindowEx(desktopHandle, IntPtr.Zero, null, "Run");

if (runWindowHandle.ToInt32() > 0)
{
IntPtr comboBoxHandle = FindWindowEx(runWindowHandle, IntPtr.Zero, "Combobox", null);
IntPtr textBoxHandle = FindWindowEx(comboBoxHandle, IntPtr.Zero, "Edit", null);

result = textBoxHandle;
}

return result;
}

YourName
شنبه 11 شهریور 1385, 09:59 صبح
سلام. از هر دوتون خیلی ممنون تستشون میکنم.

YourName
شنبه 11 شهریور 1385, 10:19 صبح
واقعا عالی بود ممنون مخصوصا آقای titbasoft