【发布时间】: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