C# WinForm自定义控件没有焦点
public class ImgButton : Control, IHImage {
#region vars
private bool _isPressed;
public bool IsPressed
{
get { return _isPressed; }
set { _isPressed = value; }
}
private StringFormat _stringFormat;
private Rectangle _buttonTextRect = Rectangle.Empty;
private Brush _foreBrush;
private Bitmap _imgOffscreen;
#endregion
#region ctor
public TmButton()
: base()
{
_stringFormat = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
_foreBrush = new SolidBrush(this.ForeColor);
}
#endregion
#region props
public Bitmap Image { get; set; }
public Bitmap PressedImage {get;set;}
public StringFormat StringFormat {
get { return _stringFormat; }
}
public override Color ForeColor {
get {
return base.ForeColor;
}
set {
base.ForeColor = value;
_foreBrush = new SolidBrush(base.ForeColor);
}
}
#endregion
#region onverrides
protected override void Dispose(bool disposing) {
if (Image != null) {
Image.Dispose();
Image = null;
}
if (PressedImage != null) {
PressedImage.Dispose();
PressedImage = null;
}
if (_imgOffscreen != null) {
_imgOffscreen.Dispose();
_imgOffscreen = null;
}
base.Dispose(disposing);
}
protected override void OnPaintBackground(PaintEventArgs e) {
//prevent
}
protected override void OnPaint(PaintEventArgs e) {
Graphics gxOff;
Rectangle imgRect;
var image = (_isPressed && PressedImage != null) ? PressedImage : Image;
if (_imgOffscreen == null) {
_imgOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height); //InitPic Size
}
gxOff = Graphics.FromImage(_imgOffscreen);
if (this.Parent is IHImage) {
var par = this.Parent as IHImage;
gxOff.DrawImage(par.Image.Clip(this.Bounds), 0, 0);
}
if (image != null) {
var imageLeft = (this.Width - image.Width) / 2;
var imageTop = (this.Height - image.Height) / 2;
imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height);
var imageAttr = new ImageAttributes();
gxOff.DrawImage(image,imgRect, 0, 0,image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
}
if (_isPressed) {
var rc = this.ClientRectangle;
rc.Width--;
rc.Height--;
}
e.Graphics.DrawImage(_imgOffscreen, 0, 0);
if (this.IsPressed && this.PressedImage != null)
{
if (this.Text != null && this.Text != string.Empty)
{
using (Font font6 = new Font("仿宋", 6, FontStyle.Regular))
{
using (Brush br = new SolidBrush(Color.Yellow))
{
Rectangle r = new Rectangle();
r.Height = (int)e.Graphics.MeasureString(this.Text, font6).Height;
r.Width = (int)e.Graphics.MeasureString(this.Text, font6).Width;
r.X = (PressedImage.Width - r.Width) / 2;
r.Y = PressedImage.Height - PressedImage.Height / 5;
e.Graphics.DrawString(this.Text, font6, br, r);
}
}
}
}
base.OnPaint(e);
}
protected override void OnKeyDown(KeyEventArgs e) {
// _isPressed = this.Focused; this.Invalidate();
// base.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e) {
// _isPressed = false; this.Invalidate();
// base.OnKeyUp(e);
}
protected override void OnMouseDown(MouseEventArgs e) {
_isPressed = true; this.Invalidate();
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e) {
//_isPressed = false; this.Invalidate();
//base.OnMouseUp(e);
}
protected override void OnGotFocus(EventArgs e)
{
this.IsPressed = true;
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
this.IsPressed = false;
base.OnLostFocus(e);
}
#endregion
}
这是控件代码,作用是在点击下去的时候能够根据点击事件的变化在两张图片之间切换,现在遇上的问题是控件在获取焦点之后,图片能切换正常,但点击另外一个相同的按钮时,没办法切换焦点,失去焦点的控件图片无法切换回之前的。
求解。