Posted by Kosal
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
:
public class ShadowTextButton : Button
{
// Properties for shadow settings
public int ShadowOffsetX { get; set; } = 2; // Horizontal offset of the shadow
public int ShadowOffsetY { get; set; } = 2; // Vertical offset of the shadow
public Color ShadowColor { get; set; } = Color.FromArgb(128, 0, 0, 0); // Semi-transparent shadow color
public int ShadowStrokeWidth { get; set; } = 5; // Stroke width for larger shadow
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
// Graphics settings for smooth drawing
Graphics g = pevent.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// Set up fonts and brushes for drawing
Font font = this.Font;
Brush textBrush = new SolidBrush(this.ForeColor);
Brush shadowBrush = new SolidBrush(ShadowColor);
// Get the text alignment and size
StringFormat stringFormat = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
// Calculate the text position and rectangle
Rectangle rect = this.ClientRectangle;
// Draw a bigger shadow stroke by rendering the shadow multiple times in different directions
for (int i = -ShadowStrokeWidth; i <= ShadowStrokeWidth; i++)
{
for (int j = -ShadowStrokeWidth; j <= ShadowStrokeWidth; j++)
{
// Skip drawing in the middle (the actual text position)
if (i == 0 && j == 0) continue;
// Draw the shadow text with various offsets to create the larger shadow effect
g.DrawString(this.Text, font, shadowBrush,
new RectangleF(rect.X + ShadowOffsetX + i, rect.Y + ShadowOffsetY + j, rect.Width, rect.Height), stringFormat);
}
}
// Draw the actual text on top
g.DrawString(this.Text, font, textBrush, rect, stringFormat);
}
}
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:
public Form1()
{
InitializeComponent();
ShadowTextButton shadowTextButton = new ShadowTextButton
{
Text = "Shadow Stroke",
Size = new Size(200, 50),
Location = new Point(50, 50),
ForeColor = Color.White,
BackColor = Color.DarkBlue,
ShadowColor = Color.FromArgb(150, 0, 0, 0), // Semi-transparent black shadow
ShadowOffsetX = 3,
ShadowOffsetY = 3,
ShadowStrokeWidth = 5 // Set a bigger stroke width for the shadow
};
this.Controls.Add(shadowTextButton);
}
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.