private void label1_MouseEnter(object sender, EventArgs e)
{
label1.BorderStyle = BorderStyle.FixedSingle;
}当鼠标移到label1上时边框的颜色是黑色的我想请问怎么改变边框的颜色?请高手指点

解决方案 »

  1.   

    Label边框颜色不能直接修改.你可以做个自定义控件
    代码如下
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;namespace WinFormTest
    {
    /// <summary>
    /// BorderLabel 的摘要说明。
    /// </summary>
    public class BorderLabel : System.Windows.Forms.Label
    {
    /// <summary> 
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    //边框颜色和宽度
    private System.Drawing.Color lineColor ;
    private int lineWidth ;
    //边框颜色和宽度临时值
    private System.Drawing.Color tempColor ;
    private int tempWidth ;
    //此属性设置边框颜色
    public Color LineColor
    {
    set {lineColor =value;}
    get {return lineColor;}
    }
    //此属性设置边框宽度
    public int LineWidth
    {
    set {lineWidth =value;}
    get {return lineWidth;}
    } public BorderLabel():base()
    {
    // 该调用是 Windows.Forms 窗体设计器所必需的。
    InitializeComponent();

    tempColor =this.BackColor;
    tempWidth =LineWidth;
    // TODO: 在 InitializeComponent 调用后添加任何初始化 } /// <summary> 
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    protected override void OnPaint(PaintEventArgs pe)
    {
    // TODO: 在此添加自定义绘画代码 // 调用基类 OnPaint
    PaintBorder(pe.Graphics,tempColor,tempWidth);
    base.OnPaint(pe);


    }
    //鼠标到达控件事件
    protected override void OnMouseEnter(EventArgs e)
    {
    tempColor =LineColor;
    tempWidth =LineWidth;
    base.OnMouseEnter(e);
    base.Invalidate();

    }
    //鼠标离开控件事件
    protected override void OnMouseLeave(EventArgs e)
    {
    tempColor =this.BackColor;
    tempWidth =LineWidth;
    base.OnMouseLeave(e);
    base.Invalidate();
    }
    /// <summary>
    /// 画边框
    /// </summary>
    /// <param name="grap"></param>
    /// <param name="lineColor"></param>
    /// <param name="lineWidth"></param>
    protected void PaintBorder(Graphics grap,System.Drawing.Color lineColor,int lineWidth)
    {
    System.Drawing.Pen pen =new Pen(lineColor,lineWidth);
    SolidBrush brush =new SolidBrush(lineColor);
    System.Drawing.Rectangle rect =new Rectangle(0,0,this.Width,lineWidth); grap.FillRectangle(brush,rect);
    grap.DrawRectangle(pen,rect);
    rect.Y =this.Height-lineWidth;
    grap.FillRectangle(brush,rect);
    grap.DrawRectangle(pen,rect);

    rect.Width =lineWidth;
    rect.Height =this.Height;
    rect.Y =0;
    grap.FillRectangle(brush,rect);
    grap.DrawRectangle(pen,rect);
    rect.X =this.Width-lineWidth;
    grap.FillRectangle(brush,rect);
    grap.DrawRectangle(pen,rect); //Brush brush=new Brush();
    }
    #region 组件设计器生成的代码
    /// <summary> 
    /// 设计器支持所需的方法 - 不要使用代码编辑器 
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    { }
    #endregion }
    }
      

  2.   

    在www.c-sharpcorner.com/ 还是在codeproject  中有一个TextBoxXP的例子
      

  3.   

    Cnapollo(旁痞) 非常感谢你的回答,谢谢