实际上,在查看了上面发布的 jlvaquero 代码后,我在同一个站点上发现了以下内容
EnumDesktopWindows (user32)
我在我的 laucher 脚本中添加了以下 using 语句:
using System.Text;
using System.Runtime.InteropServices;
然后我将以下类添加到我的启动器脚本中
/// <summary>
/// EnumDesktopWindows Demo - shows the caption of all desktop windows.
/// Authors: Svetlin Nakov, Martin Kulov
/// Bulgarian Association of Software Developers - http://www.devbg.org/en/
/// </summary>
public class user32
{
/// <summary>
/// filter function
/// </summary>
/// <param name="hWnd"></param>
/// <param name="lParam"></param>
/// <returns></returns>
public delegate bool EnumDelegate(IntPtr hWnd, int lParam);
/// <summary>
/// check if windows visible
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsWindowVisible(IntPtr hWnd);
/// <summary>
/// return windows text
/// </summary>
/// <param name="hWnd"></param>
/// <param name="lpWindowText"></param>
/// <param name="nMaxCount"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);
/// <summary>
/// enumarator on all desktop windows
/// </summary>
/// <param name="hDesktop"></param>
/// <param name="lpEnumCallbackFunction"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
}
然后我在我的启动脚本中添加了以下函数来调用新类并进行处理以发现活动窗口
/// <summary>
/// Checks if application window open.
/// </summary>
/// <returns></returns>
private static bool IfApplicationWindowOpen(string windowName)
{
List<string> collection = new List<string>();
user32.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
{
StringBuilder strbTitle = new StringBuilder(255);
int nLength = user32.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
string strTitle = strbTitle.ToString();
if (user32.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
{
collection.Add(strTitle);
}
return true;
};
if (user32.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero))
{
foreach (string item in collection)
{
if (item.ToString().Equals(windowName))
{
return true;
break;
}
}
}
return false;
}
最后我修改了我的启动功能以包括对活动窗口的检查
/// <summary>
/// Starts the new customer.
/// </summary>
/// <param name="param">The param.</param>
public static void StartNewCustomer(Parameter param)
{
string windowName = "New Customer";
if (!IfApplicationWindowOpen(windowName))
{
GlobalFactory globalfactory = param.GlobalFactory;
try
{
Generic objNewCustomer = new Generic();
objNewCustomer.StartNewCustomerFromCustomer(param);
}
catch (TypeInitializationException tx)
{
globalfactory.ErrorHandler.Log(tx, (int)msmsError.ErrorSeverity.Major | (int)msmsError.ErrorSeverity.User);
}
catch (Exception ex)
{
globalfactory.ErrorHandler.Log(ex, (int)msmsError.ErrorSeverity.Major | (int)msmsError.ErrorSeverity.User);
}
}
else
{
MessageBox.Show("The application " + windowName + " is already open", windowName + ": Launch Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
希望这可以帮助其他有同样问题的人
问候,
漫画编码器