这是一个OFFICE XP 风格的按扭。
   using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows;
using System.Drawing.Imaging;namespace PerfectPets
{
/// <summary>
/// CoolButton 的摘要说明。
/// </summary>
public class CoolButton :UserControl
{
private Control control;
private bool mouse_honver = false;
private bool mouse_down = false; private Image image;
private int x1,x2,y1,y2;
private float cxImage,cyImage;
private RectangleF rectf;
private static Pen borderPen = new Pen(new SolidBrush(SystemColors.Highlight),2);
private Pen gPen ;
private static Brush fillBrush = new SolidBrush(CalculateColor(SystemColors.Highlight, SystemColors.Window, 70));
        private static Brush mouse_down_Brush = new SolidBrush(Color.AliceBlue);
private Brush defualtFillBrush = SystemBrushes.Control; public CoolButton(Control c,Image img)
{
SetStyle(ControlStyles.UserPaint,true);
this.Cursor = Cursors.Hand;
control = c;
locate(c.Location);

this.Size = c.Size;
x1 = c.Location.X - 1;
y1 = c.Location.Y - 1;
x2 = c.Size.Width;
y2 = c.Size.Height;

image = img;

gPen = new Pen(c.BackColor,2);
}
public void Set_Xp_Style()
{
borderPen = new Pen(new SolidBrush(SystemColors.Highlight),2);
fillBrush = new SolidBrush(CalculateColor(SystemColors.Highlight, SystemColors.Window, 70));
mouse_down_Brush = new SolidBrush(Color.AliceBlue); }
public void Set_2003_Style()
{
borderPen = new Pen(new SolidBrush(Color.FromArgb(7,3,90)),2);
fillBrush = new SolidBrush(Color.FromArgb(249,236,179));
mouse_down_Brush = new SolidBrush(Color.FromArgb(155, 154, 179)); }
public void Set_Gold_Style()
{

borderPen = new Pen(new SolidBrush(Color.FromArgb(79,99,119)),2);
fillBrush = new SolidBrush(Color.FromArgb(238,238,49));
mouse_down_Brush = new SolidBrush(Color.FromArgb(253, 11, 47));
}
public void Set_Furture_Style()
{
borderPen = new Pen(new SolidBrush(Color.FromArgb(0,0,0)),2);
fillBrush = new SolidBrush(Color.FromArgb(233,156,166));
mouse_down_Brush = new SolidBrush(Color.FromArgb(245, 207, 135));
}
public void Set_Summary_Style()
{
borderPen = new Pen(new SolidBrush(Color.FromArgb(0,0,0)),2);
fillBrush = new SolidBrush(Color.FromArgb(180,24,231));
mouse_down_Brush = new SolidBrush(Color.FromArgb(133,211,80)); }
private static Color CalculateColor(Color front, Color back, int alpha)
{


Color frontColor = Color.FromArgb(255, front);
Color backColor = Color.FromArgb(255, back);

float frontRed = frontColor.R;
float frontGreen = frontColor.G;
float frontBlue = frontColor.B;
float backRed = backColor.R;
float backGreen = backColor.G;
float backBlue = backColor.B;

float fRed = frontRed*alpha/255 + backRed*((float)(255-alpha)/255);
byte newRed = (byte)fRed;
float fGreen = frontGreen*alpha/255 + backGreen*((float)(255-alpha)/255);
byte newGreen = (byte)fGreen;
float fBlue = frontBlue*alpha/255 + backBlue*((float)(255-alpha)/255);
byte newBlue = (byte)fBlue; return  Color.FromArgb(255, newRed, newGreen, newBlue); }
public Brush DefualtFillBrush
{
set
{
defualtFillBrush = value;
}
}
private void locate(Point p)
{
this.Location = p;
x1 = p.X;
y1 = p.Y;
control.Location = new Point(0,0);
}
protected override void OnMouseEnter(EventArgs e)
{
mouse_honver = true;

         Refresh();
} protected override void OnMouseLeave(EventArgs e)
{
mouse_honver = false;

 Refresh();

}
protected override void OnMouseUp(MouseEventArgs e)
{
mouse_down = false;
 Refresh();
}
protected override void OnMouseDown(MouseEventArgs e)
{
mouse_down = true;

 Refresh();

}
private void DefaultPaint(Graphics g)
{
CacultRectangle(g);
g.FillRectangle(this.defualtFillBrush,ClientRectangle);
g.DrawRectangle(gPen,0,0,x2-2,y2-2);
g.DrawImage(image,(rectf.Width - cxImage)/2+2,(rectf.Height - cyImage)/2+2); }
private void CacultRectangle(Graphics g)
{
rectf = g.VisibleClipBounds;
cxImage = g.DpiX * image.Width / image.HorizontalResolution;
cyImage = g.DpiY * image.Height / image.VerticalResolution;
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics g = pea.Graphics; if(mouse_down)
{
g.FillRectangle(mouse_down_Brush,x1,y1,x2,y2);
g.DrawRectangle(borderPen,x1+1,y1+1,x2,y2);
g.DrawImage(image,(rectf.Width - cxImage)/2+1,(rectf.Height - cyImage)/2+1);
}
else if (mouse_honver)
{
g.FillRectangle(fillBrush,x1,y1,x2,y2);
g.DrawRectangle(borderPen,x1+1,y1+1,x2,y2);
ControlPaint.DrawImageDisabled(g,image,(int)(rectf.Width - cxImage)/2+3,(int)(rectf.Height - cyImage)/2+3,SystemColors.ControlDarkDark);
g.DrawImage(image,(rectf.Width - cxImage)/2,(rectf.Height - cyImage)/2);
}
else
{
DefaultPaint(g);
}
}
}
}

解决方案 »

  1.   

    下面这个就是和OFFICE XP 风格一样的菜单类
       public class MenuItemPlus:MenuItem
    {
    private Image image;
    private Font font = new Font("Times New Roman",8);
    private SplitShortcut split;
    private Color controlColor =  SystemColors.Control;
    private Color penColor = Color.DarkBlue;
    private Color fillColor = Color.LightSteelBlue;
    private Color normalFillColor =  Color.WhiteSmoke;
    private Color textColor = Color.Black; public MenuItemPlus()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    this.OwnerDraw = true;
    }
    public MenuItemPlus(string  name)
    {
    this.Text = name;
    this.image = null;
    this.OwnerDraw = true;

    }
    public MenuItemPlus(Image img)
    {
    image = img;
    this.OwnerDraw = true;
    } public Image _image
    {
    get
    {
    return image;
    }
    set
    {
    image = value;
    }
    }
    public Color ControlColor
    {
    get
    {
    return this.controlColor;
    }
    set
    {
    this.controlColor = value;
    }
    }
    public Color FillColor
    {
    get
    {
    return this.fillColor;
    }
    set
    {
    this.fillColor = value;
    }
    }
    public Color NormalFillColor
    {
    get
    {
    return this.normalFillColor;
    }
    set
    {
    this.normalFillColor = value;
    }
    }
    public Color PenColor
    {
    get
    {
    return this.penColor; }
    set
    {
    this.penColor = value; }
    }
    protected override void OnMeasureItem(MeasureItemEventArgs e)
    {
    //base.OnMeasureItem (e);
    e.ItemHeight = 26;
    e.ItemWidth = 160;
    } protected override void OnDrawItem(DrawItemEventArgs e)
    {
    //base.OnDrawItem (e);
    Graphics grfx = e.Graphics;
    split = new SplitShortcut();
    DrawBackground(split._splitShort(this.Text),image,e.State,e);
    } private void DrawBackground(string str,Image image,DrawItemState state,DrawItemEventArgs e)
    {
    Graphics g = e.Graphics;
    Rectangle bounds = new Rectangle(new Point(e.Bounds.X,e.Bounds.Y),new Size(e.Bounds.Width,e.Bounds.Height));
    StringFormat strfmt = new StringFormat();
    strfmt.Alignment = StringAlignment.Center;
    strfmt.LineAlignment = StringAlignment.Center; if (((e.State & DrawItemState.Selected) > 0)) 
    {

    bounds.Inflate(-1, -1);
    ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width, bounds.Height, Border3DStyle.Flat , Border3DSide.Top | Border3DSide.Left | Border3DSide.Right);
    g.FillRectangle(new SolidBrush(this.controlColor),bounds.X-1,bounds.Y-1,25,28);
    g.FillRectangle(new SolidBrush(this.fillColor), bounds);
    g.DrawRectangle(new Pen(new SolidBrush(this.penColor),1),bounds);
    bounds.Inflate(-30,-6);
    g.DrawString(str,font,new SolidBrush(this.textColor),bounds);
    bounds.Inflate(30,6);
    ControlPaint.DrawImageDisabled(g,image,e.Bounds.X + 7,e.Bounds.Y +6,SystemColors.ControlDarkDark);
    g.DrawImage(image,e.Bounds.X + 5,e.Bounds.Y + 4 );


    else 
    {

    bounds.Inflate(1,1);

    //g.DrawRectangle(new Pen(framecolor), bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1);
    ControlPaint.DrawBorder3D(g, bounds.Left, bounds.Top, bounds.Width, bounds.Height, Border3DStyle.Flat , Border3DSide.Top | Border3DSide.Left | Border3DSide.Right);
    g.FillRectangle(new SolidBrush(this.normalFillColor), bounds);
    bounds.Inflate(-32,-8);
    g.DrawString(str,font,new SolidBrush(this.textColor),bounds);
    bounds.Inflate(32,7);
    g.FillRectangle(new SolidBrush(this.controlColor),bounds.X-1,bounds.Y-1,27,28);
    g.DrawImage(image,e.Bounds.X + 7,e.Bounds.Y + 6);
    }
    }

    }
      

  2.   

    这是一个OFFICE XP 风格的按扭。
       using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    using System.Windows;
    using System.Drawing.Imaging;namespace PerfectPets
    {
    /// <summary>
    /// CoolButton 的摘要说明。
    /// </summary>
    public class CoolButton :UserControl
    {
    private Control control;
    private bool mouse_honver = false;
    private bool mouse_down = false; private Image image;
    private int x1,x2,y1,y2;
    private float cxImage,cyImage;
    private RectangleF rectf;
    private static Pen borderPen = new Pen(new SolidBrush(SystemColors.Highlight),2);
    private Pen gPen ;
    private static Brush fillBrush = new SolidBrush(CalculateColor(SystemColors.Highlight, SystemColors.Window, 70));
            private static Brush mouse_down_Brush = new SolidBrush(Color.AliceBlue);
    private Brush defualtFillBrush = SystemBrushes.Control; public CoolButton(Control c,Image img)
    {
    SetStyle(ControlStyles.UserPaint,true);
    this.Cursor = Cursors.Hand;
    control = c;
    locate(c.Location);

    this.Size = c.Size;
    x1 = c.Location.X - 1;
    y1 = c.Location.Y - 1;
    x2 = c.Size.Width;
    y2 = c.Size.Height;

    image = img;

    gPen = new Pen(c.BackColor,2);
    }
    public void Set_Xp_Style()
    {
    borderPen = new Pen(new SolidBrush(SystemColors.Highlight),2);
    fillBrush = new SolidBrush(CalculateColor(SystemColors.Highlight, SystemColors.Window, 70));
    mouse_down_Brush = new SolidBrush(Color.AliceBlue); }
    public void Set_2003_Style()
    {
    borderPen = new Pen(new SolidBrush(Color.FromArgb(7,3,90)),2);
    fillBrush = new SolidBrush(Color.FromArgb(249,236,179));
    mouse_down_Brush = new SolidBrush(Color.FromArgb(155, 154, 179)); }
    public void Set_Gold_Style()
    {

    borderPen = new Pen(new SolidBrush(Color.FromArgb(79,99,119)),2);
    fillBrush = new SolidBrush(Color.FromArgb(238,238,49));
    mouse_down_Brush = new SolidBrush(Color.FromArgb(253, 11, 47));
    }
    public void Set_Furture_Style()
    {
    borderPen = new Pen(new SolidBrush(Color.FromArgb(0,0,0)),2);
    fillBrush = new SolidBrush(Color.FromArgb(233,156,166));
    mouse_down_Brush = new SolidBrush(Color.FromArgb(245, 207, 135));
    }
    public void Set_Summary_Style()
    {
    borderPen = new Pen(new SolidBrush(Color.FromArgb(0,0,0)),2);
    fillBrush = new SolidBrush(Color.FromArgb(180,24,231));
    mouse_down_Brush = new SolidBrush(Color.FromArgb(133,211,80)); }
    private static Color CalculateColor(Color front, Color back, int alpha)
    {


    Color frontColor = Color.FromArgb(255, front);
    Color backColor = Color.FromArgb(255, back);

    float frontRed = frontColor.R;
    float frontGreen = frontColor.G;
    float frontBlue = frontColor.B;
    float backRed = backColor.R;
    float backGreen = backColor.G;
    float backBlue = backColor.B;

    float fRed = frontRed*alpha/255 + backRed*((float)(255-alpha)/255);
    byte newRed = (byte)fRed;
    float fGreen = frontGreen*alpha/255 + backGreen*((float)(255-alpha)/255);
    byte newGreen = (byte)fGreen;
    float fBlue = frontBlue*alpha/255 + backBlue*((float)(255-alpha)/255);
    byte newBlue = (byte)fBlue; return  Color.FromArgb(255, newRed, newGreen, newBlue); }
    public Brush DefualtFillBrush
    {
    set
    {
    defualtFillBrush = value;
    }
    }
    private void locate(Point p)
    {
    this.Location = p;
    x1 = p.X;
    y1 = p.Y;
    control.Location = new Point(0,0);
    }
    protected override void OnMouseEnter(EventArgs e)
    {
    mouse_honver = true;

             Refresh();
    } protected override void OnMouseLeave(EventArgs e)
    {
    mouse_honver = false;

     Refresh();

    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
    mouse_down = false;
     Refresh();
    }
    protected override void OnMouseDown(MouseEventArgs e)
    {
    mouse_down = true;

     Refresh();

    }
    private void DefaultPaint(Graphics g)
    {
    CacultRectangle(g);
    g.FillRectangle(this.defualtFillBrush,ClientRectangle);
    g.DrawRectangle(gPen,0,0,x2-2,y2-2);
    g.DrawImage(image,(rectf.Width - cxImage)/2+2,(rectf.Height - cyImage)/2+2); }
    private void CacultRectangle(Graphics g)
    {
    rectf = g.VisibleClipBounds;
    cxImage = g.DpiX * image.Width / image.HorizontalResolution;
    cyImage = g.DpiY * image.Height / image.VerticalResolution;
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
    Graphics g = pea.Graphics; if(mouse_down)
    {
    g.FillRectangle(mouse_down_Brush,x1,y1,x2,y2);
    g.DrawRectangle(borderPen,x1+1,y1+1,x2,y2);
    g.DrawImage(image,(rectf.Width - cxImage)/2+1,(rectf.Height - cyImage)/2+1);
    }
    else if (mouse_honver)
    {
    g.FillRectangle(fillBrush,x1,y1,x2,y2);
    g.DrawRectangle(borderPen,x1+1,y1+1,x2,y2);
    ControlPaint.DrawImageDisabled(g,image,(int)(rectf.Width - cxImage)/2+3,(int)(rectf.Height - cyImage)/2+3,SystemColors.ControlDarkDark);
    g.DrawImage(image,(rectf.Width - cxImage)/2,(rectf.Height - cyImage)/2);
    }
    else
    {
    DefaultPaint(g);
    }
    }
    }
    }