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

How to set the TAB width in a Windows Forms TextBox control?(如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?)
本文介绍了如何在 Windows 窗体 TextBox 控件中设置 TAB 宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个带有 MultiLine = trueAcceptsTab == 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 宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)