如何调整标签控件的大小

时间:2018-12-28 09:09:03

标签: c# winforms resize label

我试图允许用户在打印标签之前调整标签(控件,而不是字体)的大小。

为此,我使用了LabelPrint类,并将AutoSize属性设置为false。我已经编写了将字体大小调整为标签大小的代码。但是我无法更改标签尺寸...感谢您的帮助,这是代码。

 <client>
  <endpoint name ="upload" address="http://localhost:62193/uploadPdf.svc" binding="basicHttpBinding" contract="ServiceInterface.LargeData.IFileUpload" />

</client>

我试图在LabelPrint类中添加它:

class LabelPrint : Label
{

    public LabelPrint()
    {
        this.ResizeRedraw = true;
        this.AutoSize = false;
        this.TextAlign = ContentAlignment.MiddleCenter;
        this.Font = new Font(this.Font.FontFamily, 12);
        this.SizeChanged += new EventHandler(this.SizeLabelFont);
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == 0x84)
        {  // Trap WM_NCHITTEST
            var pos = this.PointToClient(new Point(m.LParam.ToInt32()));
            if (pos.X >= this.Size.Width - grab && pos.Y >= this.Size.Height - grab)
                m.Result = new IntPtr(17);  // HT_BOTTOMRIGHT
        }
    }
    private const int grab = 16;


    public void SizeLabelFont(object sender, EventArgs e)
    {
        // Only bother if there's text.
        string txt = this.Text;
        if (txt.Length > 0)
        {
            int best_size = 100;

            // See how much room we have, allowing a bit
            // for the Label's internal margin.
            int wid = this.DisplayRectangle.Width - 3;
            int hgt = this.DisplayRectangle.Height - 3;

            // Make a Graphics object to measure the text.
            using (Graphics gr = this.CreateGraphics())
            {
                for (int i = 1; i <= 100; i++)
                {
                    using (Font test_font =
                        new Font(this.Font.FontFamily, i))
                    {
                        // See how much space the text would
                        // need, specifying a maximum width.
                        SizeF text_size =
                            gr.MeasureString(txt, test_font);
                        if ((text_size.Width > wid) ||
                            (text_size.Height > hgt))
                        {
                            best_size = i - 1;
                            break;
                        }
                    }
                }
            }

            // Use that font size.
            this.Font = new Font(this.Font.FontFamily, best_size);
        }
    }
}

但是我仍然遇到同样的问题:AutoSize设置为false,所以当我增加字体大小时,我看不到我所有的label.Text,但是我无法调整标签本身的大小……我的意思是用鼠标和ResizeRedraw ()。

0 个答案:

没有答案