reno.button
No notes
Syntax:
C#
/**************************************************************** * Copyright (C) 2009 NISSAN MOTOR CO., LTD. * All rights reserved * * $Workfile: CFButton.cs $ * * $Author: Zhangyi $ * $Date: 09/11/20 15:22 $ * $Revision: 9 $ * * Revision Desc: ****************************************************************/ using System; using System.ComponentModel; using System.Windows.Forms; using System.Collections; using System.Drawing; namespace DiagnosticTool.Application.Framework.GUI.CommonControls { /// <summary> /// This desiger class fixes the height and remove some properties from the property box. /// </summary> internal class CFButtonDesigner : System.Windows.Forms.Design.ControlDesigner { /// <summary> /// Adjusts the set of properties the component exposes through a <see cref="T:System.ComponentModel.TypeDescriptor"/>. /// </summary> /// <param name="properties">An <see cref="T:System.Collections.IDictionary"/> containing the properties for the class of the component.</param> protected override void PreFilterProperties(IDictionary properties) { base.PreFilterProperties(properties); /* * Remove properties */ properties.Remove("BackgroundImage"); // Other four properties will instead of this property. properties.Remove("ForeColor"); // ForeColor will be fixed. properties.Remove("FlatStyle"); properties.Remove("TabStop"); properties.Remove("UseMnemonic"); } } /// <summary> /// CFButton is the base class of CFAAButton. /// Four background image for Normal, MouseDown, Normal+Disable and MouseDown+Disable can be set free. /// Also, its size can be set free. /// Warning: Background images are stretch. /// </summary> [DesignerAttribute(typeof(CFButtonDesigner))] [DefaultEvent("Click")] [ToolboxBitmap(typeof(CFButton), "Resources.CFButton.bmp")] public partial class CFButton : Button { #region -Constructor- /// <summary> /// Initializes a new instance of the <see cref="CFButton"/> class. /// </summary> public CFButton() { InitializeComponent(); } /// <summary> /// Initializes a new instance of the <see cref="CFButton"/> class. /// </summary> /// <param name="container">The container.</param> public CFButton(IContainer container) { if (container != null) { container.Add(this); InitializeComponent(); //this.ParentChanged += new EventHandler(CFButton_ParentChanged); } } #endregion #region -Public Properties- /// <summary> /// Gets or sets the background image layout as defined in the <see cref="T:System.Windows.Forms.ImageLayout"/> enumeration. /// </summary> /// <returns>One of the values of <see cref="T:System.Windows.Forms.ImageLayout"/> (<see cref="F:System.Windows.Forms.ImageLayout.Center"/> , <see cref="F:System.Windows.Forms.ImageLayout.None"/>, <see cref="F:System.Windows.Forms.ImageLayout.Stretch"/>, <see cref="F:System.Windows.Forms.ImageLayout.Tile"/>, or <see cref="F:System.Windows.Forms.ImageLayout.Zoom"/>). <see cref="F:System.Windows.Forms.ImageLayout.Tile"/> is the default value.</returns> [Category("CommonControls"), DefaultValue(ImageLayout.Stretch), Description("Gets or sets the background image layout")] public override ImageLayout BackgroundImageLayout { get { return base.BackgroundImageLayout; } set { base.BackgroundImageLayout = value; } } /// <summary> /// Gets or sets a value indicating whether [self transparent]. /// </summary> /// <value><c>true</c> if [self transparent]; otherwise, <c>false</c>.</value> [Browsable(false)] internal bool SelfTransparent { get { return m_bSelfTransparent; } set { m_bSelfTransparent = value; } } /// <summary> /// Gets or sets the appearance for the CFButton /// </summary> /// <value>The appearance.</value> [Category("CommonControls"), DefaultValue(ButtonAppearance.PushButton), Description("Controls whether the CFButton appears as a PushButton or a ToggleButton")] public virtual ButtonAppearance Appearance { get { return m_buttonAppearance; } set { m_buttonAppearance = value; // Redraw background image and reset fore color this.ChangeBackgroundImageAndForeColor(); } } /// <summary> /// Gets or sets the foreground color of the control. /// </summary> /// <value></value> /// <returns>The foreground <see cref="T:System.Drawing.Color"/> of the control. The default is the value of the <see cref="P:System.Windows.Forms.Control.DefaultForeColor"/> property.</returns> /// <PermissionSet> /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/> /// </PermissionSet> [Category("CommonControls"), Description("Gets or sets the foreground color of the control.")] public override Color ForeColor { get { return base.ForeColor; } set { /* * PushButton ForeColor is decided by MouseState * ToggleButton also can be decided by Check State */ if (m_mouseState == MouseState.MouseDown || ((m_buttonAppearance == ButtonAppearance.ToggleButton) && (true == m_bIsChecked))) { // Fixed Color base.ForeColor = DefaultSettings.N_RED; } else { // Fixed Color base.ForeColor = DefaultSettings.DEFAULT_FORE_COLOR; } } } /// <summary> /// Gets or sets the check state for only ToggleButton /// To PushButton, this property is useless. /// <value>true</value> means the ToggleButton is checked down /// <value>false</value> means the ToggleButton is not checked. This is also the default state. /// If CFButton appears as a ToggleButton, sets checkstate true or mouse down can make it appears checked down. /// Only sets checkstate false can make a checked ToggleButton to appear normal. /// </summary> [Category("CommonControls"), DefaultValue(false), Description("Controls whether the toggle CFButton was checked down or not")] public virtual bool CheckState { get { return m_bIsChecked; } set { m_bIsChecked = value; // Redraw background image and reset fore color this.ChangeBackgroundImageAndForeColor(); } } /// <summary> /// Gets or sets the transparence color for the background images. /// </summary> [Category("CommonControls"), DefaultValue(typeof(Color),"0xB5,0X10,0XC6"), Description("The transparence color for the background images")] public Color TransparenceColor { get { return m_colTransparenceColor; } set { // Sets value m_colTransparenceColor = value; // Transparents the color for all background images this.TransparentColor(m_colTransparenceColor); // Redraw background image and reset fore color this.ChangeBackgroundImageAndForeColor(); // Refresh this.Invalidate(); } } /// <summary> /// Gets or sets the background image for normal state /// </summary> [Category("CommonControls"), DefaultValue(null), Description("The background image for normal state")] public Bitmap BackgroundImageNormal { get { return m_bmBackgroundImageNormal; } set { // Restore the original image resource m_bmResourceNormal = value; // Original image resource can be newed if (m_bmResourceNormal != null) { // Remove border if (this.FlatAppearance.BorderSize != 0) { this.FlatAppearance.BorderSize = 0; } // Copy the original image resource to background bitmap m_bmBackgroundImageNormal = new Bitmap(m_bmResourceNormal); if (true == m_bSelfTransparent) { // Transparence m_bmBackgroundImageNormal.MakeTransparent(m_colTransparenceColor); } } else { m_bmBackgroundImageNormal = null; } // Redraw background image and reset fore color this.ChangeBackgroundImageAndForeColor(); } } /// <summary> /// Gets or sets the background image for normal state and the button is disabled /// </summary> [Category("CommonControls"), DefaultValue(null), Description("The background image for normal state and the button is disabled")] public Bitmap BackgroundImageNormalDisabled { get { return m_bmBackgroundImageNormalDisabled; } set { // Restore the original image resource m_bmResourceNormalDisabled = value; // Original image resource can be newed if (m_bmResourceNormalDisabled != null) { // Remove border if (this.FlatAppearance.BorderSize != 0) { this.FlatAppearance.BorderSize = 0; } // Copy the original image resource to background bitmap m_bmBackgroundImageNormalDisabled = new Bitmap(m_bmResourceNormalDisabled); if (true == m_bSelfTransparent) { // Transparence m_bmBackgroundImageNormalDisabled.MakeTransparent(m_colTransparenceColor); } } else { m_bmBackgroundImageNormalDisabled = null; } // Redraw background image and reset fore color this.ChangeBackgroundImageAndForeColor(); } } /// <summary> /// Gets or sets the background image for mouse down state. /// </summary> [Category("CommonControls"), DefaultValue(null), Description("The background image for mouse down state")] public Bitmap BackgroundImageMouseDown { get { return m_bmBackgroundImageMouseDown; } set { // Restore the original image resource m_bmResourceMouseDown = value; // Original image resource can be newed if (m_bmResourceMouseDown != null) { // Remove border if (this.FlatAppearance.BorderSize != 0) { this.FlatAppearance.BorderSize = 0; } // Copy the original image resource to background bitmap m_bmBackgroundImageMouseDown = new Bitmap(m_bmResourceMouseDown); if (true == m_bSelfTransparent) { // Transparence m_bmBackgroundImageMouseDown.MakeTransparent(m_colTransparenceColor); } } else { m_bmBackgroundImageMouseDown = null; } // Redraw background image and reset fore color this.ChangeBackgroundImageAndForeColor(); } } /// <summary> /// Gets or sets the background image for mouse down state and the button is disabled /// </summary> [Category("CommonControls"), DefaultValue(null), Description("The background image for mouse down state and the button is disabled")] public Bitmap BackgroundImageMouseDownDisabled { get { return m_bmBackgroundImageMouseDownDisabled; } set { // Restore the original image resource m_bmResourceMouseDownDisabled = value; // Original image resource can be newed if (value != null) { // Remove border if (this.FlatAppearance.BorderSize != 0) { this.FlatAppearance.BorderSize = 0; } // Copy the original image resource to background bitmap m_bmBackgroundImageMouseDownDisabled = new Bitmap(m_bmResourceMouseDownDisabled); if (true == m_bSelfTransparent) { // Transparence m_bmBackgroundImageMouseDownDisabled.MakeTransparent(m_colTransparenceColor); } } else { m_bmBackgroundImageMouseDownDisabled = null; } // Redraw background image and reset fore color this.ChangeBackgroundImageAndForeColor(); } } /// <summary> /// Gets or sets a value indicating whether [mouse event enabled]. /// </summary> /// <value><c>true</c> if [mouse event enabled]; otherwise, <c>false</c>.</value> [Category("CommonControls"), DefaultValue(true), Description("Gets or sets a value indicating whether [mouse event enabled].")] public bool MouseEventEnabled { get { return m_bMouseEventEnabled; } set { m_bMouseEventEnabled = value; } } #endregion #region -Protected Methods- /// <summary> /// Changes the background image and fore color by check state, mouse state, enabled state and background images. /// Always works when Button state changed or background image changed. /// </summary> protected virtual void ChangeBackgroundImageAndForeColor() { // Button is disabled if (false == this.Enabled) { // PushButton State if (m_buttonAppearance == ButtonAppearance.PushButton) { // Change background image and fore color this.BackgroundImage = m_bmBackgroundImageNormalDisabled; this.ForeColor = DefaultSettings.DISABLED_FORE_COLOR; } // ToggleButton State else { // Not checked down if (false == m_bIsChecked) { // Change background image and fore color this.BackgroundImage = m_bmBackgroundImageNormalDisabled; this.ForeColor = DefaultSettings.DISABLED_FORE_COLOR; } // Checked down else { // Change background image and fore color this.BackgroundImage = m_bmBackgroundImageMouseDownDisabled; this.ForeColor = DefaultSettings.DISABLED_FORE_COLOR; } } } // Button is Enabled else { // PushButton State if (m_buttonAppearance == ButtonAppearance.PushButton) { // When mouse state is mouse down if (m_mouseState == MouseState.MouseDown) { // Change background image and fore color this.BackgroundImage = m_bmBackgroundImageMouseDown; this.ForeColor = DefaultSettings.N_RED; } // When mouse state is normal else { // Change background image and fore color this.BackgroundImage = m_bmBackgroundImageNormal; this.ForeColor = DefaultSettings.DEFAULT_FORE_COLOR; } } // ToggleButton State else { // Not checked down if (false == m_bIsChecked) { // Change background image and fore color this.BackgroundImage = m_bmBackgroundImageNormal; base.ForeColor = DefaultSettings.DEFAULT_FORE_COLOR; } // Checked down else { // Change background image and fore color this.BackgroundImage = m_bmBackgroundImageMouseDown; this.ForeColor = DefaultSettings.N_RED; } } } } /// <summary> /// Transparent color for all background images. /// Works when transparence color changed. /// </summary> /// <param name="colTransparenceColor">The transparence color</param> protected void TransparentColor(Color colTransparenceColor) { if (m_bmResourceNormal != null) { /* * Copy the original image to background image * Transparence on the new background image * If no "COPY" and "NEW", transprance may be on the image which has been transparented. */ m_bmBackgroundImageNormal = new Bitmap(m_bmResourceNormal); // Transparence m_bmBackgroundImageNormal.MakeTransparent(colTransparenceColor); } if (m_bmResourceMouseDown != null) { m_bmBackgroundImageMouseDown = new Bitmap(m_bmResourceMouseDown); // Transparence m_bmBackgroundImageMouseDown.MakeTransparent(colTransparenceColor); } if (m_bmResourceNormalDisabled != null) { m_bmBackgroundImageNormalDisabled = new Bitmap(m_bmResourceNormalDisabled); // Transparence m_bmBackgroundImageNormalDisabled.MakeTransparent(colTransparenceColor); } if (m_bmResourceMouseDownDisabled != null) { m_bmBackgroundImageMouseDownDisabled = new Bitmap(m_bmResourceMouseDownDisabled); // Transparence m_bmBackgroundImageMouseDownDisabled.MakeTransparent(colTransparenceColor); } } /// <summary> /// Mouse down to change the image. /// </summary> /// <param name="mevent">the MouseEvent</param> protected override void OnMouseDown(MouseEventArgs e) { // Right click down is useless. if ((e.Button & MouseButtons.Left) != MouseButtons.Left) { return; } // Mouse event enabled is false if (false == m_bMouseEventEnabled) { return; } // Catch capture this.Capture = true; if (m_mouseState != MouseState.MouseDown) { // Push Button appearance is decided by Mouse State if (m_buttonAppearance == ButtonAppearance.PushButton) { m_mouseState = MouseState.MouseDown; // Redraw background images this.ChangeBackgroundImageAndForeColor(); } } base.OnMouseDown(e); } /// <summary> /// Mouse up to change the image. /// </summary> /// <param name="mevent">the MouseEvent</param> protected override void OnMouseUp(MouseEventArgs e) { if (this.Capture == false) { return; } // Mouse event enabled is false if (false == m_bMouseEventEnabled) { return; } if (m_mouseState != MouseState.Normal) { m_mouseState = MouseState.Normal; } this.Capture = false; // Redraw background image if( m_buttonAppearance == ButtonAppearance.PushButton ) { this.ChangeBackgroundImageAndForeColor(); } else { if (!(e.X < 0 || e.X > this.Width || e.Y < 0 || e.Y > this.Height)) { if (false == m_bIsChecked) { m_bIsChecked = true; } else { m_bIsChecked = false; } } // Redraw background images this.ChangeBackgroundImageAndForeColor(); } base.OnMouseUp(e); } /// <summary> /// Raises the <see cref="E:System.Windows.Forms.Control.MouseMove"/> event. /// </summary> /// <param name="e">A <see cref="T:System.Windows.Forms.MouseEventArgs"/> that contains the event data.</param> protected override void OnMouseMove(MouseEventArgs e) { if (m_mouseState == MouseState.MouseDown) { if (e.X < 0 || e.X > this.Width || e.Y < 0 || e.Y > this.Height) { if( m_buttonAppearance == ButtonAppearance.PushButton ) { this.OnMouseUp( e ); } return; } } base.OnMouseMove(e); } /// <summary> /// Enabled property changing to change the image. /// </summary> /// <param name="mevent">the MouseEvent</param> protected override void OnEnabledChanged(EventArgs e) { if (true == this.Enabled && m_buttonAppearance == ButtonAppearance.PushButton) { if (m_mouseState != MouseState.Normal) { m_mouseState = MouseState.Normal; } } // Redraw background image. this.ChangeBackgroundImageAndForeColor(); base.OnEnabledChanged(e); } /// <summary> /// Override of show focus cue or not /// </summary> protected override bool ShowFocusCues { get { //Always hide focus cue return false; } } /// <summary> /// Remove base mouse enter event. /// </summary> protected override void OnMouseEnter(EventArgs e) { // Do nothing } /// <summary> /// Remove base mouse leave event. /// </summary> protected override void OnMouseLeave(EventArgs e) { // Do nothing } /// <summary> /// Raises the <see cref="E:System.Windows.Forms.Control.GotFocus"/> event. /// </summary> /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param> protected override void OnGotFocus(EventArgs e) { // Do nothing } /// <summary> /// Raises the <see cref="E:System.Windows.Forms.Control.Enter"/> event. /// </summary> /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param> protected override void OnEnter(EventArgs e) { // Do nothing } /// <summary> /// Processes Windows messages. /// </summary> /// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message"/> to process.</param> protected override void WndProc(ref Message m) { if (m.Msg == WM_KEYDOWN) { return; } base.WndProc(ref m); } /// <summary> /// Raises the <see cref="M:System.Windows.Forms.ButtonBase.OnKeyUp(System.Windows.Forms.KeyEventArgs)"/> event. /// </summary> /// <param name="kevent">A <see cref="T:System.Windows.Forms.KeyEventArgs"/> that contains the event data.</param> protected override void OnKeyDown(KeyEventArgs kevent) { // Do nothing } #endregion #region -Other Fields- /// <summary> /// True : self transparent /// False : the bitmap which should be make transparent /// </summary> private bool m_bSelfTransparent = true; /// <summary> /// Mouse state /// </summary> internal enum MouseState { Normal, MouseDown } /// <summary> /// Only for toggle button /// <value>true</value> means button checked down, otherwise not. /// </summary> internal bool m_bIsChecked = false; /// <summary> /// current mouse state /// </summary> internal MouseState m_mouseState = MouseState.Normal; /// <summary> /// Button appearance /// PushButton like a normal button. /// ToggleButton once be checked down , mouse down agein is useless until its check state is changed to false. /// </summary> internal ButtonAppearance m_buttonAppearance = ButtonAppearance.PushButton; /// <summary> /// The transparence color /// </summary> internal Color m_colTransparenceColor = DefaultSettings.TRANSPARENT_COLOR; /// <summary> /// Bitmap, decides the NORMAL background image. /// </summary> internal Bitmap m_bmBackgroundImageNormal = null; /// <summary> /// Bitmap, decides the MOUSE DOWN background image. /// </summary> internal Bitmap m_bmBackgroundImageMouseDown = null; /// <summary> /// Bitmap, decides the NORMAL+DISABLED background image. /// </summary> internal Bitmap m_bmBackgroundImageNormalDisabled = null; /// <summary> /// Bitmap, decides the MOUSE DOWN+DISABLED background image. /// </summary> internal Bitmap m_bmBackgroundImageMouseDownDisabled = null; /// <summary> /// Bitmap to store original NORMAL image resource. /// </summary> internal Bitmap m_bmResourceNormal = null; /// <summary> /// Bitmap to store original MOUSE DOWN image resource. /// </summary> internal Bitmap m_bmResourceMouseDown = null; /// <summary> /// Bitmap to store original NORMAL+DISABLED image resource. /// </summary> internal Bitmap m_bmResourceNormalDisabled = null; /// <summary> /// Bitmap to store original MOUSE DOWN+DISABLED image resource. /// </summary> internal Bitmap m_bmResourceMouseDownDisabled = null; /// <summary> /// If the bool is false, the mouse down event is useless. /// </summary> private bool m_bMouseEventEnabled = true; /// <summary> /// Key press /// </summary> public const int WM_KEYDOWN = 0x0100; #endregion } }