| Comments

(long, informative post with images -- apologies aggregates...click on the link :-))

in outlook 2007, when you have an attachement in a mail message and click on the attachment file, you are presented with an option to preview this attachment directly in outlook...just like the preview pane for a normal message:

previewmode

likewise in vista's file explorers, you can enable a preview pane and get the same functionality for highlighting a file in explorer as seen here:

previewvista

for productivity, namely in outlook 2007 for me, this is a great advancement to the platform.  i now don't have to double-click a file, etc and launch another program to see what usually is just a glance that is required.  this preview mode in the file explorer has existing in windows xp, but was little used by developers.  this is likely due to the warnings and caution from microsoft to not use managed code for this feature.  in winxp, this functionality is executed in process, thus making it a dangerous path when multiple versions of the CLR might be invovled.

in vista, this is much more simplified and the model has changed to out of process...finally making it easier to do these things in managed code.  has an excellent article in the january edition of msdn magazine that covers this entire story and the in's and out's of preview handler development.  the great thing about his article aside from the depth of understanding you will gain is that stephen has created a managed interface to make it easier to implement your own preview handler.  you can download his code from the msdn preview handler code link here.  stephen encourages us to write our own preview handlers, but he's gone and done most of the ones i think we all use -- zip, xml, xaml, resx, csv, msi, internet explorer, etc.  they are all awesome.  the zip preview handler is especially cool since it also implements a treeview representation of the contents of the zip file...look at this sample showing the vista preview, but would be the same in outlook 2007 (note: this zip one also handles .gadget files):

zippvwhandler

one handler that stephen also provides is a pdf preview handler (probably the second one i will use the most next to the zip one).  in his sample, he uses the adobe activex control for the acrobat reader implementation.  now this would require that you have adobe reader installed.  if you are a frequent reader, you may know that i'm not a fan of adobe reader.  i use foxit reader from .  it's lean and mean.  i wanted to ensure i could still use the pdf preview handler, but had to modify a few things.

first i had to get the foxit activex sdk from their site.  there is a license cost to use this, which i'm currently investigating.  after getting the activex control, i modified stephen's code at about line 41 of of the PdfPreviewHandler.cs file in the download.  i changed it to:

// Foxit Reader ActiveX
public PdfAxHost()
    : base("d46a7492-4b6c-446f-8100-4812edf406c9") { }
 

then, i also had to change the pinvoke method.  the adobe control uses a LoadFile() signature, but foxit uses an OpenFile() signature.  foxit also uses a second parameter, the starting page of the document.  so i changed it to this code:

_ocx.GetType().InvokeMember(
  "OpenFile", BindingFlags.InvokeMethod, null, // changed to OpenFile
   _ocx, new object[] { fileName, 0 }, CultureInfo.InvariantCulture); // added 0 parameter to array
 

compiled and deployed...now i have pdf previewing in outlook 2007:

foxitpreview

it's a beautiful thing.  please check out stephen's article in msdn magazine -- it was a great read and well done -- i totally understood the concepts presented and it allowed me to create a few handlers of my own.  as a developer i frequently get code files sent as attachments.  i generally have to open them up and look at them in visual studio or notepad2 for code highlighting, etc.  well, with my new found knowledge i created a code preview handler.  it will see .cs, .vb, .js and .sql files and enable a previewer to show them in code-highlighted formatting in the preview window of outlook 2007 or windows vista explorer.  here's a sample:

codeprievewer

how is this accomplished?  well, aside from the great library stephen provided, i pulled together the HtmlApp code that nikhilk wrote, as well as the csharpformat from manoli and bundled them together to produce this.  i had some issues with manoli's library when trying to set the embed stylesheet property, but i just embedded it as a resource and that was fine by me.  it was easy to implement and here is the magical function code:

public override void Load(FileInfo file)
{
    StreamReader rdr = file.OpenText();
    string previewCode = rdr.ReadToEnd();
    string formattedCode = FormatCode(previewCode, file.Extension);

    HtmlApp.Html.HtmlControl html = new HtmlApp.Html.HtmlControl();
    html.LoadHtml(string.Format("<html><head><style type=\"text/css\">{0}</style><body>{1}</body></html>", SmilingGoat.PreviewHandlers.Properties.Resources.CssString, formattedCode));
    html.Dock = DockStyle.Fill;

    Controls.Add(html);
}


after this (the FormatCode funciton basically just does a switch on the file type and applies the appropriate formatting from manoli's object), the formatted code is rendered to view like the sample above.  it's really easy to implement.

one thing important to note about preview handlers is the fact that only one preview handler can be associated with one file extension and class.  what does that mean? well, simple...for example, you can't have two pdf preview handlers.  well what happens if i install two of them? last one will likely win -- depending on how it is registering itself.  you see, there will only be one registry entry for that file type, so the second one you install will overwrite the settings from the first one.  stephen planned on posting a little utility to see what preview handlers are registered for your machine...check it out to see how things are registering and to actually add additional ones.  for example, if i didn't want code highlighting, i could have easily set the TXT preview handler to also preview the code files mentioned above.  here are the links to my code preview handler:

File iconCode Preview Handler [source]
File iconCode Preview Handler Setup [MSI]

Please enjoy some of these other recent posts...

Comments