Code:
/ FX-1434 / FX-1434 / 1.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
- ActionItem.cs
- DebugView.cs
- OdbcConnectionStringbuilder.cs
- RequestStatusBarUpdateEventArgs.cs
- UpdatePanel.cs
- TextTreeRootTextBlock.cs
- StylusOverProperty.cs
- TaiwanCalendar.cs
- IntPtr.cs
- ThaiBuddhistCalendar.cs
- AnchoredBlock.cs
- AttributeCollection.cs
- WebBrowserNavigatedEventHandler.cs
- DbException.cs
- RayHitTestParameters.cs
- PaginationProgressEventArgs.cs
- HttpRequestWrapper.cs
- sqlstateclientmanager.cs
- SynthesizerStateChangedEventArgs.cs
- _ListenerResponseStream.cs
- CqlIdentifiers.cs
- SqlProfileProvider.cs
- LinkGrep.cs
- XamlSerializer.cs
- PageThemeCodeDomTreeGenerator.cs
- ITextView.cs
- WSTransactionSection.cs
- BindingManagerDataErrorEventArgs.cs
- Point4DConverter.cs
- DBNull.cs
- EntityDesignerBuildProvider.cs
- SeparatorAutomationPeer.cs
- XmlException.cs
- VarInfo.cs
- LocalizabilityAttribute.cs
- PointIndependentAnimationStorage.cs
- CodeNamespaceImportCollection.cs
- ValueSerializerAttribute.cs
- IPHostEntry.cs
- PackageRelationshipCollection.cs
- SqlNodeAnnotations.cs
- TypeExtensions.cs
- Variant.cs
- ValidationEventArgs.cs
- SystemNetHelpers.cs
- InputLanguageCollection.cs
- TimeoutValidationAttribute.cs
- BitmapData.cs
- TreeBuilder.cs
- PngBitmapEncoder.cs
- DataBindingValueUIHandler.cs
- Point4D.cs
- XmlReaderSettings.cs
- Transform.cs
- AudioStateChangedEventArgs.cs
- TreeSet.cs
- OrderingInfo.cs
- WebPartZoneCollection.cs
- MenuTracker.cs
- CurrencyManager.cs
- ProxyFragment.cs
- ElementNotAvailableException.cs
- NonBatchDirectoryCompiler.cs
- AccessDataSource.cs
- FontInfo.cs
- SamlAttribute.cs
- XamlWrappingReader.cs
- ClientProxyGenerator.cs
- LogicalMethodInfo.cs
- PictureBox.cs
- LambdaCompiler.Address.cs
- CornerRadius.cs
- ToolStripManager.cs
- COAUTHIDENTITY.cs
- FeatureSupport.cs
- MsmqInputMessage.cs
- CaseInsensitiveComparer.cs
- AuthorizationSection.cs
- SqlDataReader.cs
- EntityDataSourceStatementEditorForm.cs
- ManifestResourceInfo.cs
- IgnoreFlushAndCloseStream.cs
- TableStyle.cs
- QilLoop.cs
- EntityCommandCompilationException.cs
- BulletedListEventArgs.cs
- XmlResolver.cs
- DictionaryItemsCollection.cs
- SQLInt16.cs
- DbDataSourceEnumerator.cs
- CSharpCodeProvider.cs
- SqlTrackingWorkflowInstance.cs
- LineSegment.cs
- DataControlCommands.cs
- StatusBar.cs
- WinCategoryAttribute.cs
- Item.cs
- ApplyImportsAction.cs
- RegexRunner.cs
- SchemaTypeEmitter.cs