查找窗体是否已经创建,常用于避免弹出同一个Form。
2005下使用:
/// <summary>
/// 查找是否已创建窗体
/// </summary>
/// <param name="FormName">窗体Name</param>
/// <returns></returns>
public static bool SearchAllForm(string FormName)
{
for (int i = 0; i < Application.OpenForms.Count; i++)
{
if (Application.OpenForms[i].Name == FormName)
{
return true;
}
}
return false;
}
2003下使用:
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")] //找子窗体
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")] //用于发送信息给窗体
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
/// <summary>
/// 查找是否已创建窗体
/// </summary>
/// <param name="FormTitle">标题名称</param>
/// <returns></returns>
public static bool SearchForm(string FormTitle)
{
IntPtr ParenthWnd = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);
//查到窗体,得到整个窗体
ParenthWnd = FindWindow(null,FormTitle);
if (!ParenthWnd.Equals(IntPtr.Zero))
{
return true;
}
else
{
return false;
}
}