WinForm richtextbox的深行间距和字符间距 [英] WinForm richtextbox deep line spacing and character spacing

查看:125
本文介绍了WinForm richtextbox的深行间距和字符间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Winform中在richtextbox上编辑行间距和字符间距?我已经尝试过PARAFORMAT2,但它不允许进行深度设置.我想像Photoshop一样设置间距.例如;

How to edit line spacing and character spacing on richtextbox in winform? I've tried PARAFORMAT2 but it does not allow deep setting. I want to set spacing like photoshop. For example;

图片中是三种不同的间距格式.如何在图片中设置1,2,3之类的间距?

In the picture is three different spacing format. How to set spacing like 1,2,3 in the picture?

推荐答案

行距

您可以发送 EM_SETPARAFORMAT 消息发送到富文本框控件,然后将

You can send EM_SETPARAFORMAT message to the rich text box control and pass PARAFORMAT2 as lparam. To control line spacing, you should set the PFM_LINESPACING flag in the dwMask member and set bLineSpacingRule and dyLineSpacing members of PARAFORMAT2 to suitable values based on your requirements.

由于您需要对行距进行微调,因此似乎4适用于 bLineSpacingRule ,然后您可以将 dyLineSpacing 设置为以缇为单位的任何值.有关 bLineSpacingRule 的可用选项的更多信息,请阅读

Since you need fine-tuning for line spacing, it seems 4 is suitable for bLineSpacingRule and then you can set dyLineSpacing to any value in twip unit. For more information about available options for bLineSpacingRule, read PARAFORMAT2 documentations.

public void SetSelectionLineSpacing(byte bLineSpacingRule, int dyLineSpacing)
{
    PARAFORMAT2 format = new PARAFORMAT2();
    format.cbSize = Marshal.SizeOf(format);
    format.dwMask = PFM_LINESPACING;
    format.dyLineSpacing = dyLineSpacing;
    format.bLineSpacingRule = bLineSpacingRule;
    SendMessage(this.Handle, EM_SETPARAFORMAT, SCF_SELECTION, ref format);
}

字符间距

基于sSpacing 文档.aspx"rel =" nofollow> CHARFORMAT2 ,设置字符间距对丰富的编辑控件显示的文本没有影响.

Based on documentation of sSpacing in CHARFORMAT2, setting character spacing has no effect on the text displayed by a rich edit control.

代码

public class ExRichText : RichTextBox
{
    [DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, Int32 msg, 
                                             Int32 wParam, ref PARAFORMAT2 lParam);

    private const int SCF_SELECTION = 1;
    public const int PFM_LINESPACING = 256;
    public const int EM_SETPARAFORMAT = 1095;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct PARAFORMAT2
    {
        public int cbSize;
        public uint dwMask;
        public Int16 wNumbering;
        public Int16 wReserved;
        public int dxStartIndent;
        public int dxRightIndent;
        public int dxOffset;
        public Int16 wAlignment;
        public Int16 cTabCount;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public int[] rgxTabs;
        public int dySpaceBefore;
        public int dySpaceAfter;
        public int dyLineSpacing;
        public Int16 sStyle;
        public byte bLineSpacingRule;
        public byte bOutlineLevel;
        public Int16 wShadingWeight;
        public Int16 wShadingStyle;
        public Int16 wNumberingStart;
        public Int16 wNumberingStyle;
        public Int16 wNumberingTab;
        public Int16 wBorderSpace;
        public Int16 wBorderWidth;
        public Int16 wBorders;
    }

    public void SetSelectionLineSpacing(byte bLineSpacingRule, int dyLineSpacing)
    {
        PARAFORMAT2 format = new PARAFORMAT2();
        format.cbSize = Marshal.SizeOf(format);
        format.dwMask = PFM_LINESPACING;
        format.dyLineSpacing = dyLineSpacing;
        format.bLineSpacingRule = bLineSpacingRule;
        SendMessage(this.Handle, EM_SETPARAFORMAT, SCF_SELECTION, ref format);
    }
}

这篇关于WinForm richtextbox的深行间距和字符间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆