【问题标题】:How do I change the tab stop length for a textbox in visual studio?如何更改 Visual Studio 中文本框的制表位长度?
【发布时间】:2016-03-31 19:30:05
【问题描述】:

我正在为我拥有的一台旧袖珍 PC 制作代码编辑器程序,我希望能够在多行文本框中更改 \t 字符的大小。

我已经找了很长时间,我发现了这个EM_SETTABSTOPS,我不完全确定如何使用它,但我认为这是我需要使用的。这甚至可以做到吗?

【问题讨论】:

  • 如果这是一个丰富的编辑控件,那么是的,Windows 控件将侦听消息并相应地设置制表位。如果您使用的是 C#,那么这将是不安全的代码或 dll 导入。
  • SendMessage(tbMain.Handle, EM_SETTABSTOPS, 1, new int[] { width * 69});
  • 那我会把它放在哪里呢?编辑:它也是一个纯文本框而不是富文本框
  • 我想在您的主应用程序中创建 tbMain。
  • [DllImport("User32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, int[] lParam);

标签: c# visual-studio visual-studio-2008 windows-mobile pocketpc


【解决方案1】:

在您的表单类代码中:

private const UInt32 EM_SETTABSTOPS = 0x00CB;
private const int unitsPerCharacter = 4;

[DllImport("CoreDll.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref IntPtr lParam);

然后添加一个函数

public static void SetTextBoxTabStopLength(TextBox tb, int tabSizeInCharacters)
{
    // 1 means all tab stops are the the same length
    // This means lParam must point to a single integer that contains the desired tab length
    const uint regularLength = 1;

    // A dialog unit is 1/4 of the average character width
    int length = tabSizeInCharacters * unitsPerCharacter;

    // Pass the length pointer by reference, essentially passing a pointer to the desired length
    IntPtr lengthPointer = new IntPtr(length);
    SendMessage(tb.Handle, EM_SETTABSTOPS, (IntPtr)regularLength, ref lengthPointer);
}

然后,在 InitializeComponents() 之后,使用您的多行文本框调用该函数。

来源:http://www.pinvoke.net/default.aspx/user32.sendmessage

【讨论】:

    猜你喜欢
    • 2017-03-04
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多