[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int b(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern int a(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
//点击picturebox事件触发 窗体打开
private void pictureBox2_Click(object sender, EventArgs e)
{
OA fm = new OA();
if (Context.IsHasOpen("OA"))
{
a(b(null, "OA"), -1, 0, 0, 0, 0, 0x001 | 0x002 | 0x040);
a(b(null, "Test2"), -2, 0, 0, 0, 0, 0x001 | 0x002 | 0x040);
}
else
{
if (Context.OpenForm.Length > 0)
{
Context.OpenForm += ",";
}
Context.OpenForm += "OA";
fm.Show();
}
}
private void pictureBox3_Click(object sender, EventArgs e)
{
Test2 f = new Test2();
if (Context.IsHasOpen("Test2"))
{
a(b(null, "Test2"), -1, 0, 0, 0, 0, 0x001 | 0x002 | 0x040);
a(b(null, "OA"), -2, 0, 0, 0, 0, 0x001 | 0x002 | 0x040);
}
else
{
if (Context.OpenForm.Length > 0)
{
Context.OpenForm += ",";
}
Context.OpenForm += "Test2";
f.Show();
}
}
//Content 类
public class Context
{
public static string OpenForm = "";
//记录打开窗体名称
public static bool IsHasOpen(string newForm)
{
bool bR = false;
string[] arrylist = OpenForm.Split(',');
if (arrylist.Length > 0)
{
foreach (string str in arrylist)
{
if (newForm == str)
{
bR = true;
break;
}
else
{
bR = false;
}
}
}
return bR;
}
//窗体关闭触发此事件,清除对应窗体名称
public static string DeleteOpenFrom(string strForm)
{
string[] arrylist = OpenForm.Split(',');
if (arrylist.Length > 0)
{
OpenForm = "";
foreach (string str in arrylist)
{
if (strForm == str)
{
}
else
{
if (OpenForm.Length <= 0)
{
OpenForm += str;
}
else
{
OpenForm += "," + str;
}
}
}
}
return OpenForm;
}
}
本文介绍了一个使用C#实现的点击事件触发的窗体打开与关闭管理功能,通过`FindWindow`、`SetWindowPos`等API进行窗口操作,实现了对特定窗体的快速切换和关闭逻辑。
2740

被折叠的 条评论
为什么被折叠?



