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 / ButtonRenderer.cs / 1 / ButtonRenderer.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms {
using System;
using System.Drawing;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Forms.Internal;
using System.Windows.Forms.VisualStyles;
using Microsoft.Win32;
///
///
///
/// This is a rendering class for the Button control. It works downlevel too (obviously
/// without visual styles applied.)
///
///
public sealed class ButtonRenderer {
//Make this per-thread, so that different threads can safely use these methods.
[ThreadStatic]
private static VisualStyleRenderer visualStyleRenderer = null;
private static readonly VisualStyleElement ButtonElement = VisualStyleElement.Button.PushButton.Normal;
private static bool renderMatchingApplicationState = true;
//cannot instantiate
private ButtonRenderer() {
}
///
///
///
/// If this property is true, then the renderer will use the setting from Application.RenderWithVisualStyles to
/// determine how to render.
/// If this property is false, the renderer will always render with visualstyles.
///
///
public static bool RenderMatchingApplicationState {
get {
return renderMatchingApplicationState;
}
set {
renderMatchingApplicationState = value;
}
}
private static bool RenderWithVisualStyles {
get {
return (!renderMatchingApplicationState || Application.RenderWithVisualStyles);
}
}
///
///
///
/// Returns true if the background corresponding to the given state is partially transparent, else false.
///
///
public static bool IsBackgroundPartiallyTransparent(PushButtonState state) {
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
return visualStyleRenderer.IsBackgroundPartiallyTransparent();
}
else {
return false; //for downlevel, this is false
}
}
///
///
///
/// This is just a convenience wrapper for VisualStyleRenderer.DrawThemeParentBackground. For downlevel,
/// this isn't required and does nothing.
///
///
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
public static void DrawParentBackground(Graphics g, Rectangle bounds, Control childControl) {
if (RenderWithVisualStyles) {
InitializeRenderer(0);
visualStyleRenderer.DrawParentBackground(g, bounds, childControl);
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, PushButtonState state) {
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, bool focused, PushButtonState state) {
Rectangle contentBounds;
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
contentBounds = Rectangle.Inflate(bounds, -3, -3);
}
if (focused) {
ControlPaint.DrawFocusRectangle(g, contentBounds);
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, string buttonText, Font font, bool focused, PushButtonState state) {
DrawButton(g, bounds, buttonText, font,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine,
focused, state);
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, bool focused, PushButtonState state) {
Rectangle contentBounds;
Color textColor;
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
textColor = visualStyleRenderer.GetColor(ColorProperty.TextColor);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
contentBounds = Rectangle.Inflate(bounds, -3, -3);
textColor = SystemColors.ControlText;
}
TextRenderer.DrawText(g, buttonText, font, contentBounds, textColor, flags);
if (focused) {
ControlPaint.DrawFocusRectangle(g, contentBounds);
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, Image image, Rectangle imageBounds, bool focused, PushButtonState state) {
Rectangle contentBounds;
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
visualStyleRenderer.DrawImage(g, imageBounds, image);
contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
g.DrawImage(image, imageBounds);
contentBounds = Rectangle.Inflate(bounds, -3, -3);
}
if (focused) {
ControlPaint.DrawFocusRectangle(g, contentBounds);
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, string buttonText, Font font, Image image, Rectangle imageBounds, bool focused, PushButtonState state) {
DrawButton(g, bounds, buttonText, font,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine,
image, imageBounds, focused, state);
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, PushButtonState state) {
Rectangle contentBounds;
Color textColor;
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
visualStyleRenderer.DrawImage(g, imageBounds, image);
contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
textColor = visualStyleRenderer.GetColor(ColorProperty.TextColor);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
g.DrawImage(image, imageBounds);
contentBounds = Rectangle.Inflate(bounds, -3, -3);
textColor = SystemColors.ControlText;
}
TextRenderer.DrawText(g, buttonText, font, contentBounds, textColor, flags);
if (focused) {
ControlPaint.DrawFocusRectangle(g, contentBounds);
}
}
internal static ButtonState ConvertToButtonState(PushButtonState state) {
switch (state) {
case PushButtonState.Pressed:
return ButtonState.Pushed;
case PushButtonState.Disabled:
return ButtonState.Inactive;
default:
return ButtonState.Normal;
}
}
private static void InitializeRenderer(int state) {
if (visualStyleRenderer == null) {
visualStyleRenderer = new VisualStyleRenderer(ButtonElement.ClassName, ButtonElement.Part, state);
}
else {
visualStyleRenderer.SetParameters(ButtonElement.ClassName, ButtonElement.Part, state);
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Windows.Forms {
using System;
using System.Drawing;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Forms.Internal;
using System.Windows.Forms.VisualStyles;
using Microsoft.Win32;
///
///
///
/// This is a rendering class for the Button control. It works downlevel too (obviously
/// without visual styles applied.)
///
///
public sealed class ButtonRenderer {
//Make this per-thread, so that different threads can safely use these methods.
[ThreadStatic]
private static VisualStyleRenderer visualStyleRenderer = null;
private static readonly VisualStyleElement ButtonElement = VisualStyleElement.Button.PushButton.Normal;
private static bool renderMatchingApplicationState = true;
//cannot instantiate
private ButtonRenderer() {
}
///
///
///
/// If this property is true, then the renderer will use the setting from Application.RenderWithVisualStyles to
/// determine how to render.
/// If this property is false, the renderer will always render with visualstyles.
///
///
public static bool RenderMatchingApplicationState {
get {
return renderMatchingApplicationState;
}
set {
renderMatchingApplicationState = value;
}
}
private static bool RenderWithVisualStyles {
get {
return (!renderMatchingApplicationState || Application.RenderWithVisualStyles);
}
}
///
///
///
/// Returns true if the background corresponding to the given state is partially transparent, else false.
///
///
public static bool IsBackgroundPartiallyTransparent(PushButtonState state) {
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
return visualStyleRenderer.IsBackgroundPartiallyTransparent();
}
else {
return false; //for downlevel, this is false
}
}
///
///
///
/// This is just a convenience wrapper for VisualStyleRenderer.DrawThemeParentBackground. For downlevel,
/// this isn't required and does nothing.
///
///
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")]
public static void DrawParentBackground(Graphics g, Rectangle bounds, Control childControl) {
if (RenderWithVisualStyles) {
InitializeRenderer(0);
visualStyleRenderer.DrawParentBackground(g, bounds, childControl);
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, PushButtonState state) {
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, bool focused, PushButtonState state) {
Rectangle contentBounds;
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
contentBounds = Rectangle.Inflate(bounds, -3, -3);
}
if (focused) {
ControlPaint.DrawFocusRectangle(g, contentBounds);
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, string buttonText, Font font, bool focused, PushButtonState state) {
DrawButton(g, bounds, buttonText, font,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine,
focused, state);
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, bool focused, PushButtonState state) {
Rectangle contentBounds;
Color textColor;
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
textColor = visualStyleRenderer.GetColor(ColorProperty.TextColor);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
contentBounds = Rectangle.Inflate(bounds, -3, -3);
textColor = SystemColors.ControlText;
}
TextRenderer.DrawText(g, buttonText, font, contentBounds, textColor, flags);
if (focused) {
ControlPaint.DrawFocusRectangle(g, contentBounds);
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, Image image, Rectangle imageBounds, bool focused, PushButtonState state) {
Rectangle contentBounds;
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
visualStyleRenderer.DrawImage(g, imageBounds, image);
contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
g.DrawImage(image, imageBounds);
contentBounds = Rectangle.Inflate(bounds, -3, -3);
}
if (focused) {
ControlPaint.DrawFocusRectangle(g, contentBounds);
}
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, string buttonText, Font font, Image image, Rectangle imageBounds, bool focused, PushButtonState state) {
DrawButton(g, bounds, buttonText, font,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine,
image, imageBounds, focused, state);
}
///
///
///
/// Renders a Button control.
///
///
public static void DrawButton(Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, PushButtonState state) {
Rectangle contentBounds;
Color textColor;
if (RenderWithVisualStyles) {
InitializeRenderer((int)state);
visualStyleRenderer.DrawBackground(g, bounds);
visualStyleRenderer.DrawImage(g, imageBounds, image);
contentBounds = visualStyleRenderer.GetBackgroundContentRectangle(g, bounds);
textColor = visualStyleRenderer.GetColor(ColorProperty.TextColor);
}
else {
ControlPaint.DrawButton(g, bounds, ConvertToButtonState(state));
g.DrawImage(image, imageBounds);
contentBounds = Rectangle.Inflate(bounds, -3, -3);
textColor = SystemColors.ControlText;
}
TextRenderer.DrawText(g, buttonText, font, contentBounds, textColor, flags);
if (focused) {
ControlPaint.DrawFocusRectangle(g, contentBounds);
}
}
internal static ButtonState ConvertToButtonState(PushButtonState state) {
switch (state) {
case PushButtonState.Pressed:
return ButtonState.Pushed;
case PushButtonState.Disabled:
return ButtonState.Inactive;
default:
return ButtonState.Normal;
}
}
private static void InitializeRenderer(int state) {
if (visualStyleRenderer == null) {
visualStyleRenderer = new VisualStyleRenderer(ButtonElement.ClassName, ButtonElement.Part, state);
}
else {
visualStyleRenderer.SetParameters(ButtonElement.ClassName, ButtonElement.Part, state);
}
}
}
}
// 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
- DriveInfo.cs
- ResourceBinder.cs
- ArraySortHelper.cs
- ConfigurationErrorsException.cs
- TextChange.cs
- BooleanStorage.cs
- COSERVERINFO.cs
- SkewTransform.cs
- DEREncoding.cs
- SQLByteStorage.cs
- WebMessageEncodingElement.cs
- UpdateCommand.cs
- AssemblyUtil.cs
- GatewayDefinition.cs
- ViewCellSlot.cs
- DBSqlParserTableCollection.cs
- Util.cs
- WSDualHttpSecurity.cs
- RequestResponse.cs
- Configuration.cs
- SessionPageStateSection.cs
- VirtualPath.cs
- ModelItemKeyValuePair.cs
- TemplatedMailWebEventProvider.cs
- GridPatternIdentifiers.cs
- StatusBarItemAutomationPeer.cs
- Mapping.cs
- InstanceDataCollectionCollection.cs
- COM2PropertyDescriptor.cs
- SapiRecoInterop.cs
- followingquery.cs
- InvalidEnumArgumentException.cs
- ErrorInfoXmlDocument.cs
- BuildManager.cs
- RecognizerInfo.cs
- LabelEditEvent.cs
- Effect.cs
- DrawingGroup.cs
- MaskedTextBox.cs
- Rect3DValueSerializer.cs
- XhtmlBasicValidationSummaryAdapter.cs
- HwndKeyboardInputProvider.cs
- DataMisalignedException.cs
- WindowsIPAddress.cs
- BadImageFormatException.cs
- MarginCollapsingState.cs
- ClientConfigPaths.cs
- IDQuery.cs
- BamlRecordHelper.cs
- RawMouseInputReport.cs
- DefaultExpressionVisitor.cs
- TranslateTransform3D.cs
- Directory.cs
- XmlConverter.cs
- FigureParaClient.cs
- PropertyValidationContext.cs
- ConstraintManager.cs
- ActivityDesignerResources.cs
- BaseCAMarshaler.cs
- Documentation.cs
- EntityDataSourceViewSchema.cs
- storepermission.cs
- ApplicationActivator.cs
- Command.cs
- TypeForwardedToAttribute.cs
- Sequence.cs
- CreatingCookieEventArgs.cs
- BaseComponentEditor.cs
- PtsPage.cs
- SectionInput.cs
- DeflateStream.cs
- ButtonField.cs
- DataGridViewRowStateChangedEventArgs.cs
- Nodes.cs
- AnonymousIdentificationModule.cs
- PropertyPathConverter.cs
- HttpCookie.cs
- SqlBinder.cs
- SwitchAttribute.cs
- CaseStatementProjectedSlot.cs
- GeneratedView.cs
- Drawing.cs
- FilePresentation.cs
- NonVisualControlAttribute.cs
- Brush.cs
- PropertyIDSet.cs
- SecurityUtils.cs
- StubHelpers.cs
- PageWrapper.cs
- ThreadAttributes.cs
- ToolTipAutomationPeer.cs
- DataGridViewTopRowAccessibleObject.cs
- ProcessingInstructionAction.cs
- DataGridViewRowCancelEventArgs.cs
- WrappedIUnknown.cs
- BaseInfoTable.cs
- DirectoryObjectSecurity.cs
- SignedPkcs7.cs
- RoutedEventArgs.cs
- MailWriter.cs