RickZeeland
Вот некоторый код, который использует Метод setstyle, это для пользовательской кнопки, но, возможно, ее можно адаптировать для PictureBox:
namespace PushButtonTransparent
{
using System.Drawing;
using System.Windows.Forms;
using System;
/// <summary>
/// Transparent Button with a normal and pressed state, preferably used with transparent .PNG images,
/// it can be used without images, in which case only a black rectangle is drawn.
/// The click event must be handled by the Form, and property <see cref="pressed"/> must be set.
/// This is an extended version of <see cref="PushButton"/> which supports:
/// - Transparancy
/// - Text
/// - Textcolor (ForeColor / <see cref="ForeColorPressed"/>)
/// - Font
/// <example>
/// <code>
/// PushButtonTransparent TransparentButton;
/// this.TransparentButton.Text = "Test button";
/// // Events, images and colors can be set in the designer, or in code:
/// this.TransparentButton.Click += this.ButtonClicked;
/// this.TransparentButton.MouseUp += this.PushButtonMouseUp;
/// // Set images:
/// this.TransparentButton.ImageNotPressed = Loader.LoadBitmap(ResourceId.show_manual);
/// this.TransparentButton.ImagePressed = Loader.LoadBitmap(ResourceId.show_manual_over);
/// this.TransparentButton.Pressed = true;
/// </code>
/// </example>
/// </summary>
public class PushButtonTransparent : Control // , IButtonControl
{
private bool pressed;
/// <summary>
/// Initializes a new instance of the <see cref="PushButtonTransparent"/> class.
/// </summary>
public PushButtonTransparent()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.Opaque, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
////this.BackColor = Color.Transparent;
}
/// <summary>
/// Gets or sets imagePressed image.
/// </summary>
public Image ImagePressed { get; set; }
/// <summary>
/// Gets or sets ImageNotPressed image.
/// </summary>
public Image ImageNotPressed { get; set; }
/// <summary>
/// Gets or sets TextPosition, otherwise default position is calculated.
/// </summary>
public Point TextPosition { get; set; }
/// <summary>
/// Gets or sets TextAlignment: Near, Center, Far.
/// Only if TextPosition is not set.
/// </summary>
public StringAlignment TextAlignment { get; set; }
/// <summary>
/// Gets or sets ForeColorPressed (text color when clicked).
/// </summary>
public Color ForeColorPressed { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the button is pressed and forces a redraw.
/// </summary>
public bool Pressed
{
get
{
return this.pressed;
}
set
{
this.pressed = value;
this.Invalidate();
}
}
/// <summary>
/// Gets CreateParams and sets WsExTransparent.
/// </summary>
protected override CreateParams CreateParams
{
get
{
const int WsExTransparent = 0x20;
var cp = base.CreateParams;
cp.ExStyle |= WsExTransparent;
return cp;
}
}
/// <summary>
/// Draw ImageNotPressed / ImagePressed if property set.
/// Draw a black rectangle if image is missing.
/// Draw Text if property is set.
/// </summary>
/// <param name="pevent">
/// The PaintEventArgs.
/// </param>
protected override void OnPaint(PaintEventArgs pevent)
{
Image buttonImage;
var g = pevent.Graphics;
int width;
int height;
SolidBrush textBrush;
// this.Font = SkinSettings.FontDefault;
// float scaleFactor = this.Font.Size / 8;
if (this.pressed)
{
buttonImage = this.ImagePressed;
}
else
{
buttonImage = this.ImageNotPressed;
}
if (buttonImage == null)
{
// Draw a black rectangle if image is missing.
width = Convert.ToInt32(this.ClientRectangle.Width * scaleFactor);
height = Convert.ToInt32(this.ClientRectangle.Height * scaleFactor);
g.DrawRectangle(Pens.Black, 0, 0, width - 1, height - 1);
}
else
{
// Sets the images' size and position
width = Convert.ToInt32(buttonImage.Size.Width * scaleFactor);
height = Convert.ToInt32(buttonImage.Size.Height * scaleFactor);
var rect = new Rectangle(0, 0, width, height);
//// Draw the image
g.DrawImage(buttonImage, rect);
//// Draw with Gamma correction.
//var rect = new Rectangle(0, 0, width, height);
//var imageAttr = new System.Drawing.Imaging.ImageAttributes();
//imageAttr.SetGamma(2.0F);
//g.DrawImage(buttonImage, rect, 0, 0, width, height, GraphicsUnit.Pixel, imageAttr);
}
// No text ? then return
if (string.IsNullOrEmpty(this.Text))
{
return;
}
// Set the text font and style and draw it.
if (this.pressed && !this.ForeColorPressed.IsEmpty)
{
// Pressed color.
textBrush = new SolidBrush(this.ForeColorPressed);
}
else
{
// Normal color.
textBrush = new SolidBrush(this.ForeColor);
}
// Draw the text on TextPosition if specified.
if (!this.TextPosition.IsEmpty)
{
float x = this.TextPosition.X * scaleFactor;
float y = this.TextPosition.Y * scaleFactor;
g.DrawString(this.Text, this.Font, textBrush, x, y);
}
else
{
// Otherwise use TextAlignment.
width = Convert.ToInt32(this.Width * scaleFactor);
height = Convert.ToInt32(this.Height * scaleFactor);
var rectF = new RectangleF(0F, (height / 2) - (this.Font.Height / 2), width, height);
var strFormat = new StringFormat();
strFormat.Alignment = this.TextAlignment;
g.DrawString(this.Text, this.Font, textBrush, rectF, strFormat);
}
}
/// <summary>
/// OnPaintBackground must not be called.
/// </summary>
/// <param name="pevent">
/// The PaintEventArgs.
/// </param>
protected override void OnPaintBackground(PaintEventArgs pevent)
{
//// don't call the base class
////base.OnPaintBackground(pevent);
}
}
}