MinimizeName在C#中的实现代码(解决了我那个GDI+的问题)

代码如下:

//测试用的文字是:中华人民1共和国万岁!!!我爱北23京4天安门!!!
//字体:宋体,小三
int MagicNumber = 5;
protected override void OnPaint(PaintEventArgs e){
        Graphics g = e.Graphics;
   
        g.DrawString(MeasureString(g,Text,Font,this.ClientSize.Width-MagicNumber),Font,SystemBrushes.WindowText,0,0);

        base.OnPaint (e);
}

private string MeasureString(Graphics g,string text,Font font,int maxwidth){
        SizeF sf = g.MeasureString(text,font);
        if(sf.Width<maxwidth)return text;

        int len = text.Length;
        string s = "";
        StringBuilder sb = new StringBuilder(64);

        for(int i=len-1;i>=0;i--){
                sb.Remove(0,sb.Length);
                sb.Append(text.Substring(0,i));
                sb.Append("...");
                s = sb.ToString();
                sf = g.MeasureString(s,font);
                if(sf.Width<=maxwidth)break;
        }
       
        return s;
}

private void Form1_Resize(object sender, System.EventArgs e){
        Invalidate();
}
这是效果1:

这是效果2:

这是效果3:

posted @ 2004-04-22 11:18  鞠强  阅读(1203)  评论(4编辑  收藏  举报

hello

world