2013-12-10 34 views
1

我有一個代碼如何點擊「是」按鈕,以便應用程序與代碼

private const int WM_CLOSE = 16; 
    private const int BN_CLICKED = 245; 
    [DllImport("User32.dll")] 
    public static extern Int32 FindWindow(String lpClassName, String lpWindowName); 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam); 

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

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] 
    private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 



public void Click(string _btnTitle) 
    { 
     int hwnd = 0; 
     IntPtr hwndChild = IntPtr.Zero; 

     //Get a handle for the Calculator Application main window 
     // foreach (Process p in Process.GetProcesses()) 
     //{ 
     hwnd = FindWindow(null, FrmTitle);    
     if (hwnd != 0) 
     { 
      hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", _btnTitle); 
      SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero); 
     } 
    } 

我不能點擊按鈕「是」應用:(

任何人都得到了末梢部的消息框? TKS

+0

可以肯定,「hwnd」和「hwndChild」確實有價值嗎? –

+1

您是否檢查確認hwndChild返回了有效的按鈕?我在過去的生活中廣泛地使用了這種技術(配置3k +工作站管理),通常最大的工作是「爬樹」窗口,以確保在發送「點擊」消息之前擁有正確的控制權。我通常會使用應用程序手動找到我想要的窗口,然後編寫代碼來搜索,查找併發送我想要的消息。 (如果你的實際代碼比例子更復雜,你可能需要在正確的狀態下將「父」窗口置於正確的狀態,然後才能真正「聽」到「單擊」消息。 – Wonderbird

+1

你是否使用了spy ++或其他工具來確保你的hwndChild是你認爲它應該是什麼窗口?另外,FrmTitle實際上是表單嗎?你不應該傳遞它的句柄嗎? –

回答

1

您還沒有發送正確的消息。

嘗試在調用的SendMessage(使用BM_CLICK(0x00F5))。這應該工作提供了hwndChild是個e按鈕的窗口句柄,而不是容器對話框。

BN_CLICKED不起作用,因爲這是通知代碼,而不是消息。

+0

好的答案,並且正確無誤!我已經從回答中刪除了表示對問題年齡的擔憂,因爲這不是我們認爲對堆棧溢出重要的東西。不管問題多大,如果你有一個好的答案,你應該分享它,就像你做的一樣!我們總是從谷歌獲得新的流量,並且有很多人你的答案仍然可以幫助。 –

相關問題