代码之家  ›  专栏  ›  技术社区  ›  Adi

在C中获取窗口句柄#

  •  24
  • Adi  · 技术社区  · 16 年前

    我宣布了以下课程:

    public partial class MainWindow : Window
    

    一旦窗户有了,我就需要得到窗户的实际把手。 我该如何做以及应该将查询函数放在哪里。

    到目前为止,我尝试的是:

    IntPtr hwnd = new WindowInteropHelper(this).Handle;
    

    但我得到的句柄是0,这可能是因为它被植入了OnInitialized中——也许窗口在那个阶段还没有准备好。 而且,是的-它是通过WPF连接的,谢谢您指出!

    谢谢

    3 回复  |  直到 12 年前
        1
  •  26
  •   SliverNinja - MSFT    12 年前

    OnInitialized 方法 手柄 尚未创建。但你走对了。如果你把你的电话 Loaded 事件 手柄 将被创建,它应返回正确的 手柄 .

        2
  •  6
  •   Nir    16 年前

    最早可以拿到把手的地方是在源代码中初始化的

        3
  •  0
  •   Mez    16 年前
     [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
            public static extern int FindWindowEx(int hwndParent, int hwndEnfant, int lpClasse, string lpTitre);
    
    
    int hwnd = FindWindowEx(0, 0, 0, title);//where title is the windowtitle
    
                    //verification of the window
                    if (hwnd == 0)
                    {
                        throw new Exception("Window not found");
                    }