2016-04-18 21 views
7

Tôi có thể hiện nay để đồng bộ hóa của tôi hai RichTextBox sử dụng potion mã này:RichTextBox chuyển đổi một số dòng để di chuyển vị trí thanh

private const int SB_HORZ = 0x0; 
private const int SB_VERT = 0x1; 
private const int WM_HSCROLL = 0x114; 
private const int WM_VSCROLL = 0x115; 
private const int SB_THUMBPOSITION = 4; 
[DllImport("user32.dll", CharSet = CharSet.Auto)] 
private static extern int GetScrollPos(int hWnd, int nBar); 
[DllImport("user32.dll")] 
private static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw); 
[DllImport("user32.dll")] 
private static extern bool PostMessageA(IntPtr hWnd, int nBar, int wParam, int lParam); 
internal int HScrollPos 
{ 
    private get { return GetScrollPos((int)this.Handle, SB_HORZ); } 
    set 
    { 
     SetScrollPos((IntPtr)this.Handle, SB_HORZ, value, true); 
     PostMessageA((IntPtr)this.Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0); 
    } 
} 
internal int VScrollPos 
{ 
    get { return GetScrollPos((int)this.Handle, SB_VERT); } 
    set 
    { 
     SetScrollPos((IntPtr)this.Handle, SB_VERT, value, true); 
     PostMessageA((IntPtr)this.Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0); 
    } 
} 

tôi có thể đồng bộ hóa RichTextBoxes trong khi phím, lên Vscroll sự kiện. Trên thực tế đây không phải là mục tiêu của tôi, tôi muốn đồng bộ hóa RichTextBoxes dựa của tôi về nội dung, Những gì tôi cần:

  • Nhận dạng dòng hiện tại chưa được chọn RichTextBox.
  • Đặt vị trí thanh cuộn bằng cách sử dụng số dòng trong RichTextBox khác (mà không làm mất trọng tâm từ hiện tại).
  • Nhận số dòng từ vị trí thanh cuộn.

Lưu ý: bạn đều được chào đón để hỏi xem bạn cần những chi tiết nữa.
Cảm ơn bạn trước.

+0

Tôi nghĩ rằng ví dụ này sẽ hữu ích cho bạn http://www.codeproject.com/Articles/12152/Numbering-lines- của RichTextBox-in-NET – askeet

+0

Đây có phải là Ứng dụng WinForm không? – Ian

+0

có, một hình thức cửa sổ –

Trả lời

0

Từ những gì tôi hiểu, bạn cần phải đồng bộ hóa cuộn trên 2 RichTextBox es dựa trên số dòng. Để lại cho tôi một bình luận nếu tôi hiểu lầm nó.

RichTextBox mở rộng:

public class RichTextBoxEx : RichTextBox 
{ 
    // combination of multiple events that may cause focus(caret) to change 
    public event EventHandler FocusChanged; 

    public RichTextBoxEx() 
    { 
     this.KeyPress += (s, e) => RaiseFocusChanged(); 
     this.KeyDown += (s, e) => RaiseFocusChanged(); 
     this.KeyUp += (s, e) => RaiseFocusChanged(); 
     this.MouseClick += (s, e) => RaiseFocusChanged(); 
    } 

    private void RaiseFocusChanged() 
    { 
     var focusChanged = FocusChanged; 
     if (focusChanged != null) 
     { 
      focusChanged(this, null); 
     } 
    } 

    public int GetFirstSelectedLine() 
    { 
     var index = GetFirstCharIndexOfCurrentLine(); 
     return GetLineFromCharIndex(index); 
    } 
    public int GetFirstVisibleLine() 
    { 
     var index = GetCharIndexFromPosition(new Point(1, 1)); 
     return GetLineFromCharIndex(index); 
    } 

    public void ScrollToLine(int line) 
    { 
     if (line < 0) 
      throw new ArgumentOutOfRangeException("line cannot be less than 0"); 

     // save the current selection to be restored later 
     var selection = new { SelectionStart, SelectionLength }; 

     // select that line and scroll it to 
     Select(GetFirstCharIndexFromLine(line) + 1, 0); 
     ScrollToCaret(); 

     // restore selection 
     Select(selection.SelectionStart, selection.SelectionLength); 
    } 
} 

Cách sử dụng:

void Main() 
{ 
    var mainScreenArea = Screen.PrimaryScreen.WorkingArea; 

    var rich1 = new RichTextBoxEx() { Width = mainScreenArea.Width/2 - 10, Dock = DockStyle.Left }; 
    var rich2 = new RichTextBoxEx() { Width = mainScreenArea.Width/2 - 10, Dock = DockStyle.Right }; 
    rich1.LoadFile(__RTF_FILE_0__); 
    rich2.LoadFile(__RTF_FILE_1__); 

    // pick one : 
    // synchronize by focus 
    rich1.FocusChanged += (s, e) => rich2.ScrollToLine(rich1.GetFirstSelectedLine()); 
    // synchronize by viewbox 
    // rich1.VScroll += (s, e) => rich2.ScrollToLine(rich1.GetFirstVisibleLine()); 

    var form = new Form(); 
    form.Controls.Add(rich1); 
    form.Controls.Add(rich2); 

    form.WindowState = FormWindowState.Maximized; 
    form.ShowDialog() 
} 
Các vấn đề liên quan