【问题标题】:Outlook 2010 plugin wrong 32770 windowOutlook 2010 插件错误 32770 窗口
【发布时间】:2016-12-09 15:55:24
【问题描述】:

我正在为 Outlook 2010 开发一个自定义地址对话框,遵循此处发布的 Helmut Oberdan 示例

http://www.codeproject.com/Articles/21288/Customize-the-built-in-Outlook-Select-Names-dialog

我已经使用框架 4.5 将项目迁移到 VS2015,但是 我在使用 findwindow 函数时遇到了麻烦

IntPtr hBuiltInDialog = WinApiProvider.FindWindow("#32770", "");

在某些计算机(我的)上运行良好,而在其他计算机(客户)上则不行。 似乎该函数找到了另一个不是 Outlook 的 32770 窗口,我尝试枚举所有 32770 窗口,但是当 InspectorWrapper_Deactivate 函数启动时,列表中不存在我的 32770 窗口。 发生了一个奇怪的行为,如果我在 deactivate 函数中放置一个消息框,它会弹出两次,并且在第二次之后正确的窗口被捕获并打开我的自定义对话框。

这里是 InspectorWrapper_Deactivate 函数

void InspectorWrapper_Deactivate()
{
    _showOwnDialogOnActivate = false;

    // If there is an invisible ghost Window out there - close it
    if (_hWndInvisibleWindow != IntPtr.Zero) WinApiProvider.SendMessage(_hWndInvisibleWindow, WinApiProvider.WM_SYSCOMMAND, WinApiProvider.SC_CLOSE, 0);

    IntPtr hBuiltInDialog = WinApiProvider.FindWindow("#32770", "");
    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);

        //MessageBox.Show("Contact");

        // now check some criteria to identify the build in dialog..
        int languageId = Inspector.Application.LanguageSettings.get_LanguageID(Microsoft.Office.Core.MsoAppLanguageID.msoLanguageIDUI);
        switch (languageId)
        {
            case 1031:
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                // !!! This part is only valid for German Outlook 2007 Version !!!
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                if (!childWindowNames.Contains("Nur N&ame")) return;
                if (!childWindowNames.Contains("&Mehr Spalten")) return;
                if (!childWindowNames.Contains("A&dressbuch")) return;
                // you can even check more criteria
                break;

            case 1033:
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                // !!! This part is only valid for english Outlook 2007 Version !!!
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                if (!childWindowNames.Contains("&Name only")) return;
                if (!childWindowNames.Contains("Mo&re columns")) return;
                if (!childWindowNames.Contains("A&ddress Book")) return;
                break;

            case 1040:
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                // !!! This part is only valid for italian Outlook 2007 Version !!!
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                if (!childWindowNames.Contains("Solo n&ome")) return;
                if (!childWindowNames.Contains("Altre &colonne")) return;
                if (!childWindowNames.Contains("R&ubrica")) return;
                break;
            // TODO: place your language here....

            default:
                return;
        }

        // OK - we have the built in Recipient Dialog
        // Create a new invisible window
        _hWndInvisibleWindow = WinApiProvider.CreateWindowEx(0, "Static", "BriaSOFT", 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); 

        // use this window as new Parent for the original Dialog
        WinApiProvider.SetParent(hBuiltInDialog, _hWndInvisibleWindow);

        WinApiProvider.SendMessage(hBuiltInDialog, WinApiProvider.WM_SYSCOMMAND, WinApiProvider.SC_CLOSE, 0);
        // When our INspector becomes active again, we should show our own Dialog
        _showOwnDialogOnActivate = true;
    }
}

任何建议都会非常感激。 谢谢 弗拉维奥

遵循德米特里的建议 但仍然无法找到 32770 窗口,因为它的窗口名称为空(即使它存在)

    string windowName;
    IntPtr outlookHandle = (IntPtr)0;
    IOleWindow window = Inspector as IOleWindow;
    if (window != null)
    {
        window.GetWindow(out outlookHandle);
        List<IntPtr> subWindows = WinApiProvider.EnumChildWindows(outlookHandle);
        foreach (IntPtr hand in subWindows)
        {
            StringBuilder ClassName = new StringBuilder(256);
            int nRet = WinApiProvider.GetClassName(hand, ClassName, ClassName.Capacity);
            if (nRet != 0)
            {
                if (ClassName.ToString().Contains("#32770"))
                {
                    windowName = WinApiProvider.GetWindowName(hand);
                    if (windowName.Contains("Seleziona nomi"))
                    {
                        hBuiltInDialog = hand;
                        break;
                    }
                }
            }
        }
    }

【问题讨论】:

  • 这是非常典型的 Codeproject.com 代码,它只能在完全偶然的情况下正常工作。你必须把它扔掉。使用 System.Windows.Automation 命名空间,有一点学习曲线,但投资非常值得,将来会得到丰厚的回报。

标签: visual-studio-2015 vsto outlook-addin outlook-2010 findwindow


【解决方案1】:

不使用FindWindow,您需要找到托管该控件的 Outlook 窗口(将 Inspector 转换为 IOleWindow 和 cal lIOelWindow.GetWindow),然后查找该窗口的子窗口 (EnumChildWindows ) 具有给定的类名/标题/等。如果有多个具有该类名的窗口,您可能需要使用父/子/对等窗口的唯一组合以确保您拥有正确的窗口。

【讨论】:

  • 您好 Dmitry 非常感谢您的回复,我已按照您的说明进行操作,但循环遍历列表未找到正确的窗口,标题似乎为空,我要测试一下获取窗口名称函数
  • 先玩 Spy++ 以查看正确的窗口层次结构。
  • Using Spy++ 我看到我要搜索的窗口在桌面根目录下,问题似乎是当调用inspector的deactivate函数时,列表中仍然没有窗口
  • 在我的环境中,32770 是 AfxWndW 窗口的子窗口。
  • 在客户的环境中只有 32769(桌面)的孩子,我试图打印所有窗口的列表,但在列表中我找不到我正在寻找的 32770。
【解决方案2】:

经过几天的测试和尝试,解决方案(微软建议)是插入一个单独的线程,从检查器启动并在找到窗口时自动终止。

inspector_deactivate 内部

    if (workerThread.ThreadState == ThreadState.Unstarted)
    {
        // Start the worker thread.
        workerThread.Start();

        while (!workerThread.IsAlive) ;

        // Put the main thread to sleep for 1 millisecond to
        // allow the worker thread to do some work:
        Thread.Sleep(1);
    }
    else if (!workerThread.IsAlive)
    {
        workerObject = new Worker(Inspector);
        workerThread = new Thread(workerObject.DoWork);
        workerThread.Start();
    }

并在线程内

   public Worker(Outlook.Inspector insp)
    {
        Inspector = insp;
        _shouldStop = false;
    }

    public void DoWork()
    {
        while (!_shouldStop)
        {
            SubstituteWindow();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2012-07-23
    相关资源
    最近更新 更多