文本框与在左侧垂直滚动 [英] TextBox with vertical scrollbar on the left side

查看:172
本文介绍了文本框与在左侧垂直滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Windows窗体在C#中,其中有一个窗体上的多行TextBox控件的应用程序。

I'm developing a Windows Forms application in C#, which has a multiline TextBox control on a form.

由于特定的(无关)的原因,这个文本框需求在TextBox控件的的的侧面垂直滚动条。我已经偏离方向搜索解决方案,但我找不到任何...所以我的问题是:

Due to specific (irrelevant) reasons, this TextBox needs a vertical scrollbar on the left side of the TextBox control. I've off course searched for a solution, but I couldn't find any... so my questions are:

1)有没有一种方法,使自动一个TextBox控件(或从文本框或TextBoxBase派生的用户控件)的垂直滚动条在左边,而不是右边出现?这是首选的方法,因为所有scolling然后仍然通过控制处理。由于chancing RightToLeft属性对于这样一个TextBox实际移动滚动条到左边,我觉得必须有在这里利用的黑客攻击。

1) Is there a way to make the automatic vertical scrollbar of a TextBox control (or a usercontrol derived from TextBox or TextBoxBase) appear on the left instead of the right? This is the preferred method, since all scolling is then still handled by the control. Since chancing the RightToLeft property for such a TextBox actually moves the scrollbar to the left, I feel there must be a hack to be exploited here.

2)有,我可以在文本框滚动我IMessageFilter实施拦截的消息,即使它没有滚动条?即用户可以滚动使用箭头键和文本框上下移动的线条,但我无法找到时发生解雇任何消息。

2) Is there a message that I can intercept with my IMessageFilter implementation when the TextBox is scrolled, even though it doesn't have scrollbars? I.e. a user can scroll using the arrow keys and the textbox will move lines up and down, but I can't find any messages fired when that occurs.

的也许另一个想法如何做到这一点。

Maybe another idea of how to accomplish this?

编辑补充:文本需要被对准的右键的水平!否则我早就解决了。

Edit to add: The text needs to be aligned to the right horizontally! Otherwise I would have solved it already.

新的编辑为2014年11月3日:好吧,BenVlodgi的评论后,我开始心存疑虑我自己的理智。所以,我创建了一个测试项目,现在我还记得,为什么设置从右至左不能正常工作。

New edit as of 11/03/2014: Okay, after BenVlodgi's comment I started having doubts about my own sanity. So I created a test project and now I remember why setting RightToLeft to Yes was not working.

图片下方显示左侧与设置常规的文本框。滚动条是在左边和右边的文本,但文本不正常显示。在句末的周期移动在句子的前面。

The image below shows a regular TextBox on the left with that setting. The scrollbar is on the left and the text on the right, but the text is not shown properly. The period at the end of the sentence is moved in front of the sentence.

第二个TextBox控件是一个在LarsTech的回答表明,其功能正常和不动任何标点。

The second TextBox control is the one suggested in LarsTech's answer, which functions correctly and does not move any punctuation.

所以,我接受并奖励赏金LarsTech的答案。

Therefore, I accept and reward the bounty to LarsTech's answer.

推荐答案

我花了一些来自雷切尔圣加伦的链接,示例代码,并提出这个版本的文本框的:

I took some of the example code from Rachel Gallen's link and made this version of the TextBox:

public class TextBoxWithScrollLeft : TextBox {
  private const int GWL_EXSTYLE = -20;
  private const int WS_EX_LEFTSCROLLBAR = 16384;

  [DllImport("user32", CharSet = CharSet.Auto)]
  public extern static int GetWindowLong(IntPtr hWnd, int nIndex);

  [DllImport("user32")]
  public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

  protected override void OnHandleCreated(EventArgs e) {
    base.OnHandleCreated(e);
    int style = GetWindowLong(Handle, GWL_EXSTYLE);
    style = style | WS_EX_LEFTSCROLLBAR;
    SetWindowLong(Handle, GWL_EXSTYLE, style);
  }
}



我从来没有这么做过,但结果似乎已经奏效:

I've never done it before, but the results seem to have worked:

这篇关于文本框与在左侧垂直滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆