/// <summary> /// Begin a drag / drop operation /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ActivitiesList_MouseMove( object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (_activitiesList.SelectedItem is ActivityToolboxItem) { ActivityToolboxItem selectedItem = _activitiesList.SelectedItem as ActivityToolboxItem; IDataObject dataObject = SerializeToolboxItem( selectedItem) as IDataObject; DoDragDrop(dataObject, DragDropEffects.All); } } }
/// <summary> /// Perform owner drawing of the toolbox items /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ActivitiesList_DrawItem( object sender, DrawItemEventArgs e) { if (e.Index < 0) { return; } ActivityToolboxItem item = ((ListBox)sender).Items[e.Index] as ActivityToolboxItem; if (item != null) { Graphics graphics = e.Graphics; if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { //draw a border around the selected item graphics.FillRectangle( SystemBrushes.Window, e.Bounds); Rectangle rect = e.Bounds; rect.Width -= 2; rect.Height -= 2; graphics.DrawRectangle(SystemPens.ActiveBorder, rect); } else { //not the selected item, just fill the rect graphics.FillRectangle(SystemBrushes.Window, e.Bounds); } //draw the toolbox item image Int32 bitmapWidth = 0; if (item.Bitmap != null) { graphics.DrawImage(item.Bitmap, e.Bounds.X + 2, e.Bounds.Y + 2, item.Bitmap.Width, item.Bitmap.Height); bitmapWidth = item.Bitmap.Width; } //add the display name graphics.DrawString(item.DisplayName, e.Font, SystemBrushes.ControlText, e.Bounds.X + bitmapWidth + 2, e.Bounds.Y + 2); } }
public static Activity[] DeserializeActivitiesFromDataObject(IServiceProvider serviceProvider, IDataObject dataObj, bool addReference) { IDesignerHost designerHost = (IDesignerHost)serviceProvider.GetService(typeof(IDesignerHost)); if (designerHost == null) { throw new InvalidOperationException("IDesignerHost is missing."); } if (dataObj == null) { return new Activity[] { } } ; object data = dataObj.GetData(CF_DESIGNER); ICollection activities = null; if (data is Stream) { BinaryFormatter formatter = new BinaryFormatter(); ((Stream)data).Seek(0, SeekOrigin.Begin); object serializationData = formatter.Deserialize((Stream)data); if (serializationData is SerializationStore) { // get component serialization service ComponentSerializationService css = serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService; if (css == null) { throw new Exception("ComponentSerializationService is missing."); } // deserialize data activities = css.Deserialize((SerializationStore)serializationData); } } else { // Now check for a toolbox item. IToolboxService ts = (IToolboxService)serviceProvider.GetService(typeof(IToolboxService)); if (ts != null && ts.IsSupported(dataObj, designerHost)) { ToolboxItem toolBoxItem = ts.DeserializeToolboxItem(dataObj, designerHost); if (toolBoxItem != null) { // this will make sure that we add the assembly reference to project if (addReference && toolBoxItem.AssemblyName != null) { ITypeResolutionService trs = serviceProvider.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService; if (trs != null) { trs.ReferenceAssembly(toolBoxItem.AssemblyName); } } ActivityToolboxItem ActivityToolboxItem = toolBoxItem as ActivityToolboxItem; if (addReference && ActivityToolboxItem != null) { activities = ActivityToolboxItem.CreateComponentsWithUI(designerHost); } else { activities = toolBoxItem.CreateComponents(designerHost); } } } } return((activities != null) ? (Activity[])(new ArrayList(activities).ToArray(typeof(Activity))) : new Activity[] { }); }