C# C“关闭展望”;发件箱不是空的”;以编程方式进行对话

C# C“关闭展望”;发件箱不是空的”;以编程方式进行对话,c#,dialog,outlook,C#,Dialog,Outlook,我有一个使用Outlook在后台发送信息的程序。我希望避免让用户处理“发件箱不为空”消息,该消息在电子邮件位于发件箱中时,在用户尝试关闭Outlook时出现。因为在大多数情况下,发件箱中的电子邮件不会是他们自己发送的电子邮件。我能够获得对话框的句柄,但我不知道要发送哪个命令使其关闭。我所知道的唯一一个关闭命令在对话框中不起作用 由于安全限制,我必须使用Outlook发送电子邮件 我从中获得了代码,它向您展示了如何捕获窗口事件并对所需窗口进行排序。我已经找到了窗口,它帮助我停止了可能发送电子邮件的

我有一个使用Outlook在后台发送信息的程序。我希望避免让用户处理“发件箱不为空”消息,该消息在电子邮件位于发件箱中时,在用户尝试关闭Outlook时出现。因为在大多数情况下,发件箱中的电子邮件不会是他们自己发送的电子邮件。我能够获得对话框的句柄,但我不知道要发送哪个命令使其关闭。我所知道的唯一一个关闭命令在对话框中不起作用

由于安全限制,我必须使用Outlook发送电子邮件

我从中获得了代码,它向您展示了如何捕获窗口事件并对所需窗口进行排序。我已经找到了窗口,它帮助我停止了可能发送电子邮件的线程,但是当我完成时对话框仍然挂在那里

每当资源管理器窗口停用(即失去焦点)时,将执行以下代码

void explorerRapper_Deactivate()
{
IntPtr hBuiltInDialog=WinApiProvider.FindWindow(“#32770”,“Microsoft Office Outlook”);
if(hBuiltInDialog!=IntPtr.Zero)
{
//好的,找到一个
//让我们看看有什么儿童窗
List childWindows=WinApiProvider.EnumChildWindows(hBuiltInDialog);
//让我们获取子窗口的标题列表
列出childWindowNames=WinApiProvider.GetWindowNames(childWindows);
//现在检查一些条件以识别内置对话框。。
//下面是调试时从代码中剪切和粘贴的三个子窗口名称
//[0]=“发件箱中有未发送的邮件。若要发送邮件,Outlook必须保持运行并连接到电子邮件服务器。是否仍要退出?\r\n\r\n秒后退出”
//[1]=“不发送退出”
//[2]=“不退出”
if((childWindowNames.Contains(“发件箱中有未发送的邮件。要发送邮件,Outlook必须保持运行并连接到电子邮件服务器。是否仍要退出?\r\n\r\n秒后退出”))&&
(childWindowNames.Contains(“不发送而退出”))&&
(childWindowNames.Contains(“不退出”))
{
//此时,我们需要清空任何IkeNet电子邮件的发件箱,如果发件箱为空,则关闭对话框
//让outlook也关闭
NotifyAdmin.SetShutdownRequested();
///此关闭命令似乎不适用于此窗口。据推测,它的作用类似于按
///该键在程序运行时对窗口不起任何作用。
WinApiProvider.SendMessage(hBuiltInDialog,
WinApiProvider.WM_SYSCOMMAND,WinApiProvider.SC_CLOSE,0);
}
}
}
WinApiProvider.SC_CLOSE命令不适用于此类窗口


如果您有任何建议,我们将不胜感激。

您可以尝试使用SendKeys来[tab][tab][enter](或任何按键)。我知道这不是一个优雅的解决方案,但这个解决方案一开始真的优雅吗

我会尝试将鼠标单击事件发送到“取消”按钮(或“关闭”按钮——我不熟悉所讨论的对话框)

    void ExplorerWrapper_Deactivate()
    {
        IntPtr hBuiltInDialog = WinApiProvider.FindWindow("#32770", "Microsoft Office Outlook");
        if (hBuiltInDialog != IntPtr.Zero)
        {
            // ok, found one
            // let's see what childwindows are there
            List<IntPtr> childWindows = WinApiProvider.EnumChildWindows(hBuiltInDialog);
            // Let's get a list of captions for the child windows
            List<string> childWindowNames = WinApiProvider.GetWindowNames(childWindows);

            // now check some criteria to identify the build in dialog..

            // here are the three child window names as cut and pasted from the code when debugging
            // [0] = "There are unsent messages in your Outbox. To send messages, Outlook must remain running and connected to your e-mail server. Do you want to exit anyway?\r\n\r\nExiting in <0d> seconds"
            // [1] = "Exit Without Sending"
            // [2] = "Don't Exit"

            if ((childWindowNames.Contains("There are unsent messages in your Outbox. To send messages, Outlook must remain running and connected to your e-mail server. Do you want to exit anyway?\r\n\r\nExiting in <0d> seconds")) &&
                (childWindowNames.Contains("Exit Without Sending")) &&
                (childWindowNames.Contains("Don't Exit")))
            {
                // at this point, we need to empty the outbox of any IkeNet email, and if the outbox is then empty, close the dialog
                // and let outlook close as well
                NotifyAdmin.SetShutdownRequested();
                /// this close command does not seem to work for this window.  supposedly it acts just like pressing
                /// the <esc> key, which does nothing to the window when the program is running.
                WinApiProvider.SendMessage(hBuiltInDialog,
                      WinApiProvider.WM_SYSCOMMAND, WinApiProvider.SC_CLOSE, 0);

            }
        }
    }