zoukankan      html  css  js  c++  java
  • WPF中获取Hwnd与窗体,Uid获取控件


    void MapControl_Loaded(object sender, RoutedEventArgs e)
            {
                this.OnApplyTemplate();
                CurrentMapChanged(new DependencyPropertyChangedEventArgs(MapControl.PropertyCurrentMap, "", ""));
    
                //
                Window parentWindow = Window.GetWindow(this);
                IntPtr handle = (new WindowInteropHelper(parentWindow)).Handle;
                Console.WriteLine("parentWindow WHnd: " + handle.ToInt32());
    
                HwndSource hwndSource = HwndSource.FromHwnd(handle);
                Visual visual = hwndSource.RootVisual;
    
                Window window = (Window)visual;
                Console.WriteLine("parentWindow WHnd: " + window.Title);
                //
                String mapUid = Guid.NewGuid().ToString();
                this.Uid = mapUid;
                //
                Console.WriteLine("this control uid: " + this.Uid);
                UIElement elem = this.FindUIElementByUid(window, mapUid);
                if(elem != null)
                {
                    Console.WriteLine("this control uid: " + elem.Uid);
                }
                
    
            }
    
    
            private UIElement FindUIElementByUid(DependencyObject parent, string uid)
            {
                int count = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < count; i++)
                {
                    UIElement el = VisualTreeHelper.GetChild(parent, i) as UIElement;
                    if (el == null) continue;
                    Console.WriteLine("el.Uid: " + el.Uid);
                    if (el.Uid == uid) { return el; }
                    UIElement el1 = FindUIElementByUid(el, uid);
                    if (el1 != null) { return el1; }
                }
                //
                if (parent is ContentControl)
                {
                    UIElement content = (parent as ContentControl).Content as UIElement;
                    if (content != null)
                    {
                        Console.WriteLine("content.Uid: " + content.Uid);
                        if (content.Uid == uid) return content;
                        UIElement el1 = FindUIElementByUid(content, uid);
                        if (el1 != null) { return el1; }
                    }
                }
    
                return null;
            }


    -----------------------------------

  • 相关阅读:
    39)自定义String类(没看)
    37)智能指针(就是自动delete空间)
    36)友元函数和重载操作符 (没看) 可以看视频
    35)类和结构体类比---this
    34)static 静态成员和静态成员函数
    33)new和delete关键字
    32)(典型)构造和析构 拷贝构造 小样例
    31)类中成员初始化
    30)构造函数的初始化列表
    29)深拷贝和浅拷贝
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/11121559.html
Copyright © 2011-2022 走看看