在进行项目开发时,经常需要为应用程序设计一个具有欺骗性图形用户界面。为了实现这一目标,需要一些工具,其中最重要的工具之一是具有图形背景的图形表单,其次是像按钮和标签这样的透明组件。
为了编写具有欺骗性图形用户界面的程序,需要一些工具。其中一个重要的工具是具有图形背景的图形表单。第二个是像按钮和标签这样的透明组件。
可以使用以下代码来定义一个具有图形背景的图形表单。
public interface IControlBackground {
Bitmap BackgroundImage { get; }
}
public class GForm : Form, IControlBackground {
Bitmap background;
protected override void OnPaint(PaintEventArgs e) {
if (background != null)
e.Graphics.DrawImage(background, 0, 0);
else
base.OnPaint(e);
}
public Bitmap BackgroundImage {
get { return background; }
set { background = value; }
}
}
IControlBackground 用作表单和其他组件之间的接口,可以在这些组件的 OnPaintBackground 事件中使用它。
现在可以定义其他组件,如按钮和标签。
以下是按钮组件的代码。
public class ImageButton : System.Windows.Forms.Control {
// Constants
// Member Variables
protected ButtonState state;
protected Image image;
protected Image disabledImage;
protected Image overImage;
protected Image pressedImage;
protected DialogResult dialogResult;
protected bool isDefault;
protected Point textOffset = new Point(2, 2);
public Control BackControl;
// Designer Variables
private System.ComponentModel.Container components = null;
// Constructor
public ImageButton() {
InitializeComponent();
}
// Component Designer generated code
private void InitializeComponent() {
components = new System.ComponentModel.Container();
}
// Methods
private bool InClient(int x, int y) {
return ((x >= 0) && (x < ClientRectangle.Width) && (y >= 0) && (y < ClientRectangle.Height));
}
// Properties
public Image Image {
get { return image; }
set {
if (image == value) return;
image = value;
if (state == ButtonState.Normal) Invalidate();
}
}
public Image DisabledImage {
get { return disabledImage; }
set {
if (disabledImage == value) return;
disabledImage = value;
if (state == ButtonState.Disabled) Invalidate();
}
}
public Image OverImage {
get { return overImage; }
set {
if (overImage == value) return;
overImage = value;
if (state == ButtonState.Over) Invalidate();
}
}
public Image PressedImage {
get { return pressedImage; }
set {
if (pressedImage == value) return;
pressedImage = value;
if (state == ButtonState.Pressed) Invalidate();
}
}
public override string Text {
get { return base.Text; }
set { base.Text = value; }
}
public Point TextOffset {
get { return textOffset; }
set { textOffset = value; }
}
public ButtonState ButtonState {
get { return state; }
}
// Overrides
protected override void OnMouseEnter(EventArgs e) {
state = ButtonState.Over;
Invalidate();
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e) {
state = ButtonState.Normal;
Invalidate();
base.OnMouseLeave(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
Capture = true;
state = ButtonState.Pressed;
Invalidate();
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e) {
Capture = false;
if (state != ButtonState.Disabled) {
state = (InClient(MousePosition.X, MousePosition.Y) ? ButtonState.Over : ButtonState.Normal);
Invalidate();
}
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e) {
Capture = false;
if (state != ButtonState.Disabled) {
state = (InClient(MousePosition.X, MousePosition.Y) ? ButtonState.Over : ButtonState.Normal);
Invalidate();
}
base.OnMouseMove(e);
}
protected override void OnEnabledChanged(EventArgs e) {
state = (Enabled ? ButtonState.Normal : ButtonState.Disabled);
Invalidate();
base.OnEnabledChanged(e);
}
protected override void OnTextChanged(EventArgs e) {
Invalidate();
base.OnTextChanged(e);
}
protected override void OnPaintBackground(PaintEventArgs e) {
IControlBackground form = this.Parent as IControlBackground;
if (form == null) {
base.OnPaintBackground(e);
return;
}
if (form.BackgroundImage != null)
e.Graphics.DrawImage(form.BackgroundImage, 0, 0, Bounds, GraphicsUnit.Pixel);
}
protected override void OnPaint(PaintEventArgs e) {
Brush TBrush = new SolidBrush(ForeColor);
if ((base.Text != null) && (base.Text.Length > 0)) {
SizeF tSize = e.Graphics.MeasureString(base.Text, Font);
Point DrawPoint = new Point((int)((ClientRectangle.Width - tSize.Width) / 2), (int)((ClientRectangle.Height - tSize.Height) / 2));
if (state == ButtonState.Pressed) {
DrawPoint.X += textOffset.X;
DrawPoint.Y += textOffset.Y;
}
e.Graphics.DrawString(base.Text, Font, TBrush, DrawPoint.X, DrawPoint.Y);
TBrush.Dispose();
}
}
internal Color GetTransparentColor(Bitmap bm) {
return bm.GetPixel(0, 0);
}
public void doPaint(Graphics gx, ButtonState bs) {
ImageAttributes attrib = new ImageAttributes();
Image im;
im = this.Image;
if (bs == ButtonState.Pressed)
im = this.PressedImage;
else if (bs == ButtonState.Over)
im = this.OverImage;
if (im != null) {
Bitmap bm = new Bitmap(im);
Color color = GetTransparentColor(bm);
attrib.SetColorKey(color, color);
gx.DrawImage(bm, this.ClientRectangle, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, GraphicsUnit.Pixel, attrib);
}
else
gx.Clear(BackColor);
}
// IButtonControl Interface Implementation
public DialogResult DialogResult {
get { return dialogResult; }
set {
if (Enum.IsDefined(typeof(DialogResult), value)) {
dialogResult = value;
}
}
}
public void NotifyDefault(bool value) {
if (isDefault != value) {
isDefault = value;
}
}
public void PerformClick() {
if (Enabled) {
OnClick(EventArgs.Empty);
}
}
}
请注意,按钮背景图像中的像素(0,0)是透明颜色!为了更好地处理图像,如果使用PhotoShop,请选择与表单背景图像相同的背景透明颜色。