Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / WinForms / Managed / System / WinForms / ComponentModel / COM2Interop / COM2PictureConverter.cs / 1 / 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; ////// /// 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 /// 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 /// 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. //------------------------------------------------------------------------------ //// 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; ////// /// 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 /// 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 /// 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
- DataPointer.cs
- HtmlPanelAdapter.cs
- LabelAutomationPeer.cs
- CommandField.cs
- RowToParametersTransformer.cs
- PtsCache.cs
- Point3DValueSerializer.cs
- ObservableDictionary.cs
- DefaultTextStoreTextComposition.cs
- Wizard.cs
- SpinLock.cs
- DataSourceHelper.cs
- OraclePermission.cs
- XmlSerializerNamespaces.cs
- MessageHeaderDescriptionCollection.cs
- DocumentOrderComparer.cs
- QuaternionRotation3D.cs
- Task.cs
- ContentFileHelper.cs
- TerminatorSinks.cs
- DoubleStorage.cs
- VersionConverter.cs
- DeclarativeExpressionConditionDeclaration.cs
- GridViewRowEventArgs.cs
- TargetParameterCountException.cs
- CodeDelegateCreateExpression.cs
- TraceLevelHelper.cs
- PopOutPanel.cs
- RunWorkerCompletedEventArgs.cs
- XDRSchema.cs
- DataGridViewRowCancelEventArgs.cs
- SmiRequestExecutor.cs
- BooleanExpr.cs
- Rules.cs
- ToolStripRendererSwitcher.cs
- ProxyHelper.cs
- StateManagedCollection.cs
- BrowserDefinitionCollection.cs
- RequiredAttributeAttribute.cs
- PermissionListSet.cs
- PersonalizationStateQuery.cs
- PolyQuadraticBezierSegment.cs
- ReachIDocumentPaginatorSerializerAsync.cs
- ThemeableAttribute.cs
- DeviceContexts.cs
- ImageListStreamer.cs
- DbConnectionHelper.cs
- TypeForwardedFromAttribute.cs
- SiteMapDataSource.cs
- ListBoxChrome.cs
- PaginationProgressEventArgs.cs
- LocationSectionRecord.cs
- BitmapEffectInputData.cs
- _LocalDataStore.cs
- CodeChecksumPragma.cs
- FileIOPermission.cs
- InternalsVisibleToAttribute.cs
- UserInitiatedNavigationPermission.cs
- RequestCachePolicyConverter.cs
- XamlReaderHelper.cs
- SmiTypedGetterSetter.cs
- ProxyGenerator.cs
- _OverlappedAsyncResult.cs
- Hash.cs
- bindurihelper.cs
- IntranetCredentialPolicy.cs
- XmlSchemaSimpleTypeList.cs
- BufferModeSettings.cs
- ReverseQueryOperator.cs
- CodeArrayIndexerExpression.cs
- AdPostCacheSubstitution.cs
- ObjectDataSourceFilteringEventArgs.cs
- WebPartMinimizeVerb.cs
- RectConverter.cs
- Helper.cs
- ValidationSummary.cs
- PerfCounters.cs
- Delegate.cs
- COSERVERINFO.cs
- Propagator.ExtentPlaceholderCreator.cs
- AmbientLight.cs
- XmlTypeMapping.cs
- HttpListenerPrefixCollection.cs
- XXXInfos.cs
- InsufficientExecutionStackException.cs
- HtmlTextArea.cs
- ActivityExecutorOperation.cs
- EventMemberCodeDomSerializer.cs
- ModulesEntry.cs
- Adorner.cs
- DefaultIfEmptyQueryOperator.cs
- DataTableCollection.cs
- MergeLocalizationDirectives.cs
- ConditionalAttribute.cs
- DataGridAddNewRow.cs
- DateTimeSerializationSection.cs
- EnvelopedPkcs7.cs
- OptimizerPatterns.cs
- RoutedEventValueSerializer.cs
- LassoSelectionBehavior.cs