Sinisterly
[C#] HCButton Class - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Visual Basic & .NET Framework (https://sinister.li/Forum-Visual-Basic-NET-Framework)
+--- Thread: [C#] HCButton Class (/Thread-C-HCButton-Class)



[C#] HCButton Class - ArkPhaze - 05-10-2013

Preview *Note: Gif is actually really f*cked up. The button looks nothing like that almost with the gradient. And much sharper of course. Smile
[Image: Yrf6Hkm.gif]

Source
Code:
// HCButton - (c)Arkphaze // Released: May 09/2013 // http://hackcommunity.net using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; sealed class HCButton : Button { private readonly SolidBrush _pressedBgBrush = new SolidBrush(Color.FromArgb(20, 20, 20)); private readonly Pen _penTop = new Pen(Color.Black); private readonly Pen _penBorder = new Pen(Color.Black); private readonly SolidBrush _textBrush = new SolidBrush(Color.White); private readonly Color _normalTopLine = Color.FromArgb(65, 65, 65); private readonly Color _normalSideEdge = Color.FromArgb(40, 40, 40); private readonly Color _normalTopGrd = Color.FromArgb(33, 33, 33); private readonly Color _normalBottomGrd = Color.FromArgb(28, 28, 28); private readonly Color _hoverTopLine = Color.FromArgb(75, 75, 75); private readonly Color _hoverSideEdge = Color.FromArgb(50, 50, 50); private readonly Color _hoverTopGrd = Color.FromArgb(54, 54, 54); private readonly Color _hoverBottomGrd = Color.FromArgb(50, 50, 50); private readonly Color _pressedSideEdge = Color.FromArgb(35, 35, 35); private MouseState _mouseState = MouseState.Normal; enum MouseState { Normal, Hover, Pressed } public HCButton() { DoubleBuffered = true; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.Clear(Color.Black); e.Graphics.SmoothingMode = SmoothingMode.HighQuality; switch (_mouseState) { case MouseState.Hover: using ( LinearGradientBrush lgb = new LinearGradientBrush(Point.Empty, new Point(0, Height), _hoverTopGrd, _hoverBottomGrd)) { e.Graphics.FillRectangle(lgb, DisplayRectangle); } _penTop.Color = _hoverTopLine; _penBorder.Color = _hoverSideEdge; _textBrush.Color = Color.White; break; case MouseState.Pressed: e.Graphics.FillRectangle(_pressedBgBrush, DisplayRectangle); _penTop.Color = _pressedSideEdge; _penBorder.Color = _pressedSideEdge; _textBrush.Color = Color.FromArgb(145, 145, 145); break; default: using ( LinearGradientBrush lgb = new LinearGradientBrush(Point.Empty, new Point(0, Height), _normalTopGrd, _normalBottomGrd)) { e.Graphics.FillRectangle(lgb, DisplayRectangle); } _penTop.Color = _normalTopLine; _penBorder.Color = _normalSideEdge; _textBrush.Color = Color.White; break; } // Draw top line e.Graphics.DrawLine(_penTop, 0f, 0f, Width, 0f); // Draw other border lines e.Graphics.DrawLine(_penBorder, 0f, 0f, 0f, Height - 1); // Left e.Graphics.DrawLine(_penBorder, 0f, Height - 1, Width - 1, Height - 1); // Bottom e.Graphics.DrawLine(_penBorder, Width - 1, 0f, Width - 1, Height - 1); // Right DrawText(e.Graphics); } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); _mouseState = MouseState.Pressed; } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); _mouseState = MouseState.Hover; Invalidate(); } protected override void OnMouseMove(MouseEventArgs mevent) { base.OnMouseMove(mevent); _mouseState = MouseState.Hover; } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); _mouseState = MouseState.Normal; } private void DrawText(Graphics graphics) { SizeF textSize = graphics.MeasureString(Text, Font); graphics.DrawString(Text, Font, _textBrush, Width / 2 - textSize.Width / 2, Height / 2 - textSize.Height / 2); } }

Simple, clean, and nice looking... Designed for darker backgrounds. :ok:


[C#] HCButton Class - ArkPhaze - 05-10-2013

Preview *Note: Gif is actually really f*cked up. The button looks nothing like that almost with the gradient. And much sharper of course. Smile
[Image: Yrf6Hkm.gif]

Source
Code:
// HCButton - (c)Arkphaze // Released: May 09/2013 // http://hackcommunity.net using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; sealed class HCButton : Button { private readonly SolidBrush _pressedBgBrush = new SolidBrush(Color.FromArgb(20, 20, 20)); private readonly Pen _penTop = new Pen(Color.Black); private readonly Pen _penBorder = new Pen(Color.Black); private readonly SolidBrush _textBrush = new SolidBrush(Color.White); private readonly Color _normalTopLine = Color.FromArgb(65, 65, 65); private readonly Color _normalSideEdge = Color.FromArgb(40, 40, 40); private readonly Color _normalTopGrd = Color.FromArgb(33, 33, 33); private readonly Color _normalBottomGrd = Color.FromArgb(28, 28, 28); private readonly Color _hoverTopLine = Color.FromArgb(75, 75, 75); private readonly Color _hoverSideEdge = Color.FromArgb(50, 50, 50); private readonly Color _hoverTopGrd = Color.FromArgb(54, 54, 54); private readonly Color _hoverBottomGrd = Color.FromArgb(50, 50, 50); private readonly Color _pressedSideEdge = Color.FromArgb(35, 35, 35); private MouseState _mouseState = MouseState.Normal; enum MouseState { Normal, Hover, Pressed } public HCButton() { DoubleBuffered = true; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); e.Graphics.Clear(Color.Black); e.Graphics.SmoothingMode = SmoothingMode.HighQuality; switch (_mouseState) { case MouseState.Hover: using ( LinearGradientBrush lgb = new LinearGradientBrush(Point.Empty, new Point(0, Height), _hoverTopGrd, _hoverBottomGrd)) { e.Graphics.FillRectangle(lgb, DisplayRectangle); } _penTop.Color = _hoverTopLine; _penBorder.Color = _hoverSideEdge; _textBrush.Color = Color.White; break; case MouseState.Pressed: e.Graphics.FillRectangle(_pressedBgBrush, DisplayRectangle); _penTop.Color = _pressedSideEdge; _penBorder.Color = _pressedSideEdge; _textBrush.Color = Color.FromArgb(145, 145, 145); break; default: using ( LinearGradientBrush lgb = new LinearGradientBrush(Point.Empty, new Point(0, Height), _normalTopGrd, _normalBottomGrd)) { e.Graphics.FillRectangle(lgb, DisplayRectangle); } _penTop.Color = _normalTopLine; _penBorder.Color = _normalSideEdge; _textBrush.Color = Color.White; break; } // Draw top line e.Graphics.DrawLine(_penTop, 0f, 0f, Width, 0f); // Draw other border lines e.Graphics.DrawLine(_penBorder, 0f, 0f, 0f, Height - 1); // Left e.Graphics.DrawLine(_penBorder, 0f, Height - 1, Width - 1, Height - 1); // Bottom e.Graphics.DrawLine(_penBorder, Width - 1, 0f, Width - 1, Height - 1); // Right DrawText(e.Graphics); } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); _mouseState = MouseState.Pressed; } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); _mouseState = MouseState.Hover; Invalidate(); } protected override void OnMouseMove(MouseEventArgs mevent) { base.OnMouseMove(mevent); _mouseState = MouseState.Hover; } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); _mouseState = MouseState.Normal; } private void DrawText(Graphics graphics) { SizeF textSize = graphics.MeasureString(Text, Font); graphics.DrawString(Text, Font, _textBrush, Width / 2 - textSize.Width / 2, Height / 2 - textSize.Height / 2); } }

Simple, clean, and nice looking... Designed for darker backgrounds. :ok:


RE: [C#] HCButton Class - Psycho_Coder - 05-10-2013

Nice post. Thanks for the GDI things.


RE: [C#] HCButton Class - Psycho_Coder - 05-10-2013

Nice post. Thanks for the GDI things.


RE: [C#] HCButton Class - bluedog.tar.gz - 05-10-2013

Looks really clean. The typography should be in allcaps in my opinion.


RE: [C#] HCButton Class - bluedog.tar.gz - 05-10-2013

Looks really clean. The typography should be in allcaps in my opinion.


RE: [C#] HCButton Class - ArkPhaze - 05-10-2013

(05-10-2013, 06:41 AM)bluedog.tar.gz Wrote: Looks really clean. The typography should be in allcaps in my opinion.

It's not all caps on HC :whistle:... That's what I was going by. I could have made it a feature implementation, and I could have done that in the preview, but it's just a preview. I kept the default text, and if the user wants that all they need is shift or caps lock on their keyboard, or they can override the Text property. Smile

I've done most the dirty work though, if others want that they can add it, but credits would still be due where needed of course.


RE: [C#] HCButton Class - ArkPhaze - 05-10-2013

(05-10-2013, 06:41 AM)bluedog.tar.gz Wrote: Looks really clean. The typography should be in allcaps in my opinion.

It's not all caps on HC :whistle:... That's what I was going by. I could have made it a feature implementation, and I could have done that in the preview, but it's just a preview. I kept the default text, and if the user wants that all they need is shift or caps lock on their keyboard, or they can override the Text property. Smile

I've done most the dirty work though, if others want that they can add it, but credits would still be due where needed of course.