using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using static Vanara.PInvoke.User32;
namespace Vanara.Windows.Forms;
///
/// Implements a CommandLink button that can be used in WinForms user interfaces.
///
[ToolboxBitmap(typeof(Button))]
public abstract class VistaButtonBase : Button
{
private Icon icon;
private bool showShield;
///
/// Initializes a new instance of the class.
///
protected VistaButtonBase()
{
FlatStyle = FlatStyle.System;
}
/*///
/// Gets or sets the flat style.
///
///
/// The flat style.
///
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DefaultValue(typeof(FlatStyle), "System")]
public new FlatStyle FlatStyle
{
get { return base.FlatStyle; }
set { base.FlatStyle = value; }
}*/
///
/// Gets or sets the icon that is displayed on a button control.
///
[Description("Gets or sets the icon that is displayed on a button control."), Category("Appearance"), DefaultValue(null)]
public Icon Icon
{
get => icon;
set
{
icon = value;
if (value != null)
Image = null;
ShowShield = false;
SetImage();
}
}
///
/// Gets or sets the image that is displayed on a button control.
///
///
///
///
///
///
///
[Description("Gets or sets the image that is displayed on a button control."), Category("Appearance"), DefaultValue(null)]
public new Image Image
{
get => base.Image;
set
{
base.Image = value;
if (value != null)
Icon = null;
ShowShield = false;
SetImage();
}
}
///
/// Gets or sets a value indicating whether to display an elevated shield icon.
///
///
/// true if showing shield icon; otherwise, false.
///
[Description("Gets or sets whether if the control should use an elevated shield icon."), Category("Appearance"), DefaultValue(false)]
public bool ShowShield
{
get => showShield; set
{
if (showShield != value && IsHandleCreated)
{
showShield = value;
this.SetElevationRequiredState(value);
}
}
}
internal static bool IsPlatformSupported { get; } = Environment.OSVersion.Version.Major >= 6;
///
/// Raises the event.
///
/// An that contains the event data.
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (showShield)
SetShowShield();
if (Image != null || icon != null)
SetImage();
}
private void SetShowShield()
{
if (!IsHandleCreated) return;
SendMessage(Handle, (uint)ButtonMessage.BCM_SETSHIELD, IntPtr.Zero, showShield ? new IntPtr(1) : IntPtr.Zero);
}
///
/// Refreshes the image displayed on the button
///
private void SetImage()
{
if (!IsHandleCreated) return;
var iconhandle = IntPtr.Zero;
if (Image != null)
iconhandle = new Bitmap(Image).GetHicon();
else if (icon != null)
iconhandle = Icon.Handle;
const uint BM_SETIMAGE = 0xF7;
SendMessage(Handle, BM_SETIMAGE, (IntPtr)1, iconhandle);
}
}