从电话发送(请更正我的格式,通过电话需要几个小时)
尝试检查窗口是否可见或任务是否正在运行,如:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace yournamespace
{
class FormCheck
{
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, StringBuilder lParam);
const int WM_GETTEXT = 0x000D;
const int WM_GETTEXTLENGTH = 0x000E;
public static bool IsRunning(string window, bool exactfit)
{ return Check(window,exactfit); }
public static bool IsRunning(string window)
{ return Check(window); }
public static bool Check(string Processname)
{
Process[] pname = Process.GetProcessesByName(Processname);
if (pname.Length <= 1) { return false; } else { return true; }
}
public static bool Check(string WindowTitle, bool exactfit)
{
bool response = false;
string[] strWindowsTitles = EnumerateOpenedWindows.GetDesktopWindowsTitles();
string strResponse = "";
foreach (string strTitle in strWindowsTitles)
{
if (strTitle.Contains(WindowTitle))
{
if (exactfit)
{
if (strTitle == WindowTitle)
{
strResponse = strTitle;
break;
}
}
else
{
if (strTitle.Contains(WindowTitle))
{
strResponse = strTitle;
break;
}
}
}
}
string pid = string.Empty;
if (strResponse != "")
{
response = true;
}
else { response = false; }
return response;
}
}
public class EnumerateOpenedWindows
{
const int MAXTITLE = 255;
private static List<string> lstTitles;
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumDesktopWindows(IntPtr hDesktop,
EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int _GetWindowText(IntPtr hWnd,
StringBuilder lpWindowText, int nMaxCount);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
{
string strTitle = GetWindowText(hWnd);
if (strTitle != "" & IsWindowVisible(hWnd)) //
{
lstTitles.Add(strTitle);
}
return true;
}
/// <summary>
/// Return the window title of handle
/// </summary>
/// <param name="hWnd"></param>
/// <returns></returns>
public static string GetWindowText(IntPtr hWnd)
{
StringBuilder strbTitle = new StringBuilder(MAXTITLE);
int nLength = _GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
strbTitle.Length = nLength;
return strbTitle.ToString();
}
/// <summary>
/// Return titles of all visible windows on desktop
/// </summary>
/// <returns>List of titles in type of string</returns>
public static string[] GetDesktopWindowsTitles()
{
lstTitles = new List<string>();
EnumDelegate delEnumfunc = new EnumDelegate(EnumWindowsProc);
bool bSuccessful = EnumDesktopWindows(IntPtr.Zero, delEnumfunc, IntPtr.Zero); //for current desktop
if (bSuccessful)
{
return lstTitles.ToArray();
}
else
{
// Get the last Win32 error code
int nErrorCode = Marshal.GetLastWin32Error();
string strErrMsg = String.Format("EnumDesktopWindows failed with code {0}.", nErrorCode);
throw new Exception(strErrMsg);
}
}
}
}
通过使用“正在运行”的方法之一,例如:
int waiter = 0;
while (isRunning("Control")
{
//add 1 to an value and set to zero after x counts
waiter++;
if (waiter == 1000) { waiter=0; }
}
您的程序将卡住,直到控制关闭。如果控件永远不会再次关闭,则应插入转义序列;)