| using System; using System.Drawing; using System.Windows.Forms; public class MyButton : Control { protected override void OnPaint(PaintEventArgs e ) { //Some drawing logic Graphics gr = e.Graphics; gr.DrawImage(image, 0, 0); Graphics gr = e.Graphics; gr.DrawImage(image, 0, 0); gr.DrawRectangle(pen, this.ClientRectangle); } protected override void OnPaintBackground(PaintEventArgs e ) { //Do nothing } } |
| protected override void OnPaint(PaintEventArgs e ) { Graphics gxOff; //Offscreen graphics if (m_bmpOffscreen == null) //Bitmap for doublebuffering { m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height); } gxOff = Graphics.FromImage(m_bmpOffscreen); gxOff.Clear(this.BackColor); //Draw some bitmap gxOff.DrawImage(bmpParent, 0, 0, bmpRect, GraphicsUnit.Pixel); //Boundary rectangle Rectangle rc = this.ClientRectangle; rc.Width--; rc.Height--; //Draw boundary gxOff.DrawRectangle(new Pen(Color.Black), rc); //Draw from the memory bitmap e.Graphics.DrawImage(m_bmpOffscreen, 0, 0); base.OnPaint(e); } |
| ImageAttributes imageAttr = new ImageAttributes(); // Make a transparent key imageAttr.SetColorKey(Color.White, Color.White); //Use the key when drawing gxOff.DrawImage(m_bmp, imgRect, 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, imageAttr); |
| using System; using System.Drawing; using System.Windows.Forms; public class ImageButton : Control { //Private members private Image image; //flag to indicate the pressed state private bool bPushed; private Bitmap m_bmpOffscreen; public ImageButton() { bPushed = false; //default minimal size this.Size = new Size(21, 21); } protected override void OnPaint(PaintEventArgs e ) { //Some drawing logic } protected override void OnPaintBackground(PaintEventArgs e ) { //Do nothing } } |
| public Image Image { get { return image; } set { image = value; } } |
| protected override void OnPaint(System.Windows.Forms.PaintEventArgs e ) { Graphics gxOff; //Offscreen graphics Rectangle imgRect; //image rectangle Brush backBrush; //brush for filling a backcolor if (m_bmpOffscreen == null) //Bitmap for doublebuffering { m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height); } gxOff = Graphics.FromImage(m_bmpOffscreen); gxOff.Clear(this.BackColor); if (!bPushed) backBrush = new SolidBrush(Parent.BackColor); else //change the background when it's pressed backBrush = new SolidBrush(Color.LightGray); gxOff.FillRectangle(backBrush, this.ClientRectangle); if (image != null) { //Center the image relativelly to the control int imageLeft = (this.Width - image.Width) / 2; int imageTop = (this.Height - image.Height) / 2; if (!bPushed) { imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height); } else //The button was pressed { //Shift the image by one pixel imgRect = new Rectangle(imageLeft + 1, imageTop + 1, image.Width, image.Height); } //Set transparent key ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image)); //Draw image gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr); } if (bPushed) //The button was pressed { //Prepare rectangle Rectangle rc = this.ClientRectangle; rc.Width--; rc.Height--; //Draw rectangle gxOff.DrawRectangle(new Pen(Color.Black), rc); } //Draw from the memory bitmap e.Graphics.DrawImage(m_bmpOffscreen, 0, 0); base.OnPaint(e); } |
| private Color BackgroundImageColor(Image image) { Bitmap bmp = new Bitmap(image); return bmp.GetPixel(0, 0); } |
| protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e ) { bPushed = true; this.Invalidate(); } protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e) { bPushed = false; this.Invalidate(); } |
| public Form1() { InitializeComponent(); imageButton1 = new ImageButton(); imageButton1.Image = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("ImageButtonProject.doorin2.gif")); imageButton1.Location = new Point(30, 50); imageButton1.Size = new Size(44, 34); //Hook up into click event imageButton1.Click+=new EventHandler(imageButton1_Click); this.Controls.Add(imageButton1); } |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12641349/viewspace-150405/ ,如需转载,请注明出处,否则将追究法律责任。