【问题标题】:Programmatically access visual studio query results pane from add in以编程方式从插件访问 Visual Studio 查询结果窗格
【发布时间】:2013-05-28 10:52:18
【问题描述】:

有没有办法从 Addin 访问 Visual Studio 的查询结果窗格?

我不是指菜单 - 数据 - 事务 SQL 编辑器 - 新查询中的查询窗口。我正在考虑从服务器探索 - 扩展一些数据库 - 展开表,右键单击表并选择新查询。选择将包含哪些表后,检查几列并选择执行。新窗口将打开。该窗口是我知道的 Document 类,但无法从该类中获取控件或窗格甚至面板的内容。在底部,您可以看到一个包含行和列的窗格。

如果可能,我需要访问该窗格或网格控件。或者甚至更好地通过反射检索该查询结果窗口中选定单元格的单元格值?

我已经为 VS 构建了插件,但是无法从该网格中检索单元格的值。尝试了反射,但无法深入了解此窗格或网格控件所在的问题,甚至是所在的位置。

【问题讨论】:

    标签: c# visual-studio visual-studio-addins


    【解决方案1】:
        private static IEnumerable<IntPtr> GetChildWindows(IntPtr parent)
        {
            List<IntPtr> result = new List<IntPtr>();
            GCHandle listHandle = GCHandle.Alloc(result);
            try
            {
                NativeMethods.EnumWindowProc childProc = new NativeMethods.EnumWindowProc(EnumWindow);
                NativeMethods.EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
            }
            finally
            {
                if(listHandle.IsAllocated)
                {
                    listHandle.Free();
                }
            }
    
            return result;
        }
    
        /// <summary>
        /// Callback method to be used when enumerating windows.
        /// </summary>
        /// <param name="handle">Handle of the next window</param>
        /// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
        /// <returns>True to continue the enumeration, false to bail</returns>
        private static bool EnumWindow(IntPtr handle, IntPtr pointer)
        {
            GCHandle gch = GCHandle.FromIntPtr(pointer);
            List<IntPtr> list = gch.Target as List<IntPtr>;
            if(list == null)
            {
                throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
            }
    
            list.Add(handle);
    
            // You can modify this to check to see if you want to cancel the operation, then return a null here
            return true;
        }
    
    private DataGridView Grid()
    {
    IEnumerable<IntPtr> a = GetChildWindows(handle);
                foreach(IntPtr b in a)
            {
                Control c = Control.FromHandle(b);
                if(c == null)
                {
                    continue;
                }
    
                try
                {
                    if(c.Name != "DataGridView")
                    {
                        continue;
                    }
    
                    DataGridView dv = c as DataGridView;
                    if(dv != null)
                    {
                        return dv;
                    }
                }
                catch
                {
                    // It is safe to suppress errors here.
                }
            }
    

    }

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      相关资源
      最近更新 更多