wince+C#下,重载OnPaint方法
想弄一个位图按钮,用PictureBox做,处理单击事件很慢。
所以想到从Button类继承,发现重载OnPaint无效。
下面这段代码PC上正常的,能看到效果。
class ImageButton:Button { private Image image; public Image Image { get { return image; } set { image = value; } } protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; Rectangle rect = pe.ClipRectangle; if (image != null) { g.DrawImage(image, 0, 0, rect, GraphicsUnit.Pixel); } else { base.OnPaint(pe); } } }
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); } }
[解决办法]
是呀,我做的时候,也是用MouseDown和MouseUp来实现Click事件的。
[解决办法]
散个分哈
[解决办法]
namespace MyPocketPc{ public partial class MyImageButton:Control { public MyImageButton() { } private Image _image; public Image Image { get { return _image; } set { _image = value; } } protected override void OnPaint(PaintEventArgs e) { if (this._image != null) { e.Graphics.DrawImage(this._image, 0, 0, e.ClipRectangle, GraphicsUnit.Pixel); } base.OnPaint(e); } }}
[解决办法]
哇 学过一点点 也可以说是一窍不通哈
[解决办法]
Mark!
[解决办法]