69 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			69 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			C#
		
	
	
	
using System.Drawing;
 | 
						|
using System.Drawing.Imaging;
 | 
						|
using System.Windows.Forms;
 | 
						|
 | 
						|
namespace PDADemo
 | 
						|
{
 | 
						|
    public partial class ImageButton : Control
 | 
						|
    {
 | 
						|
        public ImageButton()
 | 
						|
        {
 | 
						|
        }
 | 
						|
 | 
						|
        Image backgroundImage;
 | 
						|
        bool pressed = false;
 | 
						|
 | 
						|
        // Property for the background image to be drawn behind the button text.
 | 
						|
        public Image BackgroundImage
 | 
						|
        {
 | 
						|
            get
 | 
						|
            {
 | 
						|
                return this.backgroundImage;
 | 
						|
            }
 | 
						|
            set
 | 
						|
            {
 | 
						|
                this.backgroundImage = value;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        // When the mouse button is pressed, set the "pressed" flag to true 
 | 
						|
        // and invalidate the form to cause a repaint.  The .NET Compact Framework 
 | 
						|
        // sets the mouse capture automatically.
 | 
						|
        protected override void OnMouseDown(MouseEventArgs e)
 | 
						|
        {
 | 
						|
            this.pressed = true;
 | 
						|
            this.Invalidate();
 | 
						|
            base.OnMouseDown(e);
 | 
						|
        }
 | 
						|
 | 
						|
        // When the mouse is released, reset the "pressed" flag 
 | 
						|
        // and invalidate to redraw the button in the unpressed state.
 | 
						|
        protected override void OnMouseUp(MouseEventArgs e)
 | 
						|
        {
 | 
						|
            this.pressed = false;
 | 
						|
            this.Invalidate();
 | 
						|
            base.OnMouseUp(e);
 | 
						|
        }
 | 
						|
 | 
						|
        // Override the OnPaint method to draw the background image and the text.
 | 
						|
        protected override void OnPaint(PaintEventArgs e)
 | 
						|
        {
 | 
						|
            e.Graphics.FillRectangle(new SolidBrush(SystemColors.ActiveCaption), e.ClipRectangle);
 | 
						|
 | 
						|
            if (this.backgroundImage != null)
 | 
						|
            {
 | 
						|
                ImageAttributes attr = new ImageAttributes();
 | 
						|
                attr.SetColorKey(Color.Magenta, Color.Magenta);
 | 
						|
 | 
						|
                if (this.pressed)
 | 
						|
                    e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);
 | 
						|
                else
 | 
						|
                    e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);
 | 
						|
 | 
						|
            }
 | 
						|
 | 
						|
            base.OnPaint(e);
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |