我想只打开一个同样的窗口,条件是搜索到了名字相同的窗口时,将焦点给窗口,否则打开! foreach (Form Form in )
{
if (Form.Name == "与好友" + sbFriends.SeletedItem.Tag.ToString() + "聊天中")
{
Form.Activate();
return;
}
}那个in后面应该填什么呢???
{
if (Form.Name == "与好友" + sbFriends.SeletedItem.Tag.ToString() + "聊天中")
{
Form.Activate();
return;
}
}那个in后面应该填什么呢???
[DllImport("User32.dll",EntryPoint="FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
通过句柄来获取窗口对象,然后才能使用这个对象。
/// 不重复打开MDI子窗体
/// 调用:this.OpenMDIWindow(typeof(窗体名).ToString());
/// </summary>
/// <param name="ChildTypeString">窗体名称(typeof(FormName).ToString())</param>
private void OpenMDIWindow(string ChildTypeString)
{
Form myChild = null;
if (!ContainMDIChild(ChildTypeString))
{
// Get current process assembly
Assembly assembly = Assembly.GetExecutingAssembly();
// Create data type using type string
Type typForm = assembly.GetType(ChildTypeString);
// Create object using type's "InvokeMember" method
Object obj = typForm.InvokeMember(
null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance,
null,
null,
null);
// Show child form
if (obj != null)
{
this.Refresh();
//Form frmTip = LoadTip.ShowTipForm(this, "正在加载窗体"); myChild = obj as Form;
myChild.MdiParent = this;
myChild.WindowState = FormWindowState.Maximized;
myChild.Show();
myChild.Focus(); //frmTip.Close(); }
}
} /// <summary>
/// Search mdi child form by specific type string
/// </summary>
/// <param name="ChildTypeString"></param>
/// <returns></returns>
private bool ContainMDIChild(string ChildTypeString)
{
Form myMDIChild = null;
foreach (Form f in this.MdiChildren)
{
if (f.GetType().ToString() == ChildTypeString)
{
// found it
myMDIChild = f;
break;
}
}
// Show the exist form
if (myMDIChild != null)
{
myMDIChild.TopMost = true;
myMDIChild.Show();
myMDIChild.Focus();
return true;
}
else
{
return false;
}
}
调用:
this.OpenMDIWindow(typeof(窗体名).ToString());
{
if (Application.OpenForms[i].Tag != null)
{
if (Application.OpenForms[i].Tag.ToString() == this.sbFriends.SeletedItem.Tag.ToString())
{
Application.OpenForms[i].Focus();
return;
}
}
}
ChatForm.FriendId = (int)sbFriends.SeletedItem.Tag;
ChatForm cf = new ChatForm();
cf.getMainForm(this);
cf.Show();