Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- ShutDownListener.cs
- FontFamilyIdentifier.cs
- XmlILModule.cs
- AutomationPattern.cs
- TypeLoader.cs
- ValueTypeFixupInfo.cs
- FutureFactory.cs
- EventHandlerList.cs
- OracleSqlParser.cs
- NavigationPropertyEmitter.cs
- _Semaphore.cs
- DataGridColumnDropSeparator.cs
- WmpBitmapEncoder.cs
- CharacterMetrics.cs
- FormsAuthenticationUser.cs
- QilInvokeEarlyBound.cs
- QilReference.cs
- TimeSpanMinutesOrInfiniteConverter.cs
- SID.cs
- CombinedGeometry.cs
- AssemblyBuilder.cs
- BufferedGraphicsManager.cs
- StoragePropertyMapping.cs
- TransactionManager.cs
- Slider.cs
- StrokeNodeData.cs
- PrinterResolution.cs
- RadioButtonStandardAdapter.cs
- DataGridViewRowsAddedEventArgs.cs
- BinaryUtilClasses.cs
- CodeExporter.cs
- TaskFileService.cs
- RegexCompiler.cs
- TreeView.cs
- DeviceContexts.cs
- DirectoryInfo.cs
- ReadingWritingEntityEventArgs.cs
- TokenDescriptor.cs
- CompoundFileDeflateTransform.cs
- HttpApplication.cs
- ButtonChrome.cs
- TextDocumentView.cs
- XmlSerializableServices.cs
- LambdaCompiler.cs
- WebServiceReceiveDesigner.cs
- Rotation3D.cs
- UnsafeNativeMethods.cs
- GenericPrincipal.cs
- LayoutUtils.cs
- CommonObjectSecurity.cs
- ListControlStringCollectionEditor.cs
- PagesChangedEventArgs.cs
- ScriptControl.cs
- HatchBrush.cs
- XmlExpressionDumper.cs
- ConnectivityStatus.cs
- PopOutPanel.cs
- DefaultClaimSet.cs
- XmlExceptionHelper.cs
- TextDecorationUnitValidation.cs
- LinqDataSourceSelectEventArgs.cs
- mediapermission.cs
- ColumnMapTranslator.cs
- MessageContractExporter.cs
- ServiceHandle.cs
- DynamicRenderer.cs
- Int32Converter.cs
- HttpHandlerAction.cs
- ScopeElement.cs
- ButtonBaseAdapter.cs
- DataListItemCollection.cs
- ToolStripProgressBar.cs
- ColumnHeaderConverter.cs
- BooleanFacetDescriptionElement.cs
- SplitContainer.cs
- DataGridItemCollection.cs
- Quad.cs
- StreamUpgradeProvider.cs
- ParallelActivityDesigner.cs
- XmlWriter.cs
- DataGridViewSelectedCellsAccessibleObject.cs
- ContentValidator.cs
- COM2Properties.cs
- ErrorHandler.cs
- SqlDependency.cs
- SecureStringHasher.cs
- TableItemPattern.cs
- Conditional.cs
- XmlNullResolver.cs
- WebConvert.cs
- ServiceBusyException.cs
- DSASignatureFormatter.cs
- ConsumerConnectionPointCollection.cs
- BitmapEffect.cs
- KeyFrames.cs
- XmlSchemaException.cs
- OdbcError.cs
- WorkflowOperationErrorHandler.cs
- Conditional.cs
- AuthenticationSection.cs