Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / WinForms / Managed / System / WinForms / ComponentModel / COM2Interop / COM2FontConverter.cs / 1305376 / COM2FontConverter.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms.ComponentModel.Com2Interop { using System.Runtime.Serialization.Formatters; using System.Runtime.InteropServices; 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 OLE_COLOR to a managed Color editor. /// internal class Com2FontConverter : Com2DataTypeToManagedDataTypeConverter { private IntPtr lastHandle = IntPtr.Zero; private Font lastFont = null; public override bool AllowExpand { get { return true; } } ////// /// Returns the managed type that this editor maps the property type to. /// public override Type ManagedType { get { return typeof(Font); } } ////// /// Converts the native value into a managed value /// [ResourceExposure(ResourceScope.Process)] [ResourceConsumption(ResourceScope.Process)] public override object ConvertNativeToManaged(object nativeValue, Com2PropertyDescriptor pd) { // we're getting an IFont thing here UnsafeNativeMethods.IFont nativeFont = nativeValue as UnsafeNativeMethods.IFont; if (nativeFont == null) { lastHandle = IntPtr.Zero; lastFont = Control.DefaultFont; return lastFont; } IntPtr fontHandle = nativeFont.GetHFont(); // see if we have this guy cached if (fontHandle == lastHandle && lastFont != null) { return lastFont; } lastHandle = fontHandle; try { // this wasn't working because it was converting everything to // world units. // Font font = Font.FromHfont(lastHandle); try { lastFont = ControlPaint.FontInPoints(font); } finally { font.Dispose(); } } catch(ArgumentException) { // we will fail on non-truetype fonts, so // just use the default font. lastFont = Control.DefaultFont; } return lastFont; } ////// /// Converts the managed value into a native value /// [ResourceExposure(ResourceScope.Process)] [ResourceConsumption(ResourceScope.Process)] public override object ConvertManagedToNative(object managedValue, Com2PropertyDescriptor pd, ref bool cancelSet) { // we default to black. // if (managedValue == null) { managedValue = Control.DefaultFont; } cancelSet = true; if (lastFont != null && lastFont.Equals(managedValue)) { // don't do anything here. return null; } lastFont = (Font)managedValue; UnsafeNativeMethods.IFont nativeFont = (UnsafeNativeMethods.IFont)pd.GetNativeValue(pd.TargetObject); // now, push all the values into the native side if (nativeFont != null) { bool changed = ControlPaint.FontToIFont(lastFont, nativeFont); if (changed) { // here, we want to pick up a new font from the handle lastFont = null; ConvertNativeToManaged(nativeFont, pd); } } 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.Runtime.InteropServices; 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 OLE_COLOR to a managed Color editor. /// internal class Com2FontConverter : Com2DataTypeToManagedDataTypeConverter { private IntPtr lastHandle = IntPtr.Zero; private Font lastFont = null; public override bool AllowExpand { get { return true; } } ////// /// Returns the managed type that this editor maps the property type to. /// public override Type ManagedType { get { return typeof(Font); } } ////// /// Converts the native value into a managed value /// [ResourceExposure(ResourceScope.Process)] [ResourceConsumption(ResourceScope.Process)] public override object ConvertNativeToManaged(object nativeValue, Com2PropertyDescriptor pd) { // we're getting an IFont thing here UnsafeNativeMethods.IFont nativeFont = nativeValue as UnsafeNativeMethods.IFont; if (nativeFont == null) { lastHandle = IntPtr.Zero; lastFont = Control.DefaultFont; return lastFont; } IntPtr fontHandle = nativeFont.GetHFont(); // see if we have this guy cached if (fontHandle == lastHandle && lastFont != null) { return lastFont; } lastHandle = fontHandle; try { // this wasn't working because it was converting everything to // world units. // Font font = Font.FromHfont(lastHandle); try { lastFont = ControlPaint.FontInPoints(font); } finally { font.Dispose(); } } catch(ArgumentException) { // we will fail on non-truetype fonts, so // just use the default font. lastFont = Control.DefaultFont; } return lastFont; } ////// /// Converts the managed value into a native value /// [ResourceExposure(ResourceScope.Process)] [ResourceConsumption(ResourceScope.Process)] public override object ConvertManagedToNative(object managedValue, Com2PropertyDescriptor pd, ref bool cancelSet) { // we default to black. // if (managedValue == null) { managedValue = Control.DefaultFont; } cancelSet = true; if (lastFont != null && lastFont.Equals(managedValue)) { // don't do anything here. return null; } lastFont = (Font)managedValue; UnsafeNativeMethods.IFont nativeFont = (UnsafeNativeMethods.IFont)pd.GetNativeValue(pd.TargetObject); // now, push all the values into the native side if (nativeFont != null) { bool changed = ControlPaint.FontToIFont(lastFont, nativeFont); if (changed) { // here, we want to pick up a new font from the handle lastFont = null; ConvertNativeToManaged(nativeFont, pd); } } 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
- SqlCacheDependencyDatabase.cs
- IdnMapping.cs
- RuntimeCompatibilityAttribute.cs
- TypeBuilder.cs
- MimeTypeMapper.cs
- WebConvert.cs
- ApplyHostConfigurationBehavior.cs
- _CookieModule.cs
- Funcletizer.cs
- RectConverter.cs
- ElementAction.cs
- ObjectDataSourceChooseMethodsPanel.cs
- DesignTimeTemplateParser.cs
- XmlBindingWorker.cs
- MethodBody.cs
- ColorKeyFrameCollection.cs
- XmlObjectSerializerWriteContextComplexJson.cs
- ModifierKeysValueSerializer.cs
- GeneralTransform3DTo2DTo3D.cs
- BoolLiteral.cs
- OdbcError.cs
- ListViewAutomationPeer.cs
- AdRotator.cs
- StyleXamlParser.cs
- BitmapSizeOptions.cs
- CryptoProvider.cs
- ScrollBarAutomationPeer.cs
- XsltInput.cs
- CompositeControl.cs
- EditorZoneBase.cs
- AbsoluteQuery.cs
- TypographyProperties.cs
- CodeIndexerExpression.cs
- Mutex.cs
- XPathNode.cs
- KeyValueInternalCollection.cs
- InstanceDataCollectionCollection.cs
- ServiceInfo.cs
- RelationshipConstraintValidator.cs
- ConfigurationSettings.cs
- Properties.cs
- FontSourceCollection.cs
- OciHandle.cs
- WaitForChangedResult.cs
- QueryParameter.cs
- ProfileSettings.cs
- FamilyTypeface.cs
- HttpContextWrapper.cs
- HostingEnvironment.cs
- PkcsMisc.cs
- JsonStringDataContract.cs
- PageFunction.cs
- MemberNameValidator.cs
- XPathDocumentBuilder.cs
- FormCollection.cs
- Subset.cs
- TextBoxRenderer.cs
- ChameleonKey.cs
- HwndKeyboardInputProvider.cs
- FlowDocumentScrollViewerAutomationPeer.cs
- SecureConversationVersion.cs
- Aes.cs
- ResponseBodyWriter.cs
- TransformProviderWrapper.cs
- TextBoxView.cs
- PersistenceTypeAttribute.cs
- DeadCharTextComposition.cs
- RowBinding.cs
- Line.cs
- COSERVERINFO.cs
- ListItem.cs
- ActivityExecutionWorkItem.cs
- FullTextState.cs
- FilePrompt.cs
- SByteStorage.cs
- TextBox.cs
- StatusBar.cs
- PerformanceCounterCategory.cs
- MultiPartWriter.cs
- DependencyObjectType.cs
- SettingsPropertyIsReadOnlyException.cs
- MetadataCache.cs
- RoutedCommand.cs
- UnsafeNativeMethods.cs
- MetricEntry.cs
- ConcurrentBag.cs
- AttributeCollection.cs
- ObjectQuery.cs
- ReadWriteObjectLock.cs
- ValueQuery.cs
- InputBinding.cs
- CollectionViewGroupInternal.cs
- Transform3DCollection.cs
- TypeForwardedToAttribute.cs
- RemotingServices.cs
- CodePropertyReferenceExpression.cs
- Utility.cs
- TypeDependencyAttribute.cs
- Vector3DKeyFrameCollection.cs
- RunInstallerAttribute.cs