簡體   English   中英

嘗試從子窗口獲取“按鈕”

[英]Trying to get a “button” from a child window

首先,我對pinvoke的體驗非常有限。 我對C ++也了解甚少。 但這是我的情況

我在C#中有一個程序,該程序使用pinvoke從C ++獲取諸如SendMessageFindWindow之類的功能。 它查找窗口的名稱,獲取句柄等。現在可以正常工作。 我將代碼降低一些。

現在,我想在子窗口之一上找到一個“按鈕”。 我有實際子窗口IntPtr ,至少我認為是。 我對什么是窗口,什么不是窗口感到非常困惑。

我正在嘗試使用EnumChildWindowsFindWindowByCaptionFindWindowEx等來遍歷所有子項並找到此按鈕。 我可以使用Spy ++獲得其“標題”,但是將其提供給FindWindowByCaption不會產生任何效果。

這是實際找到父窗口的代碼。 很抱歉,如果將其與非常老的軟件一起運行,則很抱歉。

使用pInvoke,這一切在C#中。 我嘗試首先使用此方法獲取所需的按鈕。

FindWindowByCaption(IntPtr.Zero, "{{Caption as shown by Spy++}}");

即使我給了正確的標題,這也不起作用。 所以我嘗試一下...在此示例中, Handle是包含要查找的按鈕的父窗口的IntPtr

// create a small counter so that we do not try to check
// too many windows and crash the program.
int i = 0;
var previous = new Window();
var current = new Window();

do {

    current = 
        new Window(
            InteropServices.FindWindowEx(
            Handle,
            previous.Handle,
            null, null
        )
    );
    if (current == IntPtr.Zero)
        break;

    // add current to list

    // continue
    previous = current; // never finds the right button, either.
    ++i;
} while (i < 500);

但是即使這樣,我所需的控件也從未找到。 誰能幫我這個? 它變得非常令人沮喪。 我可以看到Spy ++中的Caption ,也可以看到它有一個手柄。 它似乎沒有什么特別的。

您需要執行以下操作:

  1. 調用FindWindow查找頂層窗口。 使用類名或/或窗口標題或兩者來標識它。
  2. 反復調用FindWindowEx以查找子窗口。 將父窗口作為hwndParent傳遞,並將NULL作為hwndChildAfter 再次使用類名或/或窗口標題,或兩者,以標識子窗口。
  3. 最終,您將下降父/子窗口的層次結構,直到到達目標窗口。

因此,作為示例,請考慮以下使用Spy ++表示法的層次結構:

Window 00001000 "My MainForm window title" "MainFormWindowClass"
|
-- Window 00002000 "Panel container" "PanelWindowClass"
   |
   -- Window 00003000 "Click me!" "ButtonWindowClass"

您可以通過以下調用順序找到它:

HWND main = FindWindow("MainFormWindowClass", NULL); // just use class name
HWND panel = FindWindowEx(main, NULL, "PanelWindowClass", "Panel container");
HWND button = FindWindowEx(panel, NULL, "ButtonWindowClass", "Click me!");

請注意,我已經用C ++編寫了此代碼,以避免與p / invoke混淆。 並且為了簡潔的示例,我也省略了所有錯誤檢查。 您顯然會檢查錯誤。

我建議您手頭有Spy ++,並嘗試編寫一個簡單的C ++控制台應用程序來定位您的窗口。 一旦知道完成該工作的對FindWindowFindWindowEx的調用順序,就可以將它們轉換為C#。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM