Windows mobile 如何在windows mobile 6.5中动态更改标签高度

Windows mobile 如何在windows mobile 6.5中动态更改标签高度,windows-mobile,compact-framework,windows-mobile-6.5,windows-mobile-6,Windows Mobile,Compact Framework,Windows Mobile 6.5,Windows Mobile 6,在我的应用程序中,我将从长度未知的服务器获取文本。有人能告诉我们如何改变标签的高度,使文本在超过标签长度时不会被剪掉。使用图形。测量。下面是一个简化的示例: public class MyForm : Form { private string m_text; public string NewLabelText { get { return m_text; } set { m_text =

在我的应用程序中,我将从长度未知的服务器获取文本。有人能告诉我们如何改变标签的高度,使文本在超过标签长度时不会被剪掉。

使用
图形。测量
。下面是一个简化的示例:

public class MyForm : Form
{
    private string m_text;

    public string NewLabelText 
    { 
        get { return m_text; }
        set 
        {
             m_text = value;
             this.Refresh();
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (NewLabelText != null)
        {
            var size = e.Graphics.MeasureString(NewLabelText, label1.Font);
            label1.Width = (int)size.Width;
            label1.Height = (int)size.Height;
            label1.Text = NewLabelText;
            NewLabelText = null;
        }

        base.OnPaint(e);
    }
}

使用ctacke对问题采用的解决方案(标签的恒定宽度):


我无法使用CF测试这一点。CF中可能没有PreferredHeight,如果是,请使用label1.height。

但此代码更改了宽度和高度。如果只更改高度(添加一行)如何?问一个问题,说明宽度应该保持不变?添加换行是另一回事-OP没有要求这样做。如果这是所需要的,我也有一个解决方案,但它基本上需要创建自己的控件并手动处理文本图形。不难,但答案与只检查大小大不相同。@ctack在您的解决方案标签1中。Width得到的值大于mobile Width,因此文本再次被剪切。如果我使用约瑟夫的解决方案,我会得到多行,但我面临一个问题。如果我键入没有空格的长字符串,则文本不会被包装。例如:“dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd。看起来创建自己的控件可以解决我的问题。如果没有空格,字体不是固定宽度的,你必须测试并减少字符数,以获得正确的字符数(“WWWW”的裁剪方式与“LLLLL”不同)。你必须估计一个平均字符宽度,在那个点剪辑,然后使用MeasureString验证它是否合适。我得到了多行,但我面临一个问题。如果我键入没有空格的长字符串,则文本不会被包装。例如:“dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd。有没有办法在屏幕上手动检查和绘制字符。当您想开始自己的文本换行时,您将实现自己的绘制。我更喜欢使用文本框,为用户提供一个滚动条,而不是启动我自己的文本包装。当用户看到一个长期的项目被分成几行时,例如:
\myverylongsubderpathname

显示为
\MyVeryLong
subderpa
thName

,他/她应该怎么想?(该死,评论中不支持换行)
protected override void OnPaint(PaintEventArgs e)
{
    if (NewLabelText != null)
    {
        //get the width and height of the text
        var size = e.Graphics.MeasureString(NewLabelText, label1.Font);
        if(size.Width>label1.Width){
            //how many lines are needed to display the text
            int iLines = (int)(System.Math.Round((size.Width / label1.Width)+.5));
            //multiply with using the normal height of a one line text
            //label1.Height=iLines*label1.PreferredHeight; //preferredHeight not supported by CF
            label1.Height=(int)(iLines*size.Height*1.1); // add some gutter
        }
        label1.Text = NewLabelText;
        NewLabelText = null;
    }

    base.OnPaint(e);
}