Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / WinForms / Managed / System / WinForms / ComponentModel / COM2Interop / COM2FontConverter.cs / 1 / 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; ////// /// 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 /// 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 /// 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; ////// /// 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 /// 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 /// 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
- AttributeCollection.cs
- LoginCancelEventArgs.cs
- BypassElementCollection.cs
- FileReservationCollection.cs
- RowVisual.cs
- UrlPropertyAttribute.cs
- ToolStripRenderEventArgs.cs
- OleDbPropertySetGuid.cs
- DbMetaDataFactory.cs
- MasterPage.cs
- _LocalDataStore.cs
- Group.cs
- BitmapCacheBrush.cs
- Filter.cs
- XPathNodePointer.cs
- FilteredDataSetHelper.cs
- InvalidEnumArgumentException.cs
- AttributeInfo.cs
- LineInfo.cs
- HeaderCollection.cs
- ButtonField.cs
- httpserverutility.cs
- FrameworkRichTextComposition.cs
- VisualTreeHelper.cs
- Accessible.cs
- TextBoxDesigner.cs
- BreakRecordTable.cs
- WindowsTokenRoleProvider.cs
- OleDbError.cs
- SqlDataSourceCommandParser.cs
- VerticalAlignConverter.cs
- DynamicDocumentPaginator.cs
- XPathSelectionIterator.cs
- PageTheme.cs
- ProfileGroupSettings.cs
- ViewKeyConstraint.cs
- EntityParameterCollection.cs
- XPathMessageFilterElementComparer.cs
- QilNode.cs
- sqlpipe.cs
- StructuralCache.cs
- path.cs
- StreamDocument.cs
- ObjectDataSourceWizardForm.cs
- ExeContext.cs
- PathData.cs
- PrePrepareMethodAttribute.cs
- thaishape.cs
- ContentDisposition.cs
- AnonymousIdentificationSection.cs
- BooleanSwitch.cs
- ListViewInsertEventArgs.cs
- RsaSecurityTokenAuthenticator.cs
- XslException.cs
- GradientStopCollection.cs
- RowsCopiedEventArgs.cs
- ContainerSelectorActiveEvent.cs
- EventManager.cs
- DialogResultConverter.cs
- GenericPrincipal.cs
- RelatedImageListAttribute.cs
- SchemaNotation.cs
- ReadOnlyPropertyMetadata.cs
- StringKeyFrameCollection.cs
- TemplateParser.cs
- ByteViewer.cs
- DateTimePicker.cs
- RightsManagementInformation.cs
- BamlStream.cs
- DataBoundControlAdapter.cs
- CombinedGeometry.cs
- AutomationAttributeInfo.cs
- KeyInfo.cs
- CacheChildrenQuery.cs
- SSmlParser.cs
- XmlEncoding.cs
- SpotLight.cs
- Encoding.cs
- Int32CollectionValueSerializer.cs
- SignatureToken.cs
- XmlSchemaDatatype.cs
- ContainsRowNumberChecker.cs
- CheckableControlBaseAdapter.cs
- DescriptionAttribute.cs
- StagingAreaInputItem.cs
- SQLSingleStorage.cs
- SemaphoreSecurity.cs
- WebPartCatalogAddVerb.cs
- SizeChangedEventArgs.cs
- ControlBuilderAttribute.cs
- HelpProvider.cs
- Misc.cs
- ObjectKeyFrameCollection.cs
- ValidationService.cs
- EntityContainerEntitySet.cs
- _LocalDataStoreMgr.cs
- TemplateBindingExpressionConverter.cs
- IPipelineRuntime.cs
- ListSortDescription.cs
- XmlnsCache.cs