Windows Mobile 下 自适应大小的 Label

在mobile下面,经常要显示一些信息,由于是手持设备,一般需要显示的大些才好看见。为此,我写了一个自适应大小的函数,修改Label的字体,实现信息的最大显示。
函数使用方法: 
Value 要显示的内容
LabShow 要显示到的Label类。

字体最小为8

 void setTitleShow(string value, Label labShow)
        {

            SizeF MyFontSize = SizeF.Empty;
            Font MyFont = null;
            string strTest;
            Bitmap bmp = new Bitmap(50, 50);
            Graphics g = Graphics.FromImage(bmp);
            strTest = value;

            Rectangle MyRect = new Rectangle(0, 0, labShow.Width, labShow.Height);

            MyFont = new Font("Tahoma", MyRect.Height, FontStyle.Regular);


            MyFontSize = g.MeasureString(strTest, MyFont);


            while ((MyFontSize.Width > MyRect.Width || MyFontSize.Height > MyRect.Height) && (MyFont.Size > 8))
            {

                MyFont = new Font("Tahoma", (MyFont.Size - .01F), FontStyle.Regular);


                MyFontSize = g.MeasureString(strTest, MyFont);

            }
            labShow.Font = MyFont;

            labShow.Text = value;

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值