Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / 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.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ApplyImportsAction.cs
- XPathItem.cs
- Exceptions.cs
- IfAction.cs
- EntityContainerEmitter.cs
- IProvider.cs
- RoutedUICommand.cs
- GridViewCellAutomationPeer.cs
- FormViewCommandEventArgs.cs
- Condition.cs
- HuffModule.cs
- CopyNamespacesAction.cs
- Int32Rect.cs
- PauseStoryboard.cs
- BaseCAMarshaler.cs
- Token.cs
- CollectionViewGroup.cs
- XmlHierarchicalEnumerable.cs
- AspNetSynchronizationContext.cs
- HwndHostAutomationPeer.cs
- Track.cs
- XamlStream.cs
- PermissionListSet.cs
- OrderedDictionaryStateHelper.cs
- TextSerializer.cs
- EdmRelationshipRoleAttribute.cs
- UidManager.cs
- Deserializer.cs
- Model3DCollection.cs
- SudsParser.cs
- ParserContext.cs
- RowsCopiedEventArgs.cs
- pingexception.cs
- CustomMenuItemCollection.cs
- PassportPrincipal.cs
- FrameworkElement.cs
- CompatibleIComparer.cs
- ProtectedConfiguration.cs
- CompoundFileDeflateTransform.cs
- HtmlHistory.cs
- X509Certificate2Collection.cs
- Attributes.cs
- baseaxisquery.cs
- ServicePointManager.cs
- filewebresponse.cs
- DataGridViewCellStyleChangedEventArgs.cs
- SettingsPropertyIsReadOnlyException.cs
- DataReaderContainer.cs
- AssemblyFilter.cs
- DataTemplateSelector.cs
- ExtensionFile.cs
- EmbeddedObject.cs
- BaseComponentEditor.cs
- Component.cs
- MarkupCompilePass1.cs
- RemotingAttributes.cs
- BlockUIContainer.cs
- WindowHideOrCloseTracker.cs
- StorageEntitySetMapping.cs
- PenCursorManager.cs
- IsolatedStorageSecurityState.cs
- TextTreeObjectNode.cs
- xmlfixedPageInfo.cs
- TraceProvider.cs
- BaseWebProxyFinder.cs
- ErrorWrapper.cs
- BaseCodeDomTreeGenerator.cs
- XamlDesignerSerializationManager.cs
- HandleExceptionArgs.cs
- DesignerProperties.cs
- ActivityTrace.cs
- WizardPanel.cs
- XmlWhitespace.cs
- SqlRewriteScalarSubqueries.cs
- ExpressionQuoter.cs
- InstanceDataCollection.cs
- TextEditorTables.cs
- ContentElement.cs
- HGlobalSafeHandle.cs
- DataGridColumn.cs
- XmlValidatingReader.cs
- TPLETWProvider.cs
- CalendarItem.cs
- MapPathBasedVirtualPathProvider.cs
- WmpBitmapDecoder.cs
- SimpleTextLine.cs
- Int32Storage.cs
- _WinHttpWebProxyDataBuilder.cs
- KeyValueSerializer.cs
- Int32Animation.cs
- TableLayoutSettings.cs
- Int32CollectionConverter.cs
- DesignerTransaction.cs
- EntityDataSourceView.cs
- MissingMethodException.cs
- WrappedReader.cs
- SchemaManager.cs
- WorkflowTransactionService.cs
- HttpClientProtocol.cs
- HandoffBehavior.cs