2013-05-02 121 views
0

奇怪的問題我不能把手指放在。 搜索主窗口,然後搜索標題爲「開始」的按鈕控件。 它找到開始併發送按鈕單擊後,它只是坐着,永遠不會過去,所以我從來沒有看到控制檯中的「離開循環」。SendMessage按下按鈕後卡住

該按鈕確實被按下,彈出一個消息框,我會繼續回答這部分代碼。奇怪的是,一旦我手動回答該框,它會突破NativeMethods.SendMessage(start,BM_CLICK,IntPtr.Zero,「」);我看到「離開環路」,然後它很高興,並繼續它的方式。

我在這裏錯過了什麼?希望我解釋得很好。

while (!mainFound) 
{ 
    hwnd = NativeMethods.FindWindow(null, "Loader"); 
    if (!hwnd.Equals(IntPtr.Zero)) 
    { 
     Console.WriteLine("Found Main"); 

     IntPtr p = IntPtr.Zero; 
     while (!mainFound) 
     { 
      hwndChild = NativeMethods.FindWindowEx(hwnd, p, null, null); 
      if (hwndChild == IntPtr.Zero) 
      { 
       break; 
      } 

      IntPtr start = NativeMethods.FindWindowEx(hwndChild, IntPtr.Zero, null, "Start"); 
      if (!start.Equals(IntPtr.Zero)) 
      { 
       Console.WriteLine("Found Start"); 
       NativeMethods.SendMessage(start, BM_CLICK, IntPtr.Zero, ""); 
       Console.WriteLine("Leaving Loop"); 
       mainFound = true; 
      } 

      //Console.WriteLine(hwndChild); 
      p = hwndChild; 
     } 
} 

}

回答

2

SendMessage是一個同步調用:它等待消息返回之前被處理。從您的描述中,聽起來像BM_CLICK的處理程序顯示一個模式對話框,這意味着SendMessage將不會返回,直到模式對話框被解除。

改爲嘗試PostMessage

+0

解決了這個問題。非常感謝我沒有意識到這一點。 – Tsukasa 2013-05-02 05:31:50