日期:2014-05-17  浏览次数:21121 次

200分 C# RichTextBox 高度自适应问题
解决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;