假设我多次打开同一个表单,但我只想控制其中一个(一个 fe 具有“hello”作为窗口标题(文本) <- 来识别)我该如何做到这一点?编辑:这是我想做的一个例子(它有点复杂,我不擅长解释我想要什么)private void openthesecformfirst_Click(object sender, EventArgs e){ Form2 sec = new Form2(); sec.Text = "Hi"; sec.Show(); //The second form is now opened}private void openthesecformsecond_Click(object sender, EventArgs e){ Form2 sec = new Form2(); sec.Text = "Hello"; sec.Show(); //the second form is now opened twice}private void changelabelinfirst_Click(object sender, EventArgs e){ //Identified by the title the first opened form2 is supposed to change a label text //How do I get this one specifically?}private void changelabelinsecond_Click(object sender, EventArgs e){ //Identified by the title the second opened form2 is supposed to change a label text //How do I get this one specifically?}
2 回答
婷婷同学_
TA贡献1844条经验 获得超8个赞
对于 OS Windows 中的查找窗口,您可以使用FindWindowExWin32 Api,例如:
因为这是原始的不安全代码,您应该从 user32.dll 导入函数:
[DllImport("user32.dll", SetLastError = true)] static extern IntPtr
FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr
childAfter,
string className, string windowTitle);
导入后,您可以使用这样的功能:
var CaptionTextForLooking = "hello"; // or "Hi"
var foundWindowPtr =
FindWindowEx(IntPtr.Zero,IntPtr.Zero,CaptionTextForLooking
,IntPtr.Zero);
- 2 回答
- 0 关注
- 113 浏览
添加回答
举报
0/150
提交
取消