gpt4 book ai didi

.net - 如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?

转载 作者:行者123 更新时间:2023-12-03 11:36:48 25 4
gpt4 key购买 nike

给定一个带有 MultiLine = true 的 WinForms TextBox 控件和 AcceptsTab == true ,如何设置显示的制表符的宽度?

我想将它用作插件的快速而肮脏的脚本输入框。它真的根本不需要花哨,但是如果选项卡不显示为 8 个字符宽,那就太好了...

最佳答案

我想发送EM_SETTABSTOPS到 TextBox 的消息将起作用。

// 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首先运行。
  • Link at MSDN
  • Here is another link
  • 关于.net - 如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1298406/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com