richTextBox表の挿入

12238 ワード

別添資料:http://files.cnblogs.com/xe2011/richTextBox_InsertTable.rar
richTextBox插入表格
 
 
表の挿入
       /// <summary>

        ///     

        /// </summary>

        /// <param name="richTextBox"></param>

        /// <param name="col"> </param>

        /// <param name="row"> </param>

        /// <param name="AutoSize">=TRUE:            </param>

        private void InsertTable(RichTextBox richTextBox,int col, int row,bool AutoSize)

        {

            StringBuilder rtf = new StringBuilder();

            rtf.Append(@"{\rtf1 ");



            //int cellWidth = 1000;//col.1 width =1000

            int cellWidth = 1000; 



            if (AutoSize)

                //       (richTextBox.ClientSize.Width -       /    )*15

                cellWidth = (richTextBox.ClientSize.Width / row) * 15; //15  ROW           

 

            Text =  cellWidth.ToString() ;

            for (int i = 0; i < col; i++)

            {

                rtf.Append(@"\trowd");

                for (int j = 1; j <= row; j++)

                    rtf.Append(@"\cellx" + (j * cellWidth).ToString());

                rtf.Append(@"\intbl \cell \row"); //create row

            }

            rtf.Append(@"\pard");

            rtf.Append(@"}");

            richTextBox.SelectedRtf = rtf.ToString();

        }
 
 
拡張されたrichTextBox

using System;

using System.ComponentModel;

using System.Runtime.InteropServices;

using System.Windows.Forms;



namespace richTextBoxTableClass

{

    public class CustomRichTextBox : RichTextBox

    {



///        

        // P/Invoke declarations

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]

        private static extern IntPtr LoadLibrary(string path);



        private static IntPtr moduleHandle;



        protected override CreateParams CreateParams

        {

            get

            {

                if (moduleHandle == IntPtr.Zero)

                {

                    moduleHandle = LoadLibrary("msftedit.dll");

                    if ((long)moduleHandle < 0x20)

                    {

                        throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not load Msftedit.dll");

                    }

                }



                CreateParams createParams = base.CreateParams;

                createParams.ClassName = "RichEdit50W";

                if (this.Multiline)

                {

                    if (((this.ScrollBars & RichTextBoxScrollBars.Horizontal) != RichTextBoxScrollBars.None) &&!base.WordWrap)      

                    {

                        createParams.Style |= 0x100000;

                        if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None)

                        {

                            createParams.Style |= 0x2000;

                        }

                    }

                    if ((this.ScrollBars & RichTextBoxScrollBars.Vertical) != RichTextBoxScrollBars.None)

                    {

                        createParams.Style |= 0x200000;

                        if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None)

                        {

                            createParams.Style |= 0x2000;

                        }

                    }

                }

                if ((BorderStyle.FixedSingle == base.BorderStyle) && ((createParams.Style & 0x800000) != 0))

                {

                    createParams.Style &= -8388609;

                    createParams.ExStyle |= 0x200;

                }

                return createParams;

            }

        }





    }

}
Custoom RichText Box.C.S
 
 
呼び出し

        private void button1_Click(object sender, EventArgs e)

        {

                InsertTable(richTextBox1, 5, 4, false);

                InsertTable(richTextBox51,5, 4, false);

        }
View Code