簡體   English   中英

C#Pinvoke在第一次列表計數為0后找不到控件的集合

[英]C# Pinvoke can't find the Hwnd of Controls after List count was 0 at first time

我試圖單擊另一個應用程序中的按鈕(從我的Programm中以Process.Start開始)

問題:我需要等到“加載”屏幕消失並且GUI彈出窗口出現...

我的想法是讀取所有(Hwnd)控件,直到從GUI找到特定控件(按鈕:“ Kill Client”)為止(= GUI已打開)。

但這僅在我手動等待GUI並按“搜索控制”按鈕時有效。

如果在加載屏幕處於活動狀態時按“搜索按鈕”,則會得到Hwnd = 0(List <'IntPtr>計數也為0 ...),如果在打開GUI時再次按該按鈕,則它將再次為0( List <'IntPtr>也算...)!!!

這是我的代碼:

 public class WndSearcher
 {
    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);

    public static List<IntPtr> GetChildWindows(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        GCHandle listHandle = GCHandle.Alloc(result);
        try
        {
            EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
            EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
        return result;
    }

    private static bool EnumWindow(IntPtr handle, IntPtr pointer)
    {
        GCHandle gch = GCHandle.FromIntPtr(pointer);
        List<IntPtr> list = gch.Target as List<IntPtr>;
        if (list == null)
        {
            throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
        }
        list.Add(handle);
        return true;
    }
}

我的按鈕:

List<IntPtr> AllControlHandles = WndSearcher.GetChildWindows(selectedCharacter.Botprocess.MainWindowHandle);
IntPtr ControlHandle = AllControlHandles.Find(x => PInvoke.GetWindowTextRaw(x) == "Kill Client" ? true : false);
MessageBox.Show(ControlHandle.ToString());

PInvoke(類)的一部分:

const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);

public static string GetWindowTextRaw(IntPtr hwnd)
    {
        // Allocate correct string length first
        int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, null);
        StringBuilder sb = new StringBuilder(length + 1);
        SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
        return sb.ToString();
    }

直到現在都找不到解決方案。

因此,我決定將AutoHotKey與C#結合使用。

在C#中,我啟動AutoHotKey腳本,然后等待腳本完成。 (然后,外部程序將完全啟動)

起始參數:1.Processid 2.NewExternalProgramName

這是我的AutoHotKey腳本:

counter := 0
Loop, %0%  ; For each parameter:
{

    param := %A_Index%

    if (counter = 0) ; do sth with parameter 1
        winwait, ahk_pid %param% ; Not logged in ;wait until the text "Not logged in" can be read (Program started completely)

    if (counter = 1) ; do sth with parameter 2
        WinSetTitle, %param%

    counter += 1
}

暫無
暫無

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

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