比如说 。 我们用这个方法去判断窗口的打开 [DllImport("USER32.DLL")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);private void button1_Click(object sender, EventArgs e)
{
IntPtr ptr= FindWindow(null, "Form1");
if (ptr!= IntPtr.Zero)
{
MessageBox.Show("窗体已打开!");
}
else
{
Form1 f1 = new Form1();
f1.Owner = this;
f1.Show();
}
} 但是 。在QQ聊天 中我们又要怎么去判断呢 ?
如果按这个方法 。 是可以达到一个窗口, 但别人发信息你也不可以打开另一个窗口、
这种情况下 你要怎么办 ?? 有什么方法可以用 QQ的ID来判断 窗口么????
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);private void button1_Click(object sender, EventArgs e)
{
IntPtr ptr= FindWindow(null, "Form1");
if (ptr!= IntPtr.Zero)
{
MessageBox.Show("窗体已打开!");
}
else
{
Form1 f1 = new Form1();
f1.Owner = this;
f1.Show();
}
} 但是 。在QQ聊天 中我们又要怎么去判断呢 ?
如果按这个方法 。 是可以达到一个窗口, 但别人发信息你也不可以打开另一个窗口、
这种情况下 你要怎么办 ?? 有什么方法可以用 QQ的ID来判断 窗口么????
用这个QQ号码为key和这个窗体的句柄为Value,
比如:
private static Hashtable winhand = new Hashtable();
public static Hashtable WinHand
{
get
{
return (winhand);
}
set
{
winhand = value;
}
}
例:WinHand.Add(QQ号码,句柄);
当你点击QQ头像时,去检查WinHand是否含有这个QQ号码的窗体。如果有就
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
ShowWindow((IntPtr)TMessage.WinHand[QQ号码], 1);
没有就
Form QQ=new Form()
QQ.Show();
WinHand.Add(QQ号码,句柄);
上面是打个比方。差不多是这样子。
举个例子,你的主程序可以声明一个变量: Dictionary<string,Form> Windows=new Dictionary<string,Form>();其string类型的索引键保存窗口的目标内容(例如对方的QQ号码),每当加载一个Form就放入这个Windows集合中,每当关闭窗口(窗口的Closed事件触发)就从这个集合中删除,你随时就可以查找某个目标的已经打开的Form了。