Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / 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
- ManagedIStream.cs
- NavigatingCancelEventArgs.cs
- DesignerTransactionCloseEvent.cs
- Rules.cs
- WorkflowViewManager.cs
- CheckBoxBaseAdapter.cs
- OleDbErrorCollection.cs
- ContextBase.cs
- CodeNamespaceCollection.cs
- EventEntry.cs
- DesignSurfaceEvent.cs
- CodeSnippetExpression.cs
- SharedDp.cs
- RegionIterator.cs
- GenericEnumConverter.cs
- SqlWebEventProvider.cs
- WebPartRestoreVerb.cs
- InternalDispatchObject.cs
- ChangeInterceptorAttribute.cs
- NamedPipeTransportBindingElement.cs
- DataGridViewRowErrorTextNeededEventArgs.cs
- TemplateInstanceAttribute.cs
- ManagementQuery.cs
- RemotingSurrogateSelector.cs
- RuntimeConfigurationRecord.cs
- XmlDataProvider.cs
- OutOfMemoryException.cs
- TraceLevelStore.cs
- XamlVector3DCollectionSerializer.cs
- SuspendDesigner.cs
- SmiEventSink.cs
- ClientSponsor.cs
- DocumentViewerHelper.cs
- ISessionStateStore.cs
- Separator.cs
- TemplateBamlTreeBuilder.cs
- ContactManager.cs
- XmlDocumentFragment.cs
- ControlBuilderAttribute.cs
- XmlStreamedByteStreamReader.cs
- ActivityDesignerResources.cs
- Stack.cs
- StringSorter.cs
- RepeaterItemEventArgs.cs
- ObjectDataSourceStatusEventArgs.cs
- RequestCachePolicy.cs
- WaitHandleCannotBeOpenedException.cs
- TrackBarRenderer.cs
- ProgressPage.cs
- Variable.cs
- RowsCopiedEventArgs.cs
- PropertyGrid.cs
- PropertyManager.cs
- Trace.cs
- LinqDataSourceView.cs
- contentDescriptor.cs
- FixedSOMTextRun.cs
- LocalValueEnumerator.cs
- HostingPreferredMapPath.cs
- ToolBarPanel.cs
- TraceLevelHelper.cs
- ResourceReader.cs
- TextBoxView.cs
- ThousandthOfEmRealPoints.cs
- TriggerAction.cs
- StrokeNodeOperations2.cs
- SqlFileStream.cs
- BuildProviderCollection.cs
- TypedAsyncResult.cs
- KeyGestureValueSerializer.cs
- Renderer.cs
- BamlVersionHeader.cs
- BindingBase.cs
- Inflater.cs
- BindingCompleteEventArgs.cs
- __ComObject.cs
- IsolatedStorageFileStream.cs
- PageContentCollection.cs
- SizeF.cs
- UseLicense.cs
- SqlProviderManifest.cs
- SemanticResolver.cs
- Code.cs
- GridViewEditEventArgs.cs
- _BufferOffsetSize.cs
- ControllableStoryboardAction.cs
- ComplexType.cs
- XmlNotation.cs
- TextAdaptor.cs
- DropDownButton.cs
- TreeNodeConverter.cs
- TrackingLocationCollection.cs
- UserControlBuildProvider.cs
- SQLBoolean.cs
- Brush.cs
- BitmapCache.cs
- TypeResolvingOptions.cs
- BoolExpression.cs
- Script.cs
- LoggedException.cs