想弄一个位图按钮,用PictureBox做,处理单击事件很慢。
所以想到从Button类继承,发现重载OnPaint无效。
下面这段代码PC上正常的,能看到效果。
- C# code
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); } } }
------解决方案--------------------
把 Rectangle rect = pe.ClipRectangle => Rectangle rect = this.ClientRectangle.试试
------解决方案--------------------
从Control继承是比较可行的办法,所有的背景、文字等都自绘。
需要处理MouseDown、MouseUp等事件。
我这么做过,没发现4楼说的慢的问题,项目到目前还没有人反映说按钮单击响应慢。
个人认为楼主可以放心的按这个思路做下去。
------解决方案--------------------
顶楼上!!
------解决方案--------------------
------解决方案--------------------
Button是没有OnPaint重载的 OnBackGroudPaint也没有 根本不支持重绘
现在流行的做法就是继承Control 如果你觉得太慢 那就想问 你用这个Button不就为了点击一下么
------解决方案--------------------
------解决方案--------------------
建议继承Control
------解决方案--------------------
关注,帮顶
------解决方案--------------------
试了一下出现个错误。
错误 1 部署和/或注册失败,错误为: 0x8973190e。 写入文件“%csidl_program_files%\smartdevice\system.drawing.dll”时出错。错误 0x80070070: 磁盘空间不足。
Device Connectivity Component
------解决方案--------------------
ImageButton继承自Control,CF3.5下不慢:
- C# code
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); } }