locked
Printing in an extension just like in Visual Studio RRS feed

  • Question

  • We have an VS 2008 extension.  The print commands are disabled.  I would have thought there would be a default print implementation that just prints the text.  Anyway, I hooked up the print command.  But rather than coming up with all new code, I'd like to be able to print in a very similar fashion to how VS prints work items - with the same formatting and layout.  Is there a way to hook into this somehow?  The only thing I see that we'd have to customize is a DataGridView that we use.  Is there a way to call into a default print function, and/or supply print overrides for objects or tabs in our design?
    Thursday, August 5, 2010 5:05 PM

Answers

  • You do need to do your own printing if you want to have File.Print functionality. There is no built in print functionity. The Visual Studio text editor handles the command and does its printing. If your editor is WPF or Windows Forms based, you may use the print functionality provided by those technology.

    Another option can be to put your formatted text in the Visual Studio text editor (may be a hidden window) and invoke print on it. Visual Studio Editor forum (http://social.msdn.microsoft.com/Forums/en-US/vseditor) will be a good resource if you want to pursue this option.

    You can also consider having you formatted text in a web browser control and invoke print on it...

     


    http://blogs.msdn.com/saurabhjain
    Friday, August 27, 2010 6:32 AM

All replies

  • Hello,

    Based on my understanding, you want to implement Print Function as Visual Studio do in an Add-in, if so, why don't you reuse Print command in Visual Studio.

    DTE.ExecuteCommand("File.Print","");

    If you mean you want to write your code to implement Print Function as Visual Studio do, I would suggest you posting this question in Windows Forms General Forum or language related Forum, and you will get better answer.

    If I misunderstood you, or you have any comments, please feel free to let me know.

     

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, August 6, 2010 3:54 AM
  • Nancy, the DTE.ExecuteCommand is along the lines of what I'd like to try.  I've already written some code to implement our own Print function, but I'm having some issues with that and I'd like to experiment with something simpler where I don't have to write any of this code.

    I'm maintaining this code, so I don't really understand how the integration was set up.  After reading a little about DTE, I'm now pretty confused as to how to get a reference to it.  In "Mastering Visual Studio .NET" it says

    "The way in which you obtain a reference to the DTE object will depend on what type of code you are writing. Macros just use a global variable provided by the macro environment called <tt>DTE</tt>. Add-ins are passed a reference to this object when VS.NET initializes them. (Wizards, which are discussed in the next chapter, also have access to the DTE object in their script files through a global object called <tt>dte</tt>.)"

    I'm assuming I'm looking for some reference that was passed in since this is an Add-in, but I can't find where this would occur.  I know you can't see our code, but any more hints appreciated :-)

    Friday, August 6, 2010 3:18 PM
  • I tried the following.  The problem is that it got the instance of the Visual Studio that I was running to debug my real VS integration application, so it printed the active document of my debugger rather than of my integrated app.  Is there any way around that?

    EnvDTE.

    DTE myDTE =

    (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.9.0");

    Friday, August 6, 2010 9:59 PM
  • Hi Ekename,

    Do you mean you want to get a reference of Visual Studio in Add-in? If so, you don't need to use GetActiveObject to get it. When you use Visual Studio Add-in project Template to create this Add-in, you can use _applicationObject as DTE object.

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 9, 2010 2:06 AM
  • I don't understand what you mean.  Where is _applicationObject available?
    Monday, August 9, 2010 1:33 PM
  • When we create an Addin by project template in Visual Studio, it has a DTE object, we just need to use it directly, but not use

    System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.9.0");

    to get a DTE instance.

    You can see a definition of _applicationObject in code as following shows:

     private DTE2 _applicationObject;
    _applicationObject = (DTE2)application;

    So we can use _applicationObject as an Visual Studio instance.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, August 10, 2010 2:15 AM
  • I'm sorry, I still don't know what you mean.  Can you give more detail?  Where is "application" defined?
    Tuesday, August 10, 2010 8:33 PM
  • ekename,

    Are you implementing a custom editor or a custom toolwindow on which you are need to enable print support? It will help us determine what kind of commands to override.

    --

    Regards,

    Saurabh


    http://blogs.msdn.com/saurabhjain
    • Proposed as answer by Nancy Shao Thursday, August 12, 2010 8:38 AM
    Tuesday, August 10, 2010 8:43 PM
  • Hello Ekename,

    How are you? Do you make any progress in this issue?

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, August 13, 2010 9:41 AM
  • No, I still have the same question :-)  Where does your "application" variable in your example come from?  To answer Saurabh, I guess we have a custom editor.
    Wednesday, August 18, 2010 2:53 PM
  • Hello Ekename,

    Could you tell us what you want to do? I'm a little confused. Do you mean you are creating a custom Editor, and want to print the conten in this custom Editor in custom style?

    Regards,

    Nancy Shao [MSFT]
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, August 20, 2010 8:27 AM
  • Could you tell us what you want to do? I'm a little confused. Do you mean you are creating a custom Editor, and want to print the conten in this custom Editor in custom style?


    I have already done that.  I now want to try what we're currently discussing, i.e.

    DTE.ExecuteCommand("File.Print","");

    This does work, but it is not printing from the correct instance of Visual Studio, as mentioned above.  You told me how to do it, but you have something in your code example called  "application".  In the line

    _applicationObject = (DTE2)application;

    But you haven't explained to me where "application" comes from.  I don't know what "application" means.

     

    UPDATE: OK, I might have an idea where the confusion stems from here.  I understand there are 2 approaches to extensibility - Visual Studio Add-In, and Visual Studio Package.  I am maintaining a Package solution.  Maybe you are assuming we have an OnConnection method?  I do not see that anywhere in our code.  I suppose that is not used with the Package approach?

    Tuesday, August 24, 2010 2:12 PM
  • The DTE automation interface is a service that you can query for in your VS Package. I thought there was a help topic on this, but all I could find was  'How to: Get References to the DTE and DTE2 Objects' which details retrieving the DTE interface, and that's not what you need.

    If you have a package, all you need to do is call Package.GetGlobalService. For example:

       DTE dte = Package.GetGlobalService(typeof(DTE)) as DTE;

    Sincerely,

     

     


    Ed Dore
    Tuesday, August 24, 2010 3:07 PM
  • That does appear to be the way I should get the DTE object, so thanks for that Ed.

    However, the idea Nancy proposed now doesn't make sense.  This just turns into a recursive call. DTE.ExecuteCommand("File.Print","");

    It seems I can't enable the print command on the Visual Studio drop down menu without either specifying the print event handler, or the print cmd query event handler.  If I specify the cmd query event handler, it requires that I specify the print event handler as well, or else I get a null reference exception.  So I have to provide the print event handler, and if that calls "File.Print", it's infinitely recursive, it seems.


    What am I missing here?  Isn't there a way to enable the Print cmd on the drop down menu, and have it execute the built in "File.Print" command?

    Note: this might have something to do with how I'm referencing EnvDTE to begin with.  There are 10 choices when I go to add the reference to my project, so I don't know if I've got the right one.

    EnvDTE    8.0.0.0

    envdte     8.0.0.0

    EnvDte    8.0.0.0

    EnvDTE80 8.0.0.0

    envdte80 8.0.0.0

    EnvDTE80 8.0.0.0

    envdte90 9.0.0.0

    EnvDTE90 9.0.0.0

    envdte90a 9.0.0.0

    EnvDTE90a 9.0.0.0

    Tuesday, August 24, 2010 3:37 PM
  • You do need to do your own printing if you want to have File.Print functionality. There is no built in print functionity. The Visual Studio text editor handles the command and does its printing. If your editor is WPF or Windows Forms based, you may use the print functionality provided by those technology.

    Another option can be to put your formatted text in the Visual Studio text editor (may be a hidden window) and invoke print on it. Visual Studio Editor forum (http://social.msdn.microsoft.com/Forums/en-US/vseditor) will be a good resource if you want to pursue this option.

    You can also consider having you formatted text in a web browser control and invoke print on it...

     


    http://blogs.msdn.com/saurabhjain
    Friday, August 27, 2010 6:32 AM
  • What you seem to be saying is that VS has print capability inherent only for its known types.  You cannot enable the File/Pritn command for custom types that VS does not inherently recognize.  I'll consider that to be the answer to my question unless I hear otherwise :-)

    Friday, August 27, 2010 1:29 PM
  • yes, implemened a custom editor or a custom toolwindow. Now we need to enable print support. By default it is always disabled. How to make enabled.

    Can give me steps or samples on this.

    Regards,

    Ranga

    Thursday, May 12, 2011 11:49 AM
  • You need to respond to cmdidPrint and say it is enabled.

    Ryan

    Thursday, May 12, 2011 2:19 PM
  •  

    OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService

    ;

    It works for me as per the below code.

    if (null != mcs)

    {

     

    // Create command in tool window

     

    CommandID toolwndCommandID = new CommandID(Guid.Parse("5EFC7975-14BC-11CF-9B2B-00AA00573819"

    ), 27);

     

    MenuCommand menuToolWin = new MenuCommand

    (NewProjectPackagePackage_printdoc, toolwndCommandID);

    menuToolWin.Enabled =

    true

    ;

    mcs.AddCommand(menuToolWin);

    }

    

    

     

    void NewProjectPackagePackage_printdoc(object sender, EventArgs e)

    {

     

    if (CustomPrint != null)

    CustomPrint(

    this, new CustomEventArgs(currenteditorFrame));

    }

    Friday, May 20, 2011 4:10 AM