我仅仅只是想给菜单加一个边框,使它看起来想.net的菜单那样,给panel加个边框,这样就能把它做成一个button了

解决方案 »

  1.   

    先说菜单
    this.menuItem1.OwnerDraw = true;
    加上两个事件
    private void menuItem1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    {
    MenuItem mi = (MenuItem) sender;
    // Create the hover rectangle
    Rectangle rect = new Rectangle(e.Bounds.X, 
    e.Bounds.Y + 1, 
    e.Bounds.Width, 
    e.Bounds.Height - 1); // Create the hover brush
    Brush b = new LinearGradientBrush(rect, 
    Color.White, 
    Color.CadetBlue,
    90f, false); // Fill the rectangle
    e.Graphics.FillRectangle(b, rect);
    Rectangle rect1 = new Rectangle(e.Bounds.X, 
    e.Bounds.Y, 
    e.Bounds.Width + 1, 
    e.Bounds.Height );
    e.Graphics.DrawRectangle(new Pen(Color.Blue), rect1);

    // Create stringFormat object
    StringFormat sf = new StringFormat(); // set the Alignment to center
    sf.LineAlignment = StringAlignment.Center;
    sf.Alignment = StringAlignment.Center; // Draw the text
    e.Graphics.DrawString(mi.Text, 
    System.Windows.Forms.SystemInformation.MenuFont, 
    new SolidBrush(Color.FromKnownColor(KnownColor.MenuText)), 
    e.Bounds, 
    sf);
    } private void menuItem1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
    {
    MenuItem mi = (MenuItem) sender;
    // if the item is a seperator
    if ( mi.Text == "-" ) 
    {
    e.ItemHeight = 7;

    else 
    {
    // get the item's text size
    SizeF miSize = e.Graphics.MeasureString(mi.Text, System.Windows.Forms.SystemInformation.MenuFont);
    int scWidth = 0;
    // get the short cut width
    if ( mi.Shortcut != Shortcut.None ) 
    {
    SizeF scSize = e.Graphics.MeasureString(mi.Shortcut.ToString(), System.Windows.Forms.SystemInformation.MenuFont);
    scWidth = Convert.ToInt32(scSize.Width);
    }
    // set the bounds
    int miHeight = Convert.ToInt32(miSize.Height) + 7;
    if (miHeight < 25) miHeight = 22;
    e.ItemHeight = miHeight;
    e.ItemWidth = Convert.ToInt32(miSize.Width) + scWidth + (24 * 2);
    }
    }
    }
      

  2.   

    Pannel加边框
    private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    { Panel lbl = (Panel)sender;
    Rectangle rect = new Rectangle(e.ClipRectangle.X, 
    e.ClipRectangle.Y, 
    e.ClipRectangle.Width -1, 
    e.ClipRectangle.Height-1);
    e.Graphics.DrawRectangle(new Pen(Color.Blue),rect );
    }
      

  3.   

    添加一个边框我知道应该这么写,但是我想的效果是当鼠标移动到panel上时显示边框,鼠标移开时就没有了
      

  4.   

    我想实现的是当鼠标移动到panel上时,才画一个矩形边框,鼠标移开的时候就不显示这个边框了,上面的那部分代码任何时候都会显示一个边框的
      

  5.   

    变通一下不就行了吗
    Rectangle rect1;
    private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
    this.rect1 = e.ClipRectangle;
    }
    private void panel1_MouseEnter(object sender, System.EventArgs e)
    {
    Graphics g = ((Panel)sender).CreateGraphics();
    Rectangle rect = new Rectangle(this.rect1.X,
    this.rect1.Y, 
    this.rect1.Width -1, 
    this.rect1.Height-1);
    g.DrawRectangle(new Pen(Color.Blue),rect);
    }private void panel1_MouseLeave(object sender, System.EventArgs e)
    {
    Graphics g = ((Panel)sender).CreateGraphics();
    Rectangle rect = new Rectangle(this.rect1.X,
    this.rect1.Y, 
    this.rect1.Width -1, 
    this.rect1.Height-1);
    g.DrawRectangle(new Pen(((Panel)sender).BackColor),rect);
    }
      

  6.   

    至于菜单实现你想要的效果
    参考
    http://www.codeproject.com/cs/menu/MhOffice2003Menus.asp
    结合我给你的代码就能实现了