Kosal Ang
Sat Oct 12 2024
Photo by: Florian van Duyn
In many applications, custom buttons enhance the user interface by providing a unique look and feel. One way to make your buttons stand out is by adding a shadow effect to the text, giving it depth and making it more visually appealing. In this article, we’ll create a custom ShadowTextButton
in C# using the Btton
class, which will allow us to easily apply shadow effects to the button’s text.
ShadowTextButton
ClassThe ShadowTextButton
class extends the functionality of the standard Windows Forms Button
control by adding properties to control the shadow’s appearance. These properties include:
Here’s the complete implementation of the ShadowTextButton
:
1public class ShadowTextButton : Button 2{ 3 // Properties for shadow settings 4 public int ShadowOffsetX { get; set; } = 2; // Horizontal offset of the shadow 5 public int ShadowOffsetY { get; set; } = 2; // Vertical offset of the shadow 6 public Color ShadowColor { get; set; } = Color.FromArgb(128, 0, 0, 0); // Semi-transparent shadow color 7 public int ShadowStrokeWidth { get; set; } = 5; // Stroke width for larger shadow 8 9 protected override void OnPaint(PaintEventArgs pevent) 10 { 11 base.OnPaint(pevent); 12 13 // Graphics settings for smooth drawing 14 Graphics g = pevent.Graphics; 15 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 16 g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; 17 18 // Set up fonts and brushes for drawing 19 Font font = this.Font; 20 Brush textBrush = new SolidBrush(this.ForeColor); 21 Brush shadowBrush = new SolidBrush(ShadowColor); 22 23 // Get the text alignment and size 24 StringFormat stringFormat = new StringFormat 25 { 26 Alignment = StringAlignment.Center, 27 LineAlignment = StringAlignment.Center 28 }; 29 30 // Calculate the text position and rectangle 31 Rectangle rect = this.ClientRectangle; 32 33 // Draw a bigger shadow stroke by rendering the shadow multiple times in different directions 34 for (int i = -ShadowStrokeWidth; i <= ShadowStrokeWidth; i++) 35 { 36 for (int j = -ShadowStrokeWidth; j <= ShadowStrokeWidth; j++) 37 { 38 // Skip drawing in the middle (the actual text position) 39 if (i == 0 && j == 0) continue; 40 41 // Draw the shadow text with various offsets to create the larger shadow effect 42 g.DrawString(this.Text, font, shadowBrush, 43 new RectangleF(rect.X + ShadowOffsetX + i, rect.Y + ShadowOffsetY + j, rect.Width, rect.Height), stringFormat); 44 } 45 } 46 47 // Draw the actual text on top 48 g.DrawString(this.Text, font, textBrush, rect, stringFormat); 49 } 50} 51 52
The properties in ShadowTextButton
allow developers to easily adjust the shadow’s appearance:
OnPaint
MethodThe core of the shadow effect lies in the overridden OnPaint
method. Here’s how the drawing process works:
ShadowStrokeWidth
determines the range of these offsets.To use the ShadowTextButton
in your form, create an instance of the class and customize its appearance. For example:
1public Form1() 2{ 3 InitializeComponent(); 4 5 ShadowTextButton shadowTextButton = new ShadowTextButton 6 { 7 Text = "Shadow Stroke", 8 Size = new Size(200, 50), 9 Location = new Point(50, 50), 10 ForeColor = Color.White, 11 BackColor = Color.DarkBlue, 12 ShadowColor = Color.FromArgb(150, 0, 0, 0), // Semi-transparent black shadow 13 ShadowOffsetX = 3, 14 ShadowOffsetY = 3, 15 ShadowStrokeWidth = 5 // Set a bigger stroke width for the shadow 16 }; 17 18 this.Controls.Add(shadowTextButton); 19} 20 21
In this example:
The ShadowTextButton
is a simple yet effective way to add depth and dimension to your user interface. By allowing customization of shadow properties, this control can be easily adapted to suit the design of your application. Whether you want a subtle shadow for a sleek look or a bold shadow for emphasis, this control gives you the flexibility to enhance your button text.
This approach adds both functionality and visual appeal to the buttons in your Windows Forms applications, making them more interactive and polished.