0

VsPackage 2010 - Accessing underlying UserControl inside a Custom Editor using IVsWindowFrame

by Syed Aziz ur Rahman 12. October 2012 17:07

By using the sample I mentioned in my previous post here (VsPackage 2010 - Custom Tool Window using IVsHierarchy and IVsUIHierarchy ), I was able to make a custom tool window and a custom editor by Implementing IVsUIHierarchy and IVsHierarchy. Now on an UI event in my custom tool window e.g. double click, I am opening my custom Editor by calling "OpenSpecificEditor". As you know, OpenSpecificEditor method has a out parameter (handle) of IVsWindowFrame which you can use to show the editor. It is working fine and editor is getting opened. A call would be like this.

private IVsWindowFrame OpenEditor(HierarchyTreeNode selectedItem)
{
	IVsUIShellOpenDocument openDoc = (IVsUIShellOpenDocument)GetService(typeof(SVsUIShellOpenDocument));
	IVsWindowFrame editorFrame;
	Guid guidEditorType = GuidList.guidEditorFactory;
	Guid guidLogView = VSConstants.LOGVIEWID.Primary_guid;
	IOleServiceProvider spContext = (IOleServiceProvider)GetService(typeof(IOleServiceProvider));
	string moniker = selectedItem.Text;

	int hr = openDoc.OpenSpecificEditor(0, moniker, ref guidEditorType, "",
	ref guidLogView, "Vj Editor - ", (IVsUIHierarchy)this, (uint)selectedItem.ID,
	IntPtr.Zero, spContext, out editorFrame);

	if (editorFrame != null)
	{
		editorFrame.Show();
	}
	return editorFrame;
}

 Similarly when I tried to get a reference of already created editor using "IsDocumentOpen" method, I got an object of same IVsWindowFrame class. Sample code will be like 

private IVsWindowFrame AddEditorIfNotExist(HierarchyTreeNode hNode)
{
    IVsUIHierarchy outHierOwner = null;
    UInt32[] outHierItem = null;
    IVsWindowFrame editorFrame;
    int open;
    Guid guidLogView = VSConstants.LOGVIEWID.Primary_guid;

    IVsUIShellOpenDocument openDoc = (IVsUIShellOpenDocument)GetService(typeof(SVsUIShellOpenDocument));
    int result = openDoc.IsDocumentOpen(this, (uint)hNode.ID, hNode.Text, ref guidLogView, (uint)__VSIDOFLAGS.IDO_ActivateIfOpen,
            out outHierOwner, outHierItem, out editorFrame, out open);

    if (open != 1)
    {
        editorFrame = OpenEditor(hNode);
    }
    return editorFrame;
}

Now I want to call some public methods of the actual user control inside the editor. I also want to hook up with some events in case the editor control wants to send a message to tool window. Searching on google didn't help so I asked this basic question on MSDN VSX forum. The answer I got was quite cryptic but it worked.

private CtrlChatEditor GetChatEditorFromIVsWindowFrame(IVsWindowFrame editorFrame)
{
    if (editorFrame == null) return null;
    object objControl = null;

//the code to get the inner object
 int result = editorFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out objControl);


if (result != VSConstants.S_OK) return null; VjEditorPane pane = objControl as VjEditorPane; if (pane == null) return null; CtrlChatEditor ctrlChatEditor = pane.Window as CtrlChatEditor; return ctrlChatEditor;//might be null }

Therefore my OpenEditor will look like this now.

private IVsWindowFrame OpenEditor(HierarchyTreeNode selectedItem)
{
    //HierarchyTreeNode selectedItem = ctrlToolWindow.GetItem(itemID);
    IVsUIShellOpenDocument openDoc = (IVsUIShellOpenDocument)GetService(typeof(SVsUIShellOpenDocument));
    IVsWindowFrame editorFrame;
    Guid guidEditorType = GuidList.guidEditorFactory;
    Guid guidLogView = VSConstants.LOGVIEWID.Primary_guid;
    IOleServiceProvider spContext = (IOleServiceProvider)GetService(typeof(IOleServiceProvider));
    string moniker = selectedItem.Text;

    int hr = openDoc.OpenSpecificEditor(0, moniker, ref guidEditorType, "",
        ref guidLogView, "Vj Editor - ", (IVsUIHierarchy)this, (uint)selectedItem.ID,
        IntPtr.Zero, spContext, out editorFrame);

    if (editorFrame != null)
    { editorFrame.Show(); 
//call public propreties and hooking up with the events. CtrlChatEditor ctrlChatEditor = GetChatEditorFromIVsWindowFrame(editorFrame); if (ctrlChatEditor != null) { ctrlChatEditor.OnMessageNeedToBeSend += CtrlChatEditor_OnMessageNeedToBeSend; ctrlChatEditor.ClientContact = selectedItem.ClientContact; ctrlChatEditor.BuddyContact = selectedItem.BuddyContact; } } return editorFrame;
}

Hope this will help someone.

 

Tags: ,

VsPackage 2010 | VSX

0

VsPackage 2010 - Custom Tool Window using IVsHierarchy and IVsUIHierarchy

by Syed Aziz ur Rahman 25. September 2012 16:58

Environment: VS2010

I am trying to make an extension with the following requirements

1. Need a tool window like solution explorer.
2. Double click on a node will open a custom editor.
3. The editor will NOT be linked to a file.
4. The extension should work with or without any solution opened.

So you can say it is like Server explorer. I came to know after search that i have to use IVsHierarchy and IVsUIHierarchy etc to implement this. I found some samples for that for vs2008 but was unable to find any basic tutorial/sample for vs2010.

I have downloaded Mr.Ed Dore sample for Visual Studio 2008 from VSX Insider, merged it with default package project created by Visual Studio 2010, made it working and upload it here. Please feel free to use it. HierAnarchy.zip (378.70 kb)  . Ed Dore is working on a sample where we can host our own control in toolwindow instead of using default VsHierarchy window.

Tags: ,

VSX

0

VsPackage 2010 How to host Windows Forms User Control in Visual Studio Tool Window

by Syed Aziz ur Rahman 14. September 2012 15:38

I recently started learning how to extend Visual Studio using packages. I will be posting more about it. Here is the first thing I learned today. Do not mind about the site layout. I will fix it soon.

When we create a new VS Package project with ToolWindow, by default a control MyControl.xaml will be added which will be tied to the ToolWindow. I do not have expertise in WPF and its controls. Therefore I tried to use normal winforms user controls. I added a new usercontrol in the project and changed the code in the MyToolWindow.cs to use that control but all I see is a blank Toolwindow when I debug the project.

//Normal code in auto generated MyToolWindow.cs
base.Content = new MyControl();

//The usercontrol i created is SimpleControl. I have changed the code to.
base.Content = new SimpleControl();

 

Here is the snapshot of the code and solution exploror.

Snapshot of a auto generated project

 

I searched a lot for the simple question. I was not able to get any answer. I knew that it should be very simple. I found another guy asking the same question on stackoverflow here but no answer. Finally posted the question on MSDN forum. Mr. Ed Dore was nice enough to answer it here. I am cross posting it here for more visibility in search engine.

The ToolWindowPane can be used to host WPF content or a Winform control.

For a Winform control, you just need to override the Window property get, and leave the Content property null.

For example:

public MyToolWindow() :  base(null)
    {
        this.Caption = Resources.ToolWindowTitle;
        this.BitmapResourceID = 301;
        this.BitmapIndex = 1;
        control = new MyControl();
    }

     override public System.Windows.Forms.IWin32Window Window
    {
        get
        {
            return (System.Windows.Forms.IWin32Window)control;
        }
    }



I hope this will help someone.

Tags: ,

DotNet | VSX

Powered by BlogEngine.NET 2.5.0.6
Original Design by Laptop Geek, Adapted by onesoft