解决200分全送
纠结2天了。
动态创建RichTextBox,不要滚动条,高度随内容变化而变化
代码:
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);        RichTextBox richTextBox = null;
        int sf = 0;
        int pnlHight = 0;
        void richTextBox_Resize(object sender, EventArgs e)
        {
            int EM_GETLINECOUNT = 0x00BA;//获取总行数的消息号 
            int lc = SendMessage(richTextBox.Handle, EM_GETLINECOUNT, IntPtr.Zero, "");
            sf = richTextBox.Font.Height * (lc + 1) + richTextBox.Location.Y;
            richTextBox.Height = sf-30;
            richTextBox.Size = new System.Drawing.Size(912, richTextBox.Height);        
        }private void createControls(string Message, string SendName, string SendTime, int replyId, int i)
        {
            Panel pnl = new Panel();
            pnl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;            richTextBox = new RichTextBox();
            richTextBox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;       
            richTextBox.Name = "richTextBox" + "_" + i;
            richTextBox.TabIndex = 7;
            richTextBox.Text = Message;
            richTextBox.ReadOnly = true;      
            
            richTextBox.ScrollBars = RichTextBoxScrollBars.None;
            richTextBox.Anchor = (AnchorStyles.Right | AnchorStyles.Left);
            //得到RichTextBox高度
            int EM_GETLINECOUNT = 0x00BA;//获取总行数的消息号 
            int lc = SendMessage(richTextBox.Handle, EM_GETLINECOUNT, IntPtr.Zero, "");
            int sf = richTextBox.Font.Height * (lc + 1) + richTextBox.Location.Y;
            richTextBox.Height = sf;
            pnlHight = richTextBox.Height;            richTextBox.Resize += new EventHandler(richTextBox_Resize);
            richTextBox.Location = new System.Drawing.Point(20, 30);
            pnl.Controls.Add(richTextBox);            richTextBox.Text+="\r\n"+richTextBox.Location.X+"__Y:"+richTextBox.Location.Y;
            richTextBox.Text += "\r\n" +"Heihgt:"+ richTextBox.Height;           pnl.Controls.Add(richTextBox);
            pnl.Location = new System.Drawing.Point(0,size);
            pnl.Name = "panel" + "_" + i;
            pnl.Size = new System.Drawing.Size(1200, richTextBox.Height+40);
            pnl.AutoScroll = true;
            this.pnlMessage.Controls.Add(pnl);
            size += richTextBox.Height + 40;
}

每个RichTextBox
20__Y:30  坐标,X:20 Y:30 
Heihgt:28  高度:28

RichTextBox的坐标会上下变化,上图中第一个位置偏下了,第二个开始都是正常的,
这2个图RichTextBox的位置会随RichTextBox的高度变化而向下面偏移很多,不知道什么原因,还请大家看下上面代码有什么问题,纠结2天了,,
或者有什么更好的办法提供下,,,谢谢大家了

解决方案 »

  1.   

    RichTextBox外面套一个panel,定位pannel,RichTextBox定位0,0
      

  2.   

       pnl.Controls.Add(richTextBox);
                pnl.Location = new System.Drawing.Point(0,size);貌似这个是个关键
      

  3.   

      再次申请200分  pnl.Controls.Add(richTextBox);
      pnl.Location = new System.Drawing.Point(0,size);貌似这个是个关键
      

  4.   

    哎哟

    回答过 这个问题
    http://topic.csdn.net/u/20120315/09/05334c86-15cf-4983-854a-2485d6f46dea.htmlrichTextBox1.ScrollBars = RichTextBoxScrollBars.None;
        
        
      private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)
      {
      richTextBox1.Height = e.NewRectangle.Height;
      }
      

  5.   


    不是这个问题,这个是panel的位置,panel的位置再怎么变,richTextBox的位置是在panel内的,
      

  6.   

    试试这样吧,AppendText("内容"+"\r"),后面加上\r换行        private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                int lineheight = richTextBox1.Font.Height;
                int linescount = richTextBox1.Lines.Length;
                if (linescount * lineheight > richTextBox1.Height)
                    richTextBox1.Height = lineheight * linescount;
            }        private void timer1_Tick(object sender, EventArgs e)//定时插入数据
            {
                richTextBox1.AppendText("你好大恢复啊哈赶快哈个卡号干哈是\r");
            }
      

  7.   

    肯定是你的size算法有问题,建设你把发关人,发送时间及richTextBox这所有的控件集成一个用户控件,richTextBox直接去用户控件里调整,外面位置就像你调整一个单独的控件一样调整了,或者装在一个panel控件里,直接调整panel也可以
      

  8.   


    擦,原来是这句代码的原因导致的,,,这句代码去掉就好了,,,,      richTextBox.Anchor = (AnchorStyles.Right | AnchorStyles.Left);
      

  9.   

     richTextBox.Anchor = (AnchorStyles.Right | AnchorStyles.Left);不解
      

  10.   

           private void FPicture_Load(object sender, EventArgs e)
            {
                PictBox.Rtf = PicBoxes.Rtf;
                int ImgStartIndex = PictBox.Rtf.IndexOf("\\picw");  //获取字符串的位置索引
                int ImgEnd1Index = PictBox.Rtf.IndexOf("\\pich");   //获取字符串的位置索引
                int ImgEnd2Index = PictBox.Rtf.IndexOf("\\picwgoal");//获取字符串的位置索引之间的数据
                string a = PictBox.Rtf.Substring(ImgStartIndex + 5, ImgEnd1Index - ImgStartIndex - 5);  
                string b = PictBox.Rtf.Substring(ImgEnd1Index + 5, ImgEnd2Index - ImgEnd1Index - 5);
                int c = Convert.ToInt16(a);
                c = c / 26;                 //与正常像素大约26倍关系,goal的大约为15被关系
                int d=Convert.ToInt16(b);
                d=d/26;
                if (c>0&&d>0)       //图片太大会产生负值,大图使用滚动条形式
                {
                    PictBox.Width = c;
                    PictBox.Height = d;
                    this.Width = c+50;
                    this.Height = d+50;
                }
                //PictBox.Enabled = false;  //导致滚动条无法使用        }