123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.ComponentModel;
- using CustomControls.HelperClasses;
- using System.Drawing.Design;
- namespace CustomControls.Win32Controls
- {
- public class PushButton:Button
- {
- #region "Variables"
-
- private Enumerations.ButtonState _State=Enumerations.ButtonState.Normal;
- private Image _Image= null;
- private ContentAlignment _TextAlign=ContentAlignment.MiddleLeft;
- private ContentAlignment _ImageAlign=ContentAlignment.MiddleRight;
- private StringFormat strFormat;
-
-
- #endregion
- #region "Properties"
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public override Color ForeColor
- {
- get
- {
- return CustomControls.BaseClasses.AppColors.TextColor;
- }
- set
- {
-
- }
- }
- [Browsable(false)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
- public override Color BackColor
- {
- get
- {
- return CustomControls.BaseClasses.AppColors.ControlColor;
- }
- set
- {
-
- }
- }
- override protected System.Drawing.Size DefaultSize
- {
- get{return new System.Drawing.Size(100, 20);}
- }
- [Category("Appearance")]
- [DefaultValue(typeof(System.Drawing.Image),"null")]
- public new Image Image
- {
- get{return _Image;}
- set
- {
- if (value !=_Image)
- {
- _Image= value;
- Invalidate();
- }
- }
- }
-
- protected virtual Enumerations.ButtonState State
- {
- get{return _State;}
- set
- {
- if(value!=_State)
- {
- _State= value;
- Invalidate();
- }
- }
- }
-
- [Category("Appearance")]
- [DefaultValue(typeof(ContentAlignment),"MiddleLeft")]
- public override ContentAlignment TextAlign
- {
- get{return _TextAlign;}
- set
- {
- if (value!=_TextAlign)
- {
- _TextAlign= value;
- Invalidate();
- }
- }
- }
- [Category("Appearance")]
- [DefaultValue(typeof(ContentAlignment),"MiddleRight")]
- public new ContentAlignment ImageAlign
- {
- get{return _ImageAlign;}
- set
- {
- if (value!=_ImageAlign)
- {
- _ImageAlign= value;
- Invalidate();
- }
- }
- }
- public override string Text
- {
- get{return base.Text;}
- set
- {
- if (value!=base.Text)
- {
- base.Text= value;
- Invalidate();
- base.OnTextChanged(new EventArgs());
- }
- }
- }
-
-
- #endregion
- #region "Constructor"
-
- public PushButton()
- {
- SetStyle(ControlStyles.UserPaint, true);
- SetStyle(ControlStyles.AllPaintingInWmPaint, true);
- SetStyle(ControlStyles.DoubleBuffer, true);
- strFormat= new StringFormat(StringFormatFlags.NoWrap);
- strFormat.Alignment=StringAlignment.Center;
- }
- #endregion
- #region "Overrides"
- protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
- {
- PaintBackground(e.Graphics,ClientRectangle);
- if (Image!=null)
- {
- DrawImage(e.Graphics,GetImageRectangle(e.Graphics));
- }
- if (Text!=string.Empty)
- {
- DrawText(e.Graphics,GetTextRect(e.Graphics));
- }
-
- }
-
-
- protected override void OnResize(System.EventArgs e)
- {
- base.OnResize(e);
- Invalidate();
- }
-
- protected override void OnMouseEnter(EventArgs e)
- {
- base.OnMouseEnter(e);
- State= Enumerations.ButtonState.Hot;
-
- }
-
- protected override void OnMouseLeave(EventArgs e)
- {
-
- base.OnMouseLeave(e);
- State= Enumerations.ButtonState.Normal;
-
-
- }
-
-
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
- Focus();
- State= Enumerations.ButtonState.Pushed;
-
- }
-
- protected override void OnMouseUp(MouseEventArgs e)
- {
- State= Enumerations.ButtonState.Hot;
- base.OnMouseUp(e);
- }
-
- protected override void OnGotFocus(EventArgs e)
- {
- base.OnGotFocus(e);
- Invalidate();
- }
-
-
- protected override void OnLostFocus(EventArgs e)
- {
- base.OnLostFocus(e);
- State= Enumerations.ButtonState.Normal;
- Invalidate();
- }
- protected override void OnKeyDown(KeyEventArgs e)
- {
- base.OnKeyDown (e);
- if(e.KeyData==Keys.Space)
- {
- State=Enumerations.ButtonState.Pushed;
- }
- else if (e.KeyData== Keys.Enter)
- {
- base.OnClick(new EventArgs());
- }
- }
- protected override void OnKeyUp(KeyEventArgs e)
- {
- if(e.KeyData==Keys.Space)
- {
- if (RectangleToScreen(DisplayRectangle).Contains(Cursor.Position))
- {State=Enumerations.ButtonState.Hot; }
- else
- {State=Enumerations.ButtonState.Normal;}
-
- base.OnClick(new EventArgs());
- }
- else{base.OnKeyUp(e);}
- }
-
- #endregion
- #region "Implementation"
- protected virtual void PaintBackground(Graphics g, Rectangle bounds)
- {
- Color BackColor=(this.Parent!=null)?Parent.BackColor:CustomControls.BaseClasses.AppColors.ControlColor;
- Color BorderColor=CustomControls.BaseClasses.AppColors.HighlightColor;
-
- if(Enabled)
- {
- switch (State)
- {
- case Enumerations.ButtonState.Normal:
- {
- if (Focused)
- {
- BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
- }
- break;
- }
- case Enumerations.ButtonState.Hot:
- {
-
- BackColor=CustomControls.BaseClasses.AppColors.HighlightColor;
- BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
- break;
- }
- case Enumerations.ButtonState.Pushed:
- {
- BackColor=CustomControls.BaseClasses.AppColors.HighlightColorDark;
- BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
- break;
- }
- }
-
-
- }
- else
- {
- BackColor=CustomControls.BaseClasses.AppColors.ControlColor;
- BorderColor=CustomControls.BaseClasses.AppColors.ToolbarBackColor;
- }
- using( SolidBrush BackBrush= new SolidBrush(Color.White))
- {
- if(State==Enumerations.ButtonState.Hot || State==Enumerations.ButtonState.Pushed ){g.FillRectangle(BackBrush,bounds);}
- BackBrush.Color=BackColor ;
- g.FillRectangle(BackBrush,bounds);
-
- }
- using(Pen BorderPen = new Pen(BorderColor))
- {
- g.DrawRectangle(BorderPen,new Rectangle(bounds.X,bounds.Y,bounds.Width-1,bounds.Height-1));
- }
-
- }
-
- protected virtual void DrawText(Graphics g,Rectangle bounds)
- {
- using (SolidBrush brush= new SolidBrush(SystemColors.ControlText))
- {
- StringFormat StrFormat= new StringFormat(StringFormatFlags.NoWrap);
- StrFormat.Alignment=StringAlignment.Center;
-
- if (Enabled)
- {
- switch (State)
- {
- case Enumerations.ButtonState.Normal:
-
- case Enumerations.ButtonState.Hot:
- {
- g.DrawString(Text,Font,brush,bounds,StrFormat );
- break;
- }
- case Enumerations.ButtonState.Pushed:
- {
- brush.Color=SystemColors.HighlightText;
- g.DrawString(Text,Font,brush,bounds,StrFormat );
- break;
- }
- }
- }
- else
- {
- brush.Color=SystemColors.GrayText;
- g.DrawString(Text,Font,brush,bounds,StrFormat);
- }
- }
- }
- protected virtual void DrawImage(Graphics g,Rectangle bounds )
- {
- if (Enabled)
- {
-
- switch (State)
- {
- case Enumerations.ButtonState.Normal:
- case Enumerations.ButtonState.Pushed:
- {
-
- g.DrawImage(Image,bounds);
- break;
- }
- case Enumerations.ButtonState.Hot:
- {
-
- DrawImageDisabled(g,new Rectangle(bounds.X+1,bounds.Y+1,bounds.Width,bounds.Height),Image);
- g.DrawImage(Image,new Rectangle(bounds.X-1,bounds.Y-1,bounds.Width,bounds.Height));
- break;
- }
- }
-
- }
- else
- {
- DrawImageDisabled(g,bounds,Image);
- }
- }
- protected void DrawImageDisabled(Graphics g,Rectangle bounds, Image image)
- {
- Bitmap DisabledImage = new Bitmap((Bitmap) image);
- Color shadowColor = SystemColors.ControlDark;
- Color transparent1 = Color.FromArgb(0, 0, 0, 0);
-
- for(int pixelX = 0; pixelX < image.Width; pixelX++)
- {
- for(int pixelY = 0; pixelY < image.Height; pixelY++)
- {
- Color pixel=DisabledImage.GetPixel(pixelX, pixelY);
- if ( pixel!= transparent1 )
- DisabledImage.SetPixel(pixelX, pixelY, shadowColor);
- }
- }
-
- g.DrawImage(DisabledImage, bounds);
- }
-
- private Rectangle GetTextRect(Graphics g)
- {
- SizeF textSize=g.MeasureString(Text,Font,Width,strFormat);
- return PositionRect(new Rectangle(3,3,Width-6,Height-6),new Rectangle(0,0,(int)textSize.Width+3,(int)textSize.Height+3),TextAlign);
- }
- private Rectangle GetImageRectangle(Graphics g)
- {
- return PositionRect(new Rectangle(3,3,Width-6,Height-6),new Rectangle(0,0,Image.Width,Image.Height),ImageAlign);
- }
- private Rectangle PositionRect(Rectangle parentRect, Rectangle childRect, ContentAlignment position)
- {
- int cWidth= Math.Min(parentRect.Width,childRect.Width);
- int cHeight=Math.Min(parentRect.Height,childRect.Height);
- switch(position)
- {
- case ContentAlignment.TopLeft:
- {
- return new Rectangle(parentRect.X,parentRect.Y,cWidth,cHeight);
- }
- case ContentAlignment.MiddleLeft:
- {
- return new Rectangle(parentRect.X,parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
- }
- case ContentAlignment.BottomLeft:
- {
- return new Rectangle(parentRect.X,parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
- }
- case ContentAlignment.TopCenter:
- {
- return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y,cWidth,cHeight);
- }
- case ContentAlignment.MiddleCenter:
- {
- return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
- }
- case ContentAlignment.BottomCenter:
- {
- return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
- }
- case ContentAlignment.TopRight:
- {
- return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y,cWidth,cHeight);
- }
- case ContentAlignment.MiddleRight:
- {
- return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
- }
- case ContentAlignment.BottomRight:
- {
- return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
- }
- default:{return parentRect;}
- }
- }
- private int CenterSegment(int parentSegment, int childSegment)
- {
- return Math.Max(0,(parentSegment-childSegment)/2);
- }
-
- #endregion
- }
- }
|