代码之家  ›  专栏  ›  技术社区  ›  Technivorous

在特定位置将控件插入另一个控件?

  •  1
  • Technivorous  · 技术社区  · 6 年前

    Form1 我做的一张表格叫 TextBlock . 我正在制作一个文本编辑器,你可以在页面周围放置文本框,然后编辑它们(想想单词)。

    这是我的两张表格。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog.Filter = "Text File|*.txt";
            var result = saveFileDialog.ShowDialog();
    
            if (result == DialogResult.OK)
            {
                StreamWriter writer = new 
                StreamWriter(saveFileDialog.OpenFile());
                writer.Write(tb_Main.Text);
                writer.Dispose();
                writer.Close();
            }
        }
    
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
    
        }
    
        private void button_tb_Click(object sender, EventArgs e)
        {
            TextBlock tb_edit = new TextBlock();
            tb_edit.Text = "New Text Block";
            // tb_edit.Multiline = true;
            // tb_edit.Size = new Size(100,100);
            // tb_edit.MinimumSize = new Size(50, 50);
            tb_edit.form1 = this;
    
            tb_edit.TopLevel = false;
            tb_edit.btn_accepttb.BringToFront();
            tb_Main.Controls.Add(tb_edit);
            tb_edit.Show();
            tb_edit.BringToFront();
    
        }
    }
    

     public partial class TextBlock : Form
        {
            public Form1 form1;
            public TextBlock()
            {
                InitializeComponent();
            }
    
            private void btn_accepttb_Click(object sender, EventArgs e)
            {
                TextBox tb_edit = new TextBox();
                tb_edit.Text = "New Text Block";
                tb_edit.Multiline = true;
                tb_edit.Size = this.Size;
                int dif = form1.tb_Main.Lines.Count()*(int)tb_edit.Font.Size;
                Point loca = new Point(this.Location.X,this.Location.Y+dif);
                tb_edit.Location = this.Location;
    
    
                form1.tb_Main.Controls.Add(tb_edit);
                tb_edit.Show();
                tb_edit.BringToFront();
                form1.tb_Main.Controls.Remove(this);
            }
        }
    

    它的作用:它复制我的文本块,用于放置和调整大小。当你把它放在你想要的地方,你想要多大的时候,你点击按钮,它就会在那个位置用一个普通的文本框来代替它自己。

    我希望它做的是:目前它是一个例外工作。我将它添加到tbu Main(我的主文本框,它占据了整个表单)的控件中,在Form1中,它出现了。它的大小是正确的,除非我用文本填充tb\u Main,然后滚动,新的文本框保持在原来的位置,而其父文本框则在后面滚动。

    问:如果我向下滚动到我的文档,并决定我要这个文本框在这里,我如何确保它的位置相对的文本框滚动我把它。所以当我继续滚动时,它基本上会嵌入到我放置它的页面中(当我说“page”时,我指的是我的tbu Main)。

    1 回复  |  直到 6 年前
        1
  •  1
  •   γηράσκω δ' αεί πολλά διδασκόμε    6 年前

    最好创建一个 richtextbox 而不是 textbox 文本框 只有 用一个 富文本框 .

    子类 富文本框

    class MyRichTextBox : RichTextBox  {
    
        [DllImport( "user32.dll" )]
        private static extern bool GetScrollInfo( IntPtr hwnd, SBOrientation fnBar,
            ref SCROLLINFO lpsi );
    
        [StructLayout( LayoutKind.Sequential )]
        private struct SCROLLINFO {
            public uint cbSize;
            public ScrollInfoMask fMask;
            public int nMin;
            public int nMax;
            public uint nPage;
            public int nPos;
            public int nTrackPos;
        }
    
        private enum ScrollInfoMask : uint {
            SIF_RANGE = 0x1,
            SIF_PAGE = 0x2,
            SIF_POS = 0x4,
            SIF_DISABLENOSCROLL = 0x8,
            SIF_TRACKPOS = 0x10,
            SIF_ALL = ( SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS ),
        }
    
        private enum SBOrientation : int {
            SB_HORZ = 0x0,
            SB_VERT = 0x1,
        }
    
        private const int WM_VSCROLL = 0x115;
        private const int SB_LINEUP = 0;
        private const int SB_LINEDOWN = 1;
        private const int SB_PAGEUP = 2;
        private const int SB_PAGEDOWN = 3;
        private const int SB_THUMBPOSITION = 4;
        private const int SB_THUMBTRACK = 5;
        private const int SB_TOP = 6;
        private const int SB_BOTTOM = 7;
        private const int SB_ENDSCROLL = 8;
    
        private bool isThumbTrack = false;
        private TextBox childTextbox = null;
        private int scrollPos = 0;
    
    
        public MyTextBox( TextBox textbox ) { //here you pass the child textbox you want to scroll
    
            childTextbox = textbox;
    
        }
    
        protected override void OnVScroll( EventArgs e ) {
            if( childTextbox == null ) { base.OnVScroll( e ); return; }
    
            SCROLLINFO si = new SCROLLINFO();
            si.cbSize = (uint)Marshal.SizeOf( si );
            si.fMask = ScrollInfoMask.SIF_ALL;
    
            GetScrollInfo( this.Handle, SBOrientation.SB_VERT, ref si );
    
            //there is a difference when the user uses the thumb to get scroll pos.
            //When user uses the thumb we get *nTrackPos* otherwise *nPos*
            if( isThumbTrack == true ) {
                childTextbox.Location = new Point( childTextbox.Location.X, childTextbox.Location.Y - 
                                                   (si.nTrackPos - scrollPos ) );
    
                isThumbTrack = false;
                scrollPos = si.nTrackPos;
            }
            else {
                childTextbox.Location = new Point( childTextbox.Location.X, childTextbox.Location.Y -
                                                   ( si.nPos - scrollPos ) );
    
                scrollPos = si.nPos;
            }
    
    
            base.OnVScroll( e );
        }
    
        protected override void WndProc( ref Message m ) {
            int wParam;
    
            if(m.Msg == WM_VSCROLL ) {
    
                wParam = m.WParam.ToInt32();
                wParam &= 0xFFFF; //get low 16 bits of wParam
    
                //Check if user is using the thumb to scroll
                if( wParam == SB_THUMBTRACK ) {
                    isThumbTrack = true;
                }
    
            }
    
            base.WndProc( ref m );
        }
    
    }