在RichTextBox(c#)中禁用滚动
我编写代码以在单独的页面中显示文本,就像Microsoft Word一样,我使用一组文本框,当用户填充一个文本框时,新的文本框会自动显示,光标会移动到她身上 问题是,当用户在文本框中写入最后一行时,文本框会向下滚动一点,正如您将在运行此代码时看到的那样,因此如何禁用滚动 守则:在RichTextBox(c#)中禁用滚动,c#,winforms,richtextbox,C#,Winforms,Richtextbox,我编写代码以在单独的页面中显示文本,就像Microsoft Word一样,我使用一组文本框,当用户填充一个文本框时,新的文本框会自动显示,光标会移动到她身上 问题是,当用户在文本框中写入最后一行时,文本框会向下滚动一点,正如您将在运行此代码时看到的那样,因此如何禁用滚动 守则: using System; using System.Collections.Generic; using System.ComponentModel; u
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<myRTB> pages; // collection of our RichTextBox, use as pages
public Form1()
{
InitializeComponent();
pages = new List<myRTB>();
pages.Add(new myRTB());
pages[0].Width = 200;
pages[0].Height = 290;
pages[0].Location = new Point(50, 10);
pages[0].Name = "0";
this.Controls.Add(pages[0]);
this.Width = 300;
this.Height = 360;
this.AutoScroll = true;
}
public void AddPage(int correntPageIndex)
{
if (correntPageIndex == (pages.Count - 1))
{
// create a new page
pages.Add(new myRTB());
pages[correntPageIndex + 1].Width = 200;
pages[correntPageIndex + 1].Height = 290;
pages[correntPageIndex + 1].Location = new Point(50, pages[correntPageIndex].Location.Y + 300);
this.Controls.Add(pages[pages.Count - 1]);
this.Name = (correntPageIndex + 1).ToString();
}
bool CursorInEnd = (pages[correntPageIndex].SelectionStart == pages[correntPageIndex].TextLength);
// Transfer the last word on the previous page, to the new page
int lastLineIndex = pages[correntPageIndex].GetLineFromCharIndex(pages[correntPageIndex].TextLength - 2);
// find the index of the first char in the last line
int indexOfFirstCharInLastLine = pages[correntPageIndex].GetFirstCharIndexFromLine(lastLineIndex);
// find the index of the last space in the last line
int indexOfLastSpace = pages[correntPageIndex].Text.LastIndexOf(' ', indexOfFirstCharInLastLine);
string restOfString;
if (indexOfLastSpace < 0) // no spaces in the last line
{
restOfString = pages[correntPageIndex].Text.Substring(pages[correntPageIndex].TextLength - 1);
pages[correntPageIndex + 1].Text.Insert(0, restOfString);
pages[correntPageIndex].Text.Remove(pages[correntPageIndex].TextLength - 1);
}
else // there is spaces in the last line
{
restOfString = pages[correntPageIndex].Text.Substring(indexOfLastSpace + 1);
pages[correntPageIndex + 1].Text = pages[correntPageIndex + 1].Text.Insert(0, restOfString);
pages[correntPageIndex].Text = pages[correntPageIndex].Text.Remove(indexOfLastSpace + 1);
}
if (CursorInEnd)
{
// Move the cursor to next page
pages[correntPageIndex + 1].SelectionStart = restOfString.Length;
pages[correntPageIndex + 1].Focus();
}
}
}
class myRTB : RichTextBox
{
public myRTB()
{
this.ScrollBars = RichTextBoxScrollBars.None;
}
protected override void WndProc(ref Message m)
{
// catch the request resize message
if (m.Msg == (WM_REFLECT | WM_NOTIFY))
{
REQRESIZE rrs = (REQRESIZE)(Marshal.PtrToStructure(m.LParam, typeof(REQRESIZE)));
if (rrs.nmhdr.code == EN_REQUESTRESIZE)
{
if (rrs.rc.ToRectangle().Height > this.ClientRectangle.Height)
{
((Form1)Parent).AddPage(int.Parse(this.Name));
}
}
}
base.WndProc(ref m);
}
[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public IntPtr HWND;
public uint idFrom;
public int code;
public override String ToString()
{
return String.Format("Hwnd: {0}, ControlID: {1}, Code: {2}",
HWND, idFrom, code);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct REQRESIZE
{
public NMHDR nmhdr;
public RECT rc;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left, Top, Right, Bottom;
public override string ToString()
{
return String.Format("{0}, {1}, {2}, {3}", Left, Top, Right,
Bottom);
}
public Rectangle ToRectangle()
{
return Rectangle.FromLTRB(Left, Top, Right, Bottom);
}
}
public const int WM_USER = 0x400;
public const int WM_NOTIFY = 0x4E;
public const int WM_REFLECT = WM_USER + 0x1C00;
public const int EN_REQUESTRESIZE = 0x701;
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
列出页面;//RichTextBox的集合,用作页面
公共表格1()
{
初始化组件();
pages=新列表();
pages.Add(新的myRTB());
页面[0],宽度=200;
页数[0]。高度=290;
第[0]页。位置=新点(50,10);
页面[0]。名称=“0”;
this.Controls.Add(第[0]页);
这个。宽度=300;
这个。高度=360;
this.AutoScroll=true;
}
public void AddPage(int correntPageIndex)
{
if(correntPageIndex==(pages.Count-1))
{
//创建新页面
pages.Add(新的myRTB());
页面[correntPageIndex+1]。宽度=200;
页面[correntPageIndex+1]。高度=290;
页面[correntPageIndex+1]。位置=新点(50,页面[correntPageIndex]。位置。Y+300);
this.Controls.Add(pages[pages.Count-1]);
this.Name=(correntPageIndex+1).ToString();
}
bool CursorInEnd=(页面[correntPageIndex].SelectionStart==页面[correntPageIndex].TextLength);
//将上一页的最后一个单词转移到新页
int lastLineIndex=pages[correntPageIndex].GetLineFromCharIndex(pages[correntPageIndex].TextLength-2);
//查找最后一行中第一个字符的索引
int indexOfFirstCharInLastLine=页面[correntPageIndex].GetFirstCharIndexFromLine(lastLineIndex);
//在最后一行中查找最后一个空格的索引
int indexOfLastSpace=pages[correntPageIndex].Text.LastIndexOf(“”,indexOfFirstCharInLastLine);
弦复位;
if(indexOfLastSpace<0)//最后一行中没有空格
{
restOfString=pages[correntPageIndex].Text.Substring(pages[correntPageIndex].TextLength-1);
页面[correntPageIndex+1].Text.Insert(0,restOfString);
页面[correntPageIndex].Text.Remove(页面[correntPageIndex].TextLength-1);
}
else//最后一行有空格
{
restOfString=pages[correntPageIndex].Text.Substring(indexOfLastSpace+1);
pages[correntPageIndex+1]。Text=pages[correntPageIndex+1]。Text.Insert(0,restOfString);
pages[correntPageIndex].Text=pages[correntPageIndex].Text.Remove(indexOfLastSpace+1);
}
如果(粗略)
{
//将光标移动到下一页
页面[correntPageIndex+1]。SelectionStart=restOfString.Length;
页面[correntPageIndex+1]。焦点();
}
}
}
类myRTB:RichTextBox
{
公共桃金娘()
{
this.ScrollBars=RichTextBoxScrollBars.None;
}
受保护的覆盖无效WndProc(参考消息m)
{
//捕获请求调整大小消息
如果(m.Msg==(WM|U反射| WM|U通知))
{
REQRESIZE rrs=(REQRESIZE)(Marshal.PtrToStructure(m.LParam,typeof(REQRESIZE));
如果(rrs.nmhdr.code==EN_REQUESTRESIZE)
{
if(rrs.rc.ToRectangle().Height>this.ClientRectangle.Height)
{
((Form1)Parent.AddPage(int.Parse(this.Name));
}
}
}
基准WndProc(参考m);
}
[StructLayout(LayoutKind.Sequential)]
公共结构NMHDR
{
公共IntPtr HWND;
公共单位;
公共整数码;
公共重写字符串ToString()
{
返回String.Format(“Hwnd:{0},ControlID:{1},代码:{2}”,
HWND、idFrom、代码);
}
}
[StructLayout(LayoutKind.Sequential)]
公共结构重新调整大小
{
公共NMHDR NMHDR;
公共RECT rc;
}
[StructLayout(LayoutKind.Sequential)]
公共结构矩形
{
公共int左、上、右、下;
公共重写字符串ToString()
{
返回字符串.Forma
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
const int WM_USER = 0x400;
const int EM_HIDESELECTION = WM_USER + 63;
void OnAppend(string text)
{
bool focused = richTextBox1.Focused;
//backup initial selection
int selection = richTextBox1.SelectionStart;
int length = richTextBox1.SelectionLength;
//allow autoscroll if selection is at end of text
bool autoscroll = (selection==richTextBox1.Text.Length);
if (!autoscroll)
{
//shift focus from RichTextBox to some other control
if (focused)
textBox1.Focus();
//hide selection
SendMessage(richTextBox1.Handle, EM_HIDESELECTION, 1, 0);
}
richTextBox1.AppendText(text);
if (!autoscroll)
{
//restore initial selection
richTextBox1.SelectionStart = selection;
richTextBox1.SelectionLength = length;
//unhide selection
SendMessage(richTextBox1.Handle, EM_HIDESELECTION, 0, 0);
//restore focus to RichTextBox
if(focused) richTextBox1.Focus();
}
}