问题描述
给定一个带有 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.
- 链接在 MSDN李>
- 这是另一个链接
这篇关于如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)