public static void CloseDialogBox(IntPtr dialogBoxHandle, string buttonName) { IntPtr btnHandle = NativeUtil.FindWindowEx(dialogBoxHandle, IntPtr.Zero, null, buttonName); Assert.AreNotEqual(IntPtr.Zero, btnHandle, "Failed to find button in the dialog box."); NativeUtil.SetForegroundWindow(dialogBoxHandle); NativeUtil.SendMessage(btnHandle, 0x0201 /*left button down*/, IntPtr.Zero, IntPtr.Zero); NativeUtil.SendMessage(btnHandle, 0x0202 /*left button up*/, IntPtr.Zero, IntPtr.Zero); }
private static void CloseMessageBox(IntPtr msgBoxHandle, string buttonName) { if (buttonName == null) { // Simple close message box NativeUtil.SetForegroundWindow(msgBoxHandle); NativeUtil.SendMessage(msgBoxHandle, 0x0112 /*WM_SYSCOMMAND*/, new IntPtr(0xF060 /*SC_CLOSE*/), IntPtr.Zero); } else { // This may be flaky.. if there're more than one windows pop up at the same time.. // it will affect clicking the button IntPtr btnHandle = NativeUtil.FindWindowEx(msgBoxHandle, IntPtr.Zero, "Button", buttonName); Assert.AreNotEqual(IntPtr.Zero, btnHandle, "Failed to find button in the messagebox."); NativeUtil.SetForegroundWindow(msgBoxHandle); NativeUtil.SendMessage(btnHandle, 0x0201 /*left button down*/, IntPtr.Zero, IntPtr.Zero); NativeUtil.SendMessage(btnHandle, 0x0202 /*left button up*/, IntPtr.Zero, IntPtr.Zero); } }