• 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;
            }


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

  • 相关阅读:
    我的小问题
    js实现随机的四则运算题目
    VC++ 6.0 无法打开文件
    VC6.0致命错误 RC1015: 无法打开包含文件 'afxres.h'.解决方案
    VS2010 如何添加H文件目录和LIB目录
    学习计划(四月)
    IIS与apache的对比:大杂烩
    ubuntu下有线网卡启动(Atheros AR8161 Gigabit Ethernet)
    putty终端乱码解决办法
    Proxy Switchysharp配置
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/11121559.html
Copyright © 2020-2023  润新知