C#中怎样自绘按钮 请问大家在C#中怎样自绘按钮呢,鼠标在按钮上方按钮变为红色,鼠标按下变为蓝色,应该怎么实现?别的控件有DrawItem,但是按钮控件好像没有.谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 有事件啊buttonclick 等等的 自定义控件,重写onpaint,onmouseclick....等相关事件 [ToolboxBitmap(typeof(System.Windows.Forms.Button))] public partial class Button : System.Windows.Forms.Button { private State state = State.Normal; public Button() { Font = ControlCommon.Font; BackColor = Color.Transparent; try { //打开双缓冲 this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.StandardDoubleClick, false); this.SetStyle(ControlStyles.Selectable, false); this.ResizeRedraw = true; } catch { } } //[DefaultValue(Color.Transparent.ToArgb())] //public override Color BackColor //{ // get { return base.BackColor; } // set { base.BackColor = value; } //} //[DefaultValue(Color.FromArgb(33, 33, 33).ToArgb())] //public override Color ForeColor //{ // get { return base.ForeColor; } // set { base.ForeColor = value; } //} protected override void OnMouseEnter(EventArgs e) { state = State.MouseOver; this.Invalidate(); base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { state = State.Normal; this.Invalidate(); base.OnMouseLeave(e); } protected override void OnMouseDown(MouseEventArgs e) { if ((e.Button & MouseButtons.Left) != MouseButtons.Left) return; state = State.MouseDown; this.Invalidate(); base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { if ((e.Button & MouseButtons.Left) == MouseButtons.Left) state = State.Normal; this.Invalidate(); base.OnMouseUp(e); } protected override void OnPaint(PaintEventArgs e) { if (SkinImage.button.img == null) { base.OnPaint(e); return; } int i = (int)state; if (this.Focused && state != State.MouseDown) i = 5; if (!this.Enabled) i = 4; Rectangle rc = this.ClientRectangle; Graphics g = e.Graphics; //重绘控件背景 base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle)); SkinDraw.DrawRect2(g, SkinImage.button, rc, i); Image img = null; Size txts, imgs; txts = Size.Empty; imgs = Size.Empty; if (this.Image != null) { img = this.Image; } else if (this.ImageList != null && this.ImageIndex != -1) { img = this.ImageList.Images[this.ImageIndex]; } if (img != null) { imgs.Width = img.Width; imgs.Height = img.Height; } StringFormat format1; using (format1 = new StringFormat()) { format1.HotkeyPrefix = HotkeyPrefix.Show; SizeF ef1 = g.MeasureString(this.Text, this.Font, new SizeF((float)rc.Width, (float)rc.Height), format1); txts = Size.Ceiling(ef1); } rc.Inflate(-4, -4); if (imgs.Width * imgs.Height != 0) { Rectangle imgr = rc; imgr = SkinDraw.HAlignWithin(imgs, imgr, this.ImageAlign); imgr = SkinDraw.VAlignWithin(imgs, imgr, this.ImageAlign); if (!this.Enabled) { ControlPaint.DrawImageDisabled(g, img, imgr.Left, imgr.Top, this.BackColor); } else { g.DrawImage(img, imgr.Left, imgr.Top, img.Width, img.Height); } } //Rectangle txtr = rc; //txtr = SkinDraw.HAlignWithin(txts, txtr, this.TextAlign); //txtr = SkinDraw.VAlignWithin(txts, txtr, this.TextAlign); Brush brush2; format1 = new StringFormat(); format1.HotkeyPrefix = HotkeyPrefix.Show; if (this.RightToLeft == RightToLeft.Yes) { format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft; } brush2 = new SolidBrush(this.ForeColor); //if (this.Enabled) //{ // this.ForeColor = Color.FromArgb(33, 33, 33); //} //else //{ // this.ForeColor = Color.FromArgb(200, 200, 200); //} format1.LineAlignment = StringAlignment.Center; format1.Alignment = StringAlignment.Center; g.DrawString(this.Text, this.Font, brush2, ClientRectangle, format1); brush2.Dispose(); } } 解决问题马上给分 再发一贴,关于wcf的传输入自定义类时客户端契约怎么写 抓包后自动顶贴子是什么原理呢! c#的程序类库(dll)如何做成ACTIVX控件 日期处理问题 axWebBrowser控件的显示问题 如何获取DataGrid当前页中第一行的行号 急!急!急!求助!求助!求助! 各路高手请关注啦:在C#UserControl中,当按下Mouse时如何检测此时有没有按住Ctrl.Alt.Shift中的一个或多个键? 简单问题 WF,工作流到底是什么,在线等。。。。 如何将C#数组(如bytes[])传给具有IntPtr的托管代码函数?
buttonclick 等等的
public partial class Button : System.Windows.Forms.Button
{
private State state = State.Normal;
public Button()
{
Font = ControlCommon.Font;
BackColor = Color.Transparent;
try
{
//打开双缓冲
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.StandardDoubleClick, false);
this.SetStyle(ControlStyles.Selectable, false);
this.ResizeRedraw = true;
}
catch { }
} //[DefaultValue(Color.Transparent.ToArgb())]
//public override Color BackColor
//{
// get { return base.BackColor; }
// set { base.BackColor = value; }
//}
//[DefaultValue(Color.FromArgb(33, 33, 33).ToArgb())]
//public override Color ForeColor
//{
// get { return base.ForeColor; }
// set { base.ForeColor = value; }
//}
protected override void OnMouseEnter(EventArgs e)
{
state = State.MouseOver;
this.Invalidate();
base.OnMouseEnter(e);
} protected override void OnMouseLeave(EventArgs e)
{
state = State.Normal;
this.Invalidate();
base.OnMouseLeave(e);
} protected override void OnMouseDown(MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) != MouseButtons.Left) return; state = State.MouseDown;
this.Invalidate();
base.OnMouseDown(e);
} protected override void OnMouseUp(MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
state = State.Normal;
this.Invalidate();
base.OnMouseUp(e);
} protected override void OnPaint(PaintEventArgs e)
{
if (SkinImage.button.img == null)
{
base.OnPaint(e);
return;
} int i = (int)state;
if (this.Focused && state != State.MouseDown) i = 5;
if (!this.Enabled) i = 4;
Rectangle rc = this.ClientRectangle;
Graphics g = e.Graphics; //重绘控件背景
base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle)); SkinDraw.DrawRect2(g, SkinImage.button, rc, i); Image img = null;
Size txts, imgs; txts = Size.Empty;
imgs = Size.Empty; if (this.Image != null)
{
img = this.Image;
}
else if (this.ImageList != null && this.ImageIndex != -1)
{
img = this.ImageList.Images[this.ImageIndex];
} if (img != null)
{
imgs.Width = img.Width;
imgs.Height = img.Height;
} StringFormat format1;
using (format1 = new StringFormat())
{
format1.HotkeyPrefix = HotkeyPrefix.Show;
SizeF ef1 = g.MeasureString(this.Text, this.Font, new SizeF((float)rc.Width, (float)rc.Height), format1);
txts = Size.Ceiling(ef1);
} rc.Inflate(-4, -4);
if (imgs.Width * imgs.Height != 0)
{
Rectangle imgr = rc;
imgr = SkinDraw.HAlignWithin(imgs, imgr, this.ImageAlign);
imgr = SkinDraw.VAlignWithin(imgs, imgr, this.ImageAlign);
if (!this.Enabled)
{
ControlPaint.DrawImageDisabled(g, img, imgr.Left, imgr.Top, this.BackColor);
}
else
{
g.DrawImage(img, imgr.Left, imgr.Top, img.Width, img.Height);
}
} //Rectangle txtr = rc;
//txtr = SkinDraw.HAlignWithin(txts, txtr, this.TextAlign);
//txtr = SkinDraw.VAlignWithin(txts, txtr, this.TextAlign); Brush brush2;
format1 = new StringFormat();
format1.HotkeyPrefix = HotkeyPrefix.Show; if (this.RightToLeft == RightToLeft.Yes)
{
format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
brush2 = new SolidBrush(this.ForeColor);
//if (this.Enabled)
//{
// this.ForeColor = Color.FromArgb(33, 33, 33);
//}
//else
//{
// this.ForeColor = Color.FromArgb(200, 200, 200);
//}
format1.LineAlignment = StringAlignment.Center;
format1.Alignment = StringAlignment.Center;
g.DrawString(this.Text, this.Font, brush2, ClientRectangle, format1);
brush2.Dispose();
}
}