Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / WinForms / Managed / System / WinForms / ComponentModel / COM2Interop / COM2PictureConverter.cs / 1305376 / COM2PictureConverter.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms.ComponentModel.Com2Interop { using System.Runtime.Serialization.Formatters; using System.ComponentModel; using System.Diagnostics; using System; using System.Drawing; using System.Collections; using Microsoft.Win32; using System.Runtime.Versioning; ////// /// This class maps an IPicture to a System.Drawing.Image. /// internal class Com2PictureConverter : Com2DataTypeToManagedDataTypeConverter { object lastManaged; IntPtr lastNativeHandle; WeakReference pictureRef; IntPtr lastPalette = IntPtr.Zero; Type pictureType = typeof(Bitmap); public Com2PictureConverter(Com2PropertyDescriptor pd) { if (pd.DISPID == NativeMethods.ActiveX.DISPID_MOUSEICON || pd.Name.IndexOf("Icon") != -1) { pictureType = typeof(Icon); } } ////// /// Returns the managed type that this editor maps the property type to. /// public override Type ManagedType { get { return pictureType; } } ////// /// Converts the native value into a managed value /// [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public override object ConvertNativeToManaged(object nativeValue, Com2PropertyDescriptor pd) { if (nativeValue == null) { return null; } Debug.Assert(nativeValue is UnsafeNativeMethods.IPicture, "nativevalue is not IPicture"); UnsafeNativeMethods.IPicture nativePicture = (UnsafeNativeMethods.IPicture)nativeValue; IntPtr handle = nativePicture.GetHandle(); if (lastManaged != null && handle == lastNativeHandle) { return lastManaged; } lastNativeHandle = handle; //lastPalette = nativePicture.GetHPal(); if (handle != IntPtr.Zero) { switch (nativePicture.GetPictureType()) { case NativeMethods.Ole.PICTYPE_ICON: pictureType = typeof(Icon); lastManaged = Icon.FromHandle(handle); break; case NativeMethods.Ole.PICTYPE_BITMAP: pictureType = typeof(Bitmap); lastManaged = Image.FromHbitmap(handle); break; default: Debug.Fail("Unknown picture type"); break; } pictureRef = new WeakReference(nativePicture); } else { lastManaged = null; pictureRef = null; } return lastManaged; } ////// /// Converts the managed value into a native value /// [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public override object ConvertManagedToNative(object managedValue, Com2PropertyDescriptor pd, ref bool cancelSet) { // don't cancel the set cancelSet = false; if (lastManaged != null && lastManaged.Equals(managedValue) && pictureRef != null && pictureRef.IsAlive) { return pictureRef.Target; } // we have to build an IPicture lastManaged = managedValue; if (managedValue != null) { Guid g = typeof(UnsafeNativeMethods.IPicture).GUID; NativeMethods.PICTDESC pictdesc = null; bool own = false; if (lastManaged is Icon) { pictdesc = NativeMethods.PICTDESC.CreateIconPICTDESC(((Icon)lastManaged).Handle); } else if (lastManaged is Bitmap) { pictdesc = NativeMethods.PICTDESC.CreateBitmapPICTDESC(((Bitmap)lastManaged).GetHbitmap(), lastPalette); own = true; } else { Debug.Fail("Unknown Image type: " + managedValue.GetType().Name); } UnsafeNativeMethods.IPicture pict = UnsafeNativeMethods.OleCreatePictureIndirect(pictdesc, ref g, own); lastNativeHandle = pict.GetHandle(); pictureRef = new WeakReference(pict); return pict; } else { lastManaged = null; lastNativeHandle = lastPalette = IntPtr.Zero; pictureRef = null; return null; } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AQNBuilder.cs
- XsdValidatingReader.cs
- FlowDocument.cs
- ResXDataNode.cs
- PropertyInfo.cs
- InkPresenter.cs
- CrossSiteScriptingValidation.cs
- XmlArrayItemAttribute.cs
- AttributeInfo.cs
- ProgressChangedEventArgs.cs
- TextRangeEditLists.cs
- _IPv6Address.cs
- GroupBoxRenderer.cs
- CallSiteBinder.cs
- KnownBoxes.cs
- Propagator.Evaluator.cs
- InvokeHandlers.cs
- WebSysDescriptionAttribute.cs
- DayRenderEvent.cs
- HashRepartitionEnumerator.cs
- _SpnDictionary.cs
- ServicesUtilities.cs
- ClientClassGenerator.cs
- PartManifestEntry.cs
- WebPartDisplayModeCollection.cs
- HttpBufferlessInputStream.cs
- DisplayClaim.cs
- Stream.cs
- ArgumentDesigner.xaml.cs
- EngineSite.cs
- WebBaseEventKeyComparer.cs
- MailWebEventProvider.cs
- GlobalizationSection.cs
- HtmlHead.cs
- SqlCharStream.cs
- RegisteredHiddenField.cs
- Serialization.cs
- StructuralObject.cs
- HttpCacheParams.cs
- SqlBooleanMismatchVisitor.cs
- SqlConnectionManager.cs
- Authorization.cs
- DateTimeFormat.cs
- DBConnectionString.cs
- Span.cs
- Int16Storage.cs
- Substitution.cs
- ProfileProvider.cs
- StdRegProviderWrapper.cs
- Privilege.cs
- SearchExpression.cs
- RuntimeArgument.cs
- ScalarConstant.cs
- SynchronizedDispatch.cs
- StrongName.cs
- LineVisual.cs
- RoleGroupCollectionEditor.cs
- XmlDsigSep2000.cs
- StretchValidation.cs
- ProcessModule.cs
- LassoHelper.cs
- MappedMetaModel.cs
- columnmapfactory.cs
- WindowsSolidBrush.cs
- SaveFileDialog.cs
- CopyAttributesAction.cs
- GlyphingCache.cs
- NumberSubstitution.cs
- DisplayMemberTemplateSelector.cs
- PersonalizationStateQuery.cs
- InternalsVisibleToAttribute.cs
- MaskInputRejectedEventArgs.cs
- IRCollection.cs
- ConstraintCollection.cs
- _TimerThread.cs
- SecurityKeyUsage.cs
- SAPICategories.cs
- TypeInitializationException.cs
- FileUpload.cs
- OpenTypeCommon.cs
- IndependentAnimationStorage.cs
- TransformValueSerializer.cs
- TrackingMemoryStreamFactory.cs
- TreeNodeCollection.cs
- Clipboard.cs
- RuntimeArgumentHandle.cs
- DataGridView.cs
- objectquery_tresulttype.cs
- SQLMoney.cs
- TypeExtensionConverter.cs
- userdatakeys.cs
- Transform.cs
- ExpressionBuilderContext.cs
- COM2ComponentEditor.cs
- PropertyAccessVisitor.cs
- PropertyChangeTracker.cs
- QueryCreatedEventArgs.cs
- IgnoreSectionHandler.cs
- FontDialog.cs
- CompatibleComparer.cs