2013-01-14 161 views
1

我已經編寫了一個程序,用於在另一個程序中查找一個框並將焦點設置爲該框。一旦完成,它將發送密鑰並保存到此框中。FindWindow FindWindowEx

我正在使用Findwindow和FindwindowEx來定位框,但我有點問題。 enter image description here 如果你注意到窗口一直到第一個TPanel都一樣。之後有3Tpanel類。 3Tpanel類之後有多個TttgEdit類。

如何教我選擇的課程? 到目前爲止,這是我的代碼。

Delcare

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long 

Private Declare Auto Function FindWindow Lib "user32.dll" (_ 
ByVal lpClassName As String, _ 
ByVal lpWindowName As String _ 
) As IntPtr 

Private Declare Auto Function FindWindowEx Lib "user32.dll" (_ 
ByVal hwndParent As IntPtr, _ 
ByVal hwndChildAfter As IntPtr, _ 
ByVal lpszClass As String, _ 
ByVal lpszWindow As String _ 
) As IntPtr 

來源

Dim hWnd As IntPtr = FindWindow("TRunprgForm", Nothing) 
    If hWnd.Equals(IntPtr.Zero) Then 
     Return 
    End If 
    cb1.Checked = True 
    '--------------------instert here 
    Dim hWndChild1 As IntPtr = _ 
    FindWindowEx(hWnd, IntPtr.Zero, "TmisinvForm", Nothing) 
    If hWndChild1.Equals(IntPtr.Zero) Then 
     Return 
    End If 

    Dim hWndChild2 As IntPtr = _ 
    FindWindowEx(hWndChild1, IntPtr.Zero, "TScrollBox", Nothing) 
    If hWndChild2.Equals(IntPtr.Zero) Then 
     Return 
    End If 

    Dim hWndChild3 As IntPtr = _ 
    FindWindowEx(hWndChild2, IntPtr.Zero, "TPageControl", Nothing) 
    If hWndChild3.Equals(IntPtr.Zero) Then 
     Return 
    End If 

    Dim hWndChild4 As IntPtr = _ 
    FindWindowEx(hWndChild3, IntPtr.Zero, "TTabSheet", Nothing) 
    If hWndChild4.Equals(IntPtr.Zero) Then 
     Return 
    End If 

    Dim hWndChild5 As IntPtr = _ 
    FindWindowEx(hWndChild4, IntPtr.Zero, "TttgCenterPanel", Nothing) 
    If hWndChild5.Equals(IntPtr.Zero) Then 
     Return 
    End If 

    Dim hWndChild6 As IntPtr = _ 
    FindWindowEx(hWndChild5, IntPtr.Zero, "TPanel", Nothing) 
    If hWndChild6.Equals(IntPtr.Zero) Then 
     Return 
    End If 

    Dim hWndEdit As IntPtr = _ 
    FindWindowEx(hWndChild6, IntPtr.Zero, "TttgDBEdit", Nothing) 
    If hWndEdit.Equals(IntPtr.Zero) Then 
     Return 
    End If 

    SetForegroundWindow(hWndEdit) 

在左側的數字,的hWnd,他們改變每屏幕開和關的時間,所以我不能把它們作爲一個靜態的數字。任何幫助都是極好的。

回答

2

看起來你想要第二個TPanel在TttgCenterPanel下。

爲了做到這一點,你可以找到第一個TPanel(你已經做到了這一點),之後找到TttgCenterPanel的後代TPanel,並在第一個TPanel之後。您需要將hwndChild5傳遞給hwndChildAfter of FindWindowEx`。

' .... all the stuff you did before 

Dim hWndChild5 As IntPtr = _ 
FindWindowEx(hWndChild4, IntPtr.Zero, "TttgCenterPanel", Nothing) 
If hWndChild5.Equals(IntPtr.Zero) Then 
    Return 
End If 

Dim hWndChild6 As IntPtr = _ 
FindWindowEx(hWndChild5, IntPtr.Zero, "TPanel", Nothing) 
If hWndChild6.Equals(IntPtr.Zero) Then 
    Return 
End If 

Dim hWndChild6Second As IntPtr = _ 
FindWindowEx(hWndChild5, hWndChild6, "TPanel", Nothing) 
If hWndChild6Second.Equals(IntPtr.Zero) Then 
    Return 
End If 

Dim hWndEdit As IntPtr = _ 
FindWindowEx(hWndChild6Second, IntPtr.Zero, "TttgDBEdit", Nothing) 
If hWndEdit.Equals(IntPtr.Zero) Then 
    Return 
End If 

SetForegroundWindow(hWndEdit) 

MSDN documentation of FindWindowEx

hwndChildAfter [中,可選]

類型:HWND

的句柄,子窗口。 搜索以Z順序的下一個子窗口開始。子窗口必須是hwndParent的直接子窗口,而不僅僅是子窗口。

如果hwndChildAfter爲NULL,則搜索從hwndParent的第一個子窗口開始。

如果您嘗試查找第二個TPanel,則此方法將起作用。如果它們每次都是隨機的,這會失敗。

+0

對不起延遲。這是現貨。謝謝 – user1946138