Code:
/ DotNET / DotNET / 8.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
- SqlErrorCollection.cs
- Pen.cs
- BaseTemplatedMobileComponentEditor.cs
- SiteMapDataSource.cs
- BCryptHashAlgorithm.cs
- DataBindingCollection.cs
- ProcessModule.cs
- CookieProtection.cs
- CardSpacePolicyElement.cs
- EventEntry.cs
- TableStyle.cs
- UserPreferenceChangingEventArgs.cs
- MonthCalendarDesigner.cs
- TreeViewBindingsEditor.cs
- BindingEntityInfo.cs
- VectorValueSerializer.cs
- ExecutionTracker.cs
- ProgressPage.cs
- InboundActivityHelper.cs
- BuilderPropertyEntry.cs
- BitmapPalettes.cs
- OperandQuery.cs
- _AutoWebProxyScriptEngine.cs
- ButtonBaseAdapter.cs
- StylusPlugInCollection.cs
- UpdateTracker.cs
- EditorZoneBase.cs
- DataGridViewRowPrePaintEventArgs.cs
- Point3DAnimationBase.cs
- XmlBinaryReaderSession.cs
- Message.cs
- IWorkflowDebuggerService.cs
- SplitterEvent.cs
- DownloadProgressEventArgs.cs
- Brush.cs
- SqlDelegatedTransaction.cs
- EllipseGeometry.cs
- HierarchicalDataBoundControlAdapter.cs
- TcpAppDomainProtocolHandler.cs
- CTreeGenerator.cs
- CLRBindingWorker.cs
- CompatibleIComparer.cs
- Variant.cs
- ReliableSessionBindingElementImporter.cs
- ConnectionManagementElement.cs
- CookieProtection.cs
- ZipIOZip64EndOfCentralDirectoryLocatorBlock.cs
- DisableDpiAwarenessAttribute.cs
- InheritanceContextChangedEventManager.cs
- UriScheme.cs
- OrderedDictionaryStateHelper.cs
- HorizontalAlignConverter.cs
- CodeMemberEvent.cs
- ListBoxItemWrapperAutomationPeer.cs
- SharedPerformanceCounter.cs
- WebServiceResponse.cs
- QilIterator.cs
- CapabilitiesUse.cs
- HtmlTable.cs
- CacheMemory.cs
- WmlTextViewAdapter.cs
- StringArrayConverter.cs
- TaskResultSetter.cs
- ProcessInfo.cs
- X509CertificateValidator.cs
- XmlEnumAttribute.cs
- DynamicField.cs
- SHA384.cs
- MetadataWorkspace.cs
- TextElementEnumerator.cs
- XmlSchemaFacet.cs
- IOException.cs
- DefaultObjectMappingItemCollection.cs
- DecimalConverter.cs
- AutomationAttributeInfo.cs
- CodeGeneratorOptions.cs
- XmlDataCollection.cs
- DesignerVerb.cs
- HttpListener.cs
- DynamicDataRoute.cs
- BulletedList.cs
- XmlUnspecifiedAttribute.cs
- Expression.cs
- PagedControl.cs
- OptimizerPatterns.cs
- Button.cs
- XPathDescendantIterator.cs
- ApplicationSecurityInfo.cs
- RemotingServices.cs
- FixedHighlight.cs
- ChtmlCommandAdapter.cs
- Frame.cs
- DiscoveryExceptionDictionary.cs
- RegexNode.cs
- ResetableIterator.cs
- OpenFileDialog.cs
- X509SecurityTokenProvider.cs
- TabPage.cs
- ToolStripLabel.cs
- XsltConvert.cs