In the Image Insertion sample, we're using Adornments to place an image in a code file. The image is never actually added to the code file (the code file is text). It is simply shown as if it were added. To add the image, you have to drop it on the code file (either by dragging it from the Solution Explorer or from a Windows Explorer). To get a notification of Drag & Drop events, IDropHandlerProvider must be implemented (and exported). This interface has a single method (GetAssociatedDropHandler) that provides an IWpfTextView and expects an IDropHandler to be returned. The IDropHandler is where all the hovering, dragging and dropping code will be found. We're supporting two DropFormats (represented as attributes in the Provider class): CF_VSSTGPROJECTITEMS is the format for drop items from the Solution Explorer, and FileDrop is the standard format for dropping files (used by Windows Explorer and others).

When an image is being dragged over the surface of the code file, the HandleDraggingOver method in the IDropHandler is called. At this point we show 2 adornments: a PreviewImageAdornment (that shows the actual image) and a HighlightAdornment, which highlights the line where the image will be anchored. Adornments in Visual Studio are graphical ways to enhance the information given by a view. For instance, the dots that appear on the code when View White Space is selected are not actually in the file, those are simply an adornment being shown over the code. In the same way, the Image on Image Insertion is not in the file, but it's being shown over it.

Adornments in Visual Studio are WPF Controls. These controls are placed on Layers. The Layers allows developers to place controls in different positions on the z-axis. For instance, brace matching may be implemented as an adornment, placed on a layer that shows over the text in the code editor. The same thing can be done with the red highlighting for breakpoints. In Image Insertion we're using a default layer ("Intra Text Adornment"), but you can easilly provide your own by exporting an AdornmentLayerDefinition.

The line in which the image will be dropped must be extended in height. With the ILineTransformSourceProvider we provide a ILineTransformSource that returns a transformation for the line in which the Image is dropped.

Last edited Nov 30, 2009 at 7:52 PM by joj, version 1

Comments

No comments yet.