Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SequentialUshortCollection.cs
- SQLInt64.cs
- FieldAccessException.cs
- DataGridViewCellStyleConverter.cs
- ResourceReferenceExpressionConverter.cs
- wmiprovider.cs
- TextElementEnumerator.cs
- ExecutionContext.cs
- ButtonColumn.cs
- ScriptControl.cs
- TableAdapterManagerNameHandler.cs
- GeometryCollection.cs
- MailDefinition.cs
- InputLanguageEventArgs.cs
- TextEditorLists.cs
- WSSecurityPolicy11.cs
- SoapClientProtocol.cs
- XhtmlConformanceSection.cs
- PerformanceCounterCategory.cs
- CompilationSection.cs
- SchemaMapping.cs
- RepeaterCommandEventArgs.cs
- DynamicDocumentPaginator.cs
- RepeaterItemEventArgs.cs
- AsynchronousChannelMergeEnumerator.cs
- MsmqInputMessage.cs
- EmptyQuery.cs
- MimeBasePart.cs
- SerTrace.cs
- _RequestCacheProtocol.cs
- TTSEvent.cs
- LateBoundBitmapDecoder.cs
- WindowsGraphics.cs
- ReturnEventArgs.cs
- BrushMappingModeValidation.cs
- BitmapPalette.cs
- GridViewRow.cs
- IgnoreFileBuildProvider.cs
- objectresult_tresulttype.cs
- ConsumerConnectionPointCollection.cs
- NewItemsContextMenuStrip.cs
- SqlTypeConverter.cs
- InvokePattern.cs
- CompatibleComparer.cs
- AccessDataSource.cs
- DBAsyncResult.cs
- DataGridViewSelectedRowCollection.cs
- ManagedFilter.cs
- MenuItemBinding.cs
- ArithmeticException.cs
- XmlDocument.cs
- ControlBindingsConverter.cs
- StyleTypedPropertyAttribute.cs
- String.cs
- TextSearch.cs
- EditBehavior.cs
- SafeEventLogReadHandle.cs
- FlowDocumentPage.cs
- InputEventArgs.cs
- NavigationHelper.cs
- NetSectionGroup.cs
- OleDbParameter.cs
- IsolationInterop.cs
- WebPartsSection.cs
- String.cs
- OpCodes.cs
- DataGridColumn.cs
- FileLevelControlBuilderAttribute.cs
- TCEAdapterGenerator.cs
- DivideByZeroException.cs
- PropertyTabAttribute.cs
- DataGridHeaderBorder.cs
- ObsoleteAttribute.cs
- ToolStripItemEventArgs.cs
- PictureBox.cs
- VariableQuery.cs
- EntityContainerEmitter.cs
- MarshalByValueComponent.cs
- ModelTypeConverter.cs
- WeakReferenceList.cs
- ResolveMatches11.cs
- ItemsPresenter.cs
- NavigatingCancelEventArgs.cs
- BaseConfigurationRecord.cs
- XPathNodeInfoAtom.cs
- BaseValidator.cs
- FixedElement.cs
- PublishLicense.cs
- ReferencedAssembly.cs
- DataPager.cs
- UpdatableWrapper.cs
- RecordManager.cs
- Byte.cs
- DataGridViewAccessibleObject.cs
- SessionState.cs
- ApplicationServiceManager.cs
- NativeMethods.cs
- TableStyle.cs
- ContextMenuAutomationPeer.cs
- XamlSerializationHelper.cs