给定一个带有 MultiLine = true
和 AcceptsTab == true
的 WinForms TextBox 控件,如何设置显示的制表符的宽度?
Given a WinForms TextBox control with MultiLine = true
and AcceptsTab == true
, how can I set the width of the tab character displayed?
我想将其用作插件的快速而肮脏的脚本输入框.真的完全不需要花哨,但如果标签不显示为 8 个字符宽就好了……
I want to use this as a quick and dirty script input box for a plugin. It really doesn't need to be fancy at all, but it would be nice if tabs were not displayed as 8 characters wide...
我认为将 EM_SETTABSTOPS
消息发送到 TextBox 会起作用.
I think sending the EM_SETTABSTOPS
message to the TextBox will work.
// set tab stops to a width of 4
private const int EM_SETTABSTOPS = 0x00CB;
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr h, int msg, int wParam, int[] lParam);
public static void SetTabWidth(TextBox textbox, int tabWidth)
{
Graphics graphics = textbox.CreateGraphics();
var characterWidth = (int)graphics.MeasureString("M", textbox.Font).Width;
SendMessage
( textbox.Handle
, EM_SETTABSTOPS
, 1
, new int[] { tabWidth * characterWidth }
);
}
这可以在 Form
的构造函数中调用,但要注意:确保首先运行 InitializeComponents
.
This can be called in the constructor of your Form
, but beware: Make sure InitializeComponents
is run first.
这篇关于如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!