Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Misc / GDI / MeasurementDCInfo.cs / 1305376 / MeasurementDCInfo.cs
#if OPTIMIZED_MEASUREMENTDC
#if WGCM_TEST_SUITE // Enable tracking when built for the test suites.
#define TRACK_HDC
#define GDI_FONT_CACHE_TRACK
#endif
#if [....]_NAMESPACE
namespace System.Windows.Forms.Internal {
#elif DRAWING_NAMESPACE
namespace System.Drawing.Internal {
#else
namespace System.Experimental.Gdi {
#endif
#if [....]_PUBLIC_GRAPHICS_LIBRARY
public
#else
internal
#endif
static class MeasurementDCInfo {
/// MeasurementDCInfo
/// This class optimizes the MeasurmentGraphics as it caches in the last used font and TextMargins used.
/// This prevents unnecessary p/invoke calls to GetCurrentObject, etc
/// It has been found to give close to 2x performance when drawing lots of text in rapid succession
/// DataGridView with lots of text, etc.
/// To turn it on for your DLL, use the OPTIMIZED_MEASUREMENTDC compiler switch and add this class to the sources.
[ThreadStatic]
private static CachedInfo cachedMeasurementDCInfo;
/// IsMeasurementDC
/// Returns whether the IDeviceContext passed in is our static MeasurementDC.
/// If it is, we know a bit more information about it.
internal static bool IsMeasurementDC(DeviceContext dc)
{
WindowsGraphics sharedGraphics = WindowsGraphicsCacheManager.GetCurrentMeasurementGraphics();
return sharedGraphics != null && sharedGraphics.DeviceContext != null && sharedGraphics.DeviceContext.Hdc == dc.Hdc;
}
/// LastUsedFont -
/// Returns the font we think was last selected into the MeasurementGraphics.
///
internal static WindowsFont LastUsedFont
{
get
{
return (cachedMeasurementDCInfo == null) ? null : cachedMeasurementDCInfo.LastUsedFont;
}
set
{
if (cachedMeasurementDCInfo == null)
{
cachedMeasurementDCInfo = new CachedInfo();
}
cachedMeasurementDCInfo.UpdateFont(value);
}
}
/// GetTextMargins - checks to see if we have cached information about the current font,
/// returns info about it.
/// An MRU of Font margins was considered, but seems like overhead.
internal static IntNativeMethods.DRAWTEXTPARAMS GetTextMargins(WindowsGraphics wg, WindowsFont font) {
// PERF: operate on a local reference rather than party directly on the thread static one.
CachedInfo currentCachedInfo = cachedMeasurementDCInfo;
if (currentCachedInfo != null && currentCachedInfo.LeftTextMargin >0 && currentCachedInfo.RightTextMargin >0 && font == currentCachedInfo.LastUsedFont)
{
// we have to return clones as DrawTextEx will modify this struct
return new IntNativeMethods.DRAWTEXTPARAMS(currentCachedInfo.LeftTextMargin,currentCachedInfo.RightTextMargin);
}
else if (currentCachedInfo == null)
{
currentCachedInfo = new CachedInfo();
cachedMeasurementDCInfo = currentCachedInfo;
}
IntNativeMethods.DRAWTEXTPARAMS drawTextParams = wg.GetTextMargins(font);
currentCachedInfo.LeftTextMargin = drawTextParams.iLeftMargin;
currentCachedInfo.RightTextMargin = drawTextParams.iRightMargin;
// returning a copy here to be consistent with the return value from the cache.
return new IntNativeMethods.DRAWTEXTPARAMS(currentCachedInfo.LeftTextMargin,currentCachedInfo.RightTextMargin);
}
internal static void ResetIfIsMeasurementDC(IntPtr hdc) {
WindowsGraphics sharedGraphics = WindowsGraphicsCacheManager.GetCurrentMeasurementGraphics();
if (sharedGraphics != null && sharedGraphics.DeviceContext != null && sharedGraphics.DeviceContext.Hdc == hdc) {
CachedInfo currentCachedInfo = cachedMeasurementDCInfo;
if (currentCachedInfo != null)
{
currentCachedInfo.UpdateFont(null);
}
}
}
/// Reset
/// clear the current cached information about the measurement dc.
internal static void Reset() {
CachedInfo currentCachedInfo = cachedMeasurementDCInfo;
if (currentCachedInfo != null)
{
currentCachedInfo.UpdateFont(null);
}
}
/// CachedInfo
/// store all the thread statics together so we dont have to fetch individual fields out of TLS
private sealed class CachedInfo
{
public WindowsFont LastUsedFont;
public int LeftTextMargin;
public int RightTextMargin;
internal void UpdateFont(WindowsFont font)
{
if (LastUsedFont != font)
{
LastUsedFont = font;
LeftTextMargin = -1;
RightTextMargin = -1;
}
}
}
}
}
#endif // OPTIMIZED_MEASUREMENTDC
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
#if OPTIMIZED_MEASUREMENTDC
#if WGCM_TEST_SUITE // Enable tracking when built for the test suites.
#define TRACK_HDC
#define GDI_FONT_CACHE_TRACK
#endif
#if [....]_NAMESPACE
namespace System.Windows.Forms.Internal {
#elif DRAWING_NAMESPACE
namespace System.Drawing.Internal {
#else
namespace System.Experimental.Gdi {
#endif
#if [....]_PUBLIC_GRAPHICS_LIBRARY
public
#else
internal
#endif
static class MeasurementDCInfo {
/// MeasurementDCInfo
/// This class optimizes the MeasurmentGraphics as it caches in the last used font and TextMargins used.
/// This prevents unnecessary p/invoke calls to GetCurrentObject, etc
/// It has been found to give close to 2x performance when drawing lots of text in rapid succession
/// DataGridView with lots of text, etc.
/// To turn it on for your DLL, use the OPTIMIZED_MEASUREMENTDC compiler switch and add this class to the sources.
[ThreadStatic]
private static CachedInfo cachedMeasurementDCInfo;
/// IsMeasurementDC
/// Returns whether the IDeviceContext passed in is our static MeasurementDC.
/// If it is, we know a bit more information about it.
internal static bool IsMeasurementDC(DeviceContext dc)
{
WindowsGraphics sharedGraphics = WindowsGraphicsCacheManager.GetCurrentMeasurementGraphics();
return sharedGraphics != null && sharedGraphics.DeviceContext != null && sharedGraphics.DeviceContext.Hdc == dc.Hdc;
}
/// LastUsedFont -
/// Returns the font we think was last selected into the MeasurementGraphics.
///
internal static WindowsFont LastUsedFont
{
get
{
return (cachedMeasurementDCInfo == null) ? null : cachedMeasurementDCInfo.LastUsedFont;
}
set
{
if (cachedMeasurementDCInfo == null)
{
cachedMeasurementDCInfo = new CachedInfo();
}
cachedMeasurementDCInfo.UpdateFont(value);
}
}
/// GetTextMargins - checks to see if we have cached information about the current font,
/// returns info about it.
/// An MRU of Font margins was considered, but seems like overhead.
internal static IntNativeMethods.DRAWTEXTPARAMS GetTextMargins(WindowsGraphics wg, WindowsFont font) {
// PERF: operate on a local reference rather than party directly on the thread static one.
CachedInfo currentCachedInfo = cachedMeasurementDCInfo;
if (currentCachedInfo != null && currentCachedInfo.LeftTextMargin >0 && currentCachedInfo.RightTextMargin >0 && font == currentCachedInfo.LastUsedFont)
{
// we have to return clones as DrawTextEx will modify this struct
return new IntNativeMethods.DRAWTEXTPARAMS(currentCachedInfo.LeftTextMargin,currentCachedInfo.RightTextMargin);
}
else if (currentCachedInfo == null)
{
currentCachedInfo = new CachedInfo();
cachedMeasurementDCInfo = currentCachedInfo;
}
IntNativeMethods.DRAWTEXTPARAMS drawTextParams = wg.GetTextMargins(font);
currentCachedInfo.LeftTextMargin = drawTextParams.iLeftMargin;
currentCachedInfo.RightTextMargin = drawTextParams.iRightMargin;
// returning a copy here to be consistent with the return value from the cache.
return new IntNativeMethods.DRAWTEXTPARAMS(currentCachedInfo.LeftTextMargin,currentCachedInfo.RightTextMargin);
}
internal static void ResetIfIsMeasurementDC(IntPtr hdc) {
WindowsGraphics sharedGraphics = WindowsGraphicsCacheManager.GetCurrentMeasurementGraphics();
if (sharedGraphics != null && sharedGraphics.DeviceContext != null && sharedGraphics.DeviceContext.Hdc == hdc) {
CachedInfo currentCachedInfo = cachedMeasurementDCInfo;
if (currentCachedInfo != null)
{
currentCachedInfo.UpdateFont(null);
}
}
}
/// Reset
/// clear the current cached information about the measurement dc.
internal static void Reset() {
CachedInfo currentCachedInfo = cachedMeasurementDCInfo;
if (currentCachedInfo != null)
{
currentCachedInfo.UpdateFont(null);
}
}
/// CachedInfo
/// store all the thread statics together so we dont have to fetch individual fields out of TLS
private sealed class CachedInfo
{
public WindowsFont LastUsedFont;
public int LeftTextMargin;
public int RightTextMargin;
internal void UpdateFont(WindowsFont font)
{
if (LastUsedFont != font)
{
LastUsedFont = font;
LeftTextMargin = -1;
RightTextMargin = -1;
}
}
}
}
}
#endif // OPTIMIZED_MEASUREMENTDC
// 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
- NopReturnReader.cs
- StrokeCollectionDefaultValueFactory.cs
- CngKey.cs
- FullTextState.cs
- ApplicationGesture.cs
- webclient.cs
- Cell.cs
- ConditionalExpression.cs
- EventRoute.cs
- WindowPatternIdentifiers.cs
- CodeAttachEventStatement.cs
- DataSourceHelper.cs
- ManualResetEvent.cs
- WarningException.cs
- SmtpCommands.cs
- KeyTime.cs
- MergeLocalizationDirectives.cs
- TimersDescriptionAttribute.cs
- FontWeights.cs
- IndentedWriter.cs
- SpeakCompletedEventArgs.cs
- Line.cs
- CodeIndexerExpression.cs
- ClientTargetSection.cs
- Encoder.cs
- KeyTimeConverter.cs
- LinearKeyFrames.cs
- ListItemParagraph.cs
- Size.cs
- VBIdentifierName.cs
- RijndaelManagedTransform.cs
- SecurityTokenProviderContainer.cs
- BinaryParser.cs
- NavigationPropertyEmitter.cs
- StreamSecurityUpgradeInitiatorBase.cs
- sqlnorm.cs
- TabPanel.cs
- IsolatedStorageFileStream.cs
- CompilerState.cs
- IntSecurity.cs
- DataGridViewColumnDesignTimeVisibleAttribute.cs
- XmlValueConverter.cs
- WindowsIPAddress.cs
- CodeParameterDeclarationExpressionCollection.cs
- DoubleLinkList.cs
- XDeferredAxisSource.cs
- Comparer.cs
- WebPartZoneAutoFormat.cs
- MachineKeySection.cs
- Vector3DCollection.cs
- Method.cs
- AttributeEmitter.cs
- SymbolMethod.cs
- RemotingConfigParser.cs
- GlobalEventManager.cs
- CollectionView.cs
- ProjectionCamera.cs
- EntityFunctions.cs
- ColorAnimationBase.cs
- UrlMapping.cs
- Pointer.cs
- HttpWebRequest.cs
- IODescriptionAttribute.cs
- XmlReaderSettings.cs
- SessionStateContainer.cs
- HGlobalSafeHandle.cs
- DataGridViewCellParsingEventArgs.cs
- CompilerScopeManager.cs
- ComEventsHelper.cs
- LocalsItemDescription.cs
- SqlMethods.cs
- WindowsGraphicsCacheManager.cs
- Rectangle.cs
- SingleConverter.cs
- ComponentDispatcherThread.cs
- DataObjectMethodAttribute.cs
- GridEntry.cs
- WindowPatternIdentifiers.cs
- NetworkInterface.cs
- ExtendedPropertiesHandler.cs
- MonthCalendar.cs
- PrinterSettings.cs
- RedistVersionInfo.cs
- SingleResultAttribute.cs
- ProfessionalColors.cs
- storepermissionattribute.cs
- CatalogZoneBase.cs
- GrammarBuilder.cs
- RangeValidator.cs
- DataSvcMapFileSerializer.cs
- WinInetCache.cs
- WithStatement.cs
- DisableDpiAwarenessAttribute.cs
- DataGrid.cs
- PasswordRecovery.cs
- UIAgentAsyncEndRequest.cs
- AssemblyAttributes.cs
- CodeFieldReferenceExpression.cs
- VectorAnimationUsingKeyFrames.cs
- EllipticalNodeOperations.cs