以编程方式从add in访问visual studio查询结果窗格

时间:2013-05-28 10:52:18

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

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

我不是指菜单 - 数据 - Transact SQL Editor中的查询窗口 - 新查询。我在想从Server explore - 扩展一些数据库 - 展开表,右键单击表并选择新查询。选择要包含的表后,检查几列并选择执行。新窗口将打开。那个窗口是我所知道的Document类,但是不能从该类获取控件或窗格甚至面板的内容。在底部,您可以看到包含行和列的窗格。

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

我已经为VS构建了插件,但无法从该网格中检索单元格的值。尝试使用反射,但无法找到问题的底部,甚至是这个窗格或网格控件的位置。

1 个答案:

答案 0 :(得分:0)

    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.
            }
        }

}