簡體   English   中英

以編程方式單擊“消息框”按鈕

[英]Click A MessageBox button programmatically

正如標題所示,我試圖以編程方式模擬MessageBox中的按鈕單擊。 我之前嘗試通過其標題查找其句柄,並在SendMessage()應用WM_CLOSESC_CLOSE來關閉MessageBox。 但是,由於存在“是/否”按鈕,這不起作用(X按鈕呈灰色顯示)。

現在我想點擊No按鈕,如下所示 - :

List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
     IntPtr Window_hWnd = CloseMessageBox.FindWindowByCaption("#32770", "LastQuestion"); //Could use null as the first argument too. "#32770" represents classname Dialog.
     CloseMessageBox.EnumChildWindows(Window_hWnd, (hWnd, lParam) =>
     {
          StringBuilder sb = new StringBuilder();
          foreach (var control in GCHandle.FromIntPtr(lParam).Target as List<IntPtr>)
          {
                CloseMessageBox.GetWindowText(control, sb, 250);
                if (sb.Equals("&No"))
                {
                    CloseMessageBox.PostMessage(hWnd, CloseMessageBox.MouseDown, 0, 0);
                    CloseMessageBox.PostMessage(hWnd, CloseMessageBox.MouseUp, 0, 0);
                }
          }
          return false;
     }, GCHandle.ToIntPtr(listHandle));

 }
 catch (Exception e)
 {
     MessageBox.Show(e.ToString());
 }
 finally
 {
     if (listHandle.IsAllocated)
        listHandle.Free();
 }

從IRC的某個人的建議到目前為止,我發現之前的一些編輯,我得到了按鈕句柄(只有“&Yes”按鈕)但不是全部。 然后他建議使用這種方法,但control列表沒有填充,因此它永遠不會進入foreach 我該怎么做才能解決這個問題?

干得好。

// A delegate which is used by EnumChildWindows to execute a callback method.
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);

// This method accepts a string which represents the title name of the window you're looking for the controls on.
public static void ClickButtonLabeledNo(string windowTitle)
{
    try
    {
        // Find the main window's handle by the title.
        var windowHWnd = FindWindowByCaption(IntPtr.Zero, windowTitle);

        // Loop though the child windows, and execute the EnumChildWindowsCallback method
        EnumChildWindows(windowHWnd, EnumChildWindowsCallback, IntPtr.Zero);
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

private static bool EnumChildWindowsCallback(IntPtr handle, IntPtr pointer)
{
    const uint WM_LBUTTONDOWN = 0x0201;
    const uint WM_LBUTTONUP = 0x0202;

    var sb = new StringBuilder(256);
    // Get the control's text.
    GetWindowCaption(handle, sb, 256);
    var text = sb.ToString();

    // If the text on the control == &No send a left mouse click to the handle.
    if (text == @"&No")
    {
        PostMessage(handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
        PostMessage(handle, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
    }

    return true;
}

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

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

[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)]
private static extern IntPtr GetWindowCaption(IntPtr hwnd, StringBuilder lpString, int maxCount);

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

暫無
暫無

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

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