询问者
C#如何获得MDI子窗口的句柄?

问题
-
如何获得MDI子窗口的句柄?
现在用 FindWindow可以获得主窗口句柄,
IntPtr maindHwnd = FindWindow(null, Form1.frm1.Text); //获得主窗口句柄
但再去获子窗口句柄时始终不成功
IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null,"子窗口名"); //获得次窗口句柄
我网上查了一下原因,是因为主窗口和子窗口属于不同的类, 所以不能用获得的主窗口句柄mainHwnd基础上再去获得子窗口句柄.然后我搜遍了网友提供有方法, 无外不是用SPY++就是用EnumWindows枚举去获取, 写得很乱,而且很多是相互抄的,没点实际的例子.能提供一下清晰可行的吗?
全部回复
-
你好,
我还没试,你的意思是以下的代码不work?
static IntPtr FindWindowByIndex(IntPtr hWndParent, int index) { if (index == 0) return hWndParent; else { int ct = 0; IntPtr result = IntPtr.Zero; do { result = FindWindowEx(hWndParent, result, "Button", null); if (result != IntPtr.Zero) ++ct; } while (ct < index && result != IntPtr.Zero); return result; } }
Barry
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
IntPtr maindHwnd = FindWindow(null, "主窗口的Text名字"); //获得主窗口句柄
if (maindHwnd != IntPtr.Zero)
{
IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "Button"); //获得Button的句柄
if (childHwnd != IntPtr.Zero)
MessageBox.Show("找到Button的句柄");
}你好, 这样是可以获得Button按键的句柄的,甚至换成label等控键都可以获得它的句柄, 但如果换成子窗口就获取不到了,因为主窗口和子窗口属于不同的类, 所以不能用获得的主窗口句柄mainHwnd基础上再去获得子窗口句柄.
-
你可以用 this.MdiChildren 找出所有 MDI子窗口
大家一齊探討、學習和研究,謝謝!
MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP, MCPD,
MCT, Microsoft Community Star(TW & HK),
Microsoft MVP for VB.NET since 2003
My MSMVP Blog
請記得將對您有幫助的回覆標示為解答以幫助其他尋找解答及參與社群討論的朋友們。
Please remember to clickMark as Answer on the post that helps you. This can be beneficial to other community members reading the thread. -
privatevoid AddButtonsToMyChildren() { // If there are child forms in the parent form, add Button controls to them. for (int x =0; x < this.MdiChildren.Length;x++) { // Create a temporary Button control to add to the child form. Button tempButton = new Button(); // Set the location and text of the Button control. tempButton.Location = new Point(10,10); tempButton.Text = "OK"; // Create a temporary instance of a child form (Form 2 in this case). Form tempChild = (Form)this.MdiChildren[x]; // Add the Button control to the control collection of the form. tempChild.Controls.Add(tempButton); } }
兄弟,上面这代码表示看不懂吖,添加Button后又如何调用它获得句柄呢,我必须要得到子窗口的句柄,然后调用SetWindowLong(childHwnd, -16, 369164288);这个API作其它处理.
-
Form tempChild = (Form)this.MdiChildren[x];
主要是這句,告訴你可以這樣抓出其中一個MdiChild成form類,成form 類後,你可以找出form.Text去看這是不是你要找的 句柄,如果是,那這Form就是你要找那個
如這樣,
foreach (Form frmChild in this.MdiChildren) { if (frmChild.Text == "你要找的句柄") frmChild.SetWindowLong(childHwnd, -16, 369164288); }
大家一齊探討、學習和研究,謝謝!
MCSD, MCAD, MCSE+I, MCDBA, MCDST, MCSA, MCTS, MCITP, MCPD,
MCT, Microsoft Community Star(TW & HK),
Microsoft MVP for VB.NET since 2003
My MSMVP Blog
請記得將對您有幫助的回覆標示為解答以幫助其他尋找解答及參與社群討論的朋友們。
Please remember to clickMark as Answer on the post that helps you. This can be beneficial to other community members reading the thread.