我要自定义一个MessageBox
现 直接在Form 上写字
当内容超出了Form 的可视范围后
适当调整Form 的大小使内容显示出来
就像MessageBox 一样 无论写多少内容 都可以显示出来但是不知如何判断Form上的内容是否超出了Form 请高手解答........

解决方案 »

  1.   

    int w= System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;  
    int h= System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;  
      

  2.   

    刷新一下就看到了……
    你把TextBox改成多行的  它不就自动换行了么?  为什么要这么做
      

  3.   


    你这里的两个数值 是我在Form 上写的内容的边界吗?
      

  4.   


    用TextBox 也没办法让其根据内容调整大小
      

  5.   


    //参考
    public Form1()
            {
                InitializeComponent();            this.AutoSize = true;
                Label labelMessage = new Label();
                labelMessage.AutoSize = true;
                this.Controls.Add(labelMessage);            for (int i = 0; i < 10; i++)
                {
                    labelMessage.Text += i.ToString();
                }
                //动态调整label的显示位置,使其居中
                labelMessage.Left = (this.ClientRectangle.Width - labelMessage.Width) / 2;
                labelMessage.Top = (this.ClientRectangle.Height - labelMessage.Height) / 2;
            }
      

  6.   


    Label 的AutoSize 是对多行无效的 
    加入信息很多的话 不可能就显示在一行吧?
    MessageBox 是可以显示N 多内容
    并且自动调整窗体的大小以适应
      

  7.   

    那可以了这样protected override void OnPaint(PaintEventArgs e)
            {
                //这个可以换成你自己想显示的任何字符串
                string timeString = DateTime.Now.ToString("yyyy-MM-dd\r\nHH:mm:ss");
                SizeF strSize = e.Graphics.MeasureString(timeString, this.Font);
                //这里的显示位置可以根据你的需求来换
                float x = (this.ClientRectangle.Width - strSize.Width) / 2;
                float y = (this.ClientRectangle.Height - strSize.Height) / 2;            e.Graphics.DrawString(timeString, this.Font, Brushes.Black, x, y);
                base.OnPaint(e);
            }
      

  8.   


    Graphics g = this.infoLabel.CreateGraphics();            try
                {
                    StringFormat strFmt = new StringFormat();
                    strFmt.Alignment = StringAlignment.Near;
                    strFmt.LineAlignment = StringAlignment.Near;
                    SizeF sizeF = g.MeasureString(this.infoLabel.Text, this.infoLabel.Font, this.infoLabel.Width, strFmt);
                    this.infoLabel.Height = (int)Math.Round(sizeF.Height + 1.0f);
                }
                finally
                {
                    g.Dispose();
                }
    The infoLabel is the message label. this is only a sample code, you can adjust the form's size by the string area size. if you want more detail code can connect me.