l46*_*kok 4 .net c# richtextbox winforms
我有一个标签,根据RichTextBox上的文字标记行号.我已经联系了Vscroll的事件来处理标签.
private void rtbLogicCode_VScroll(object sender, EventArgs e)
{
Point pt = new Point(0, 1);
int firstIndex = rtbLogicCode.GetCharIndexFromPosition(pt);
int firstLine = rtbLogicCode.GetLineFromCharIndex(firstIndex);
pt.X = ClientRectangle.Width;
pt.Y = ClientRectangle.Height;
int lastIndex = rtbLogicCode.GetCharIndexFromPosition(pt);
int lastLine = rtbLogicCode.GetLineFromCharIndex(lastIndex);
// Small correction
if (rtbLogicCode.Text.EndsWith("\n"))
lastLine++;
labelLogicCode.ResetText();
LabelLineNum(firstLine+1,lastLine);
}
#endregion
private void LabelLineNum(int startNum, int lastNum)
{
labelLogicCode.Font = UIConstant.DDCLogicCodeFont;
for (int i = startNum; i < lastNum; i++)
{
labelLogicCode.Text += i + Environment.NewLine;
}
}
Run Code Online (Sandbox Code Playgroud)
除了RichTextBox使用平滑滚动功能之外,一切似乎都能正常工作,在许多情况下用户没有滚动到下一行的情况下,这会使我的行编号搞砸.这会导致行号与RichTextBox上显示的实际文本不同步.
最后,我需要禁用smoothscrolling功能来完成此任务.我被告知你可以覆盖RichTextBox的postMessage API来禁用所提到的功能,但在搜索了很多文档之后,我找不到任何好的文档.
我将非常感谢有关如何禁用smoothscrolling功能的尽可能详细的解决方案.谢谢.
这是Microsoft 的一个VB示例,建议您需要拦截WM_MOUSEWHEEL
消息.
这是C#中的快速原型:
class MyRichTextBox : RichTextBox {
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);
const uint WM_MOUSEWHEEL = 0x20A;
const uint WM_VSCROLL = 0x115;
const uint SB_LINEUP = 0;
const uint SB_LINEDOWN = 1;
const uint SB_THUMBTRACK = 5;
private void Intercept(ref Message m) {
int delta = (int)m.WParam >> 16 & 0xFF;
if((delta >> 7) == 1) {
SendMessage(m.HWnd, WM_VSCROLL, (IntPtr)SB_LINEDOWN, (IntPtr)0);
} else {
SendMessage(m.HWnd, WM_VSCROLL, (IntPtr)SB_LINEUP, (IntPtr)0);
}
}
protected override void WndProc(ref Message m) {
switch((uint)m.Msg) {
case WM_MOUSEWHEEL:
Intercept(ref m);
break;
case WM_VSCROLL:
if(((uint)m.WParam & 0xFF) == SB_THUMBTRACK) {
Intercept(ref m);
} else {
base.WndProc(ref m);
}
break;
default:
base.WndProc(ref m);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1909 次 |
最近记录: |