使用C#修改RichTextBox中的行距 [英] Modifying the line spaceing in a richtextbox using C#

查看:361
本文介绍了使用C#修改RichTextBox中的行距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.我需要修改RichTextBox中的行距.我正在用C#编程.

在此先感谢您!

I have a problem. I need to modify the line spaceing in a richtextbox. I''m programing in C#.

Thanks in advance!

推荐答案

这里有一些您可以尝试的代码.该表单包含一个RichTexBox控件和六个按钮以及一个TextBox控件.暂时会选择整个文本,因此间距会影响所有行,但是您可以根据需要自行更改.这里感兴趣的参数是:
-dyLineSpacing
-bLineSpacingRule
居住在结构PARAFORMAT.请阅读MSDN文档,以获取有关这两者如何共同控制行距的详尽解释:
Here''s some code you can experiment with. The form contains one RichTexBox control and six button plus one TextBox control. Momentarily the whole text is being selected so the spacing affects all lines, but you can change that yourself if you want. The parameters that are of interest here are:
- dyLineSpacing
- bLineSpacingRule
which live in struct PARAFORMAT. Please read the MSDN documentation for a thorough explanation on how these two play together to control linespacing: http://msdn.microsoft.com/en-us/library/bb787942(v=vs.85).aspx[^].

Here is the complete Form code (you need to construct the form yourself with the controls I mentioned at the beginning):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace RichTextBoxLineSpacing
{
    public partial class Form1 : Form
    {
        [DllImport("user32", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref PARAFORMAT lParam);

        const int PFM_SPACEBEFORE = 0x00000040;
        const int PFM_SPACEAFTER  = 0x00000080;
        const int PFM_LINESPACING = 0x00000100;

        const int SCF_SELECTION = 1;
        const int EM_SETPARAFORMAT = 1095;

        public Form1()
        {
            InitializeComponent();
        }

        private void setLineFormat(byte rule, int space)
        {
            PARAFORMAT fmt = new PARAFORMAT();
            fmt.cbSize = Marshal.SizeOf(fmt);
            fmt.dwMask = PFM_LINESPACING;
            fmt.dyLineSpacing = space;
            fmt.bLineSpacingRule = rule;
            richTextBox1.SelectAll();
            SendMessage( new HandleRef( richTextBox1, richTextBox1.Handle ),
                         EM_SETPARAFORMAT,
                         SCF_SELECTION,
                         ref fmt
                       );

        }

        private void button1_Click(object sender, EventArgs e)
        {
            setLineFormat(0, space);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            setLineFormat(1, space);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            setLineFormat(2, space);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            setLineFormat(3, space);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            setLineFormat(4, space);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            setLineFormat(5, space);
        }

        int space = 0;
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            String val = textBox1.Text;
            bool success = int.TryParse(val, out space);
            if (success)
            {
                textBox1.BackColor = Color.White;
            }
            else
            {
                textBox1.BackColor = Color.Red;
            }
        }
    }

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



该代码已在Visual Studio 2010的Windows Forms项目中进行了测试.

如果您有任何疑问,请对此解决方案发表评论.

祝您编程愉快!

-MRB



The code was tested in VisualStudio 2010 in a Windows Forms project.

If you have any doubts please leave a comment to this solution.

Happy coding!

-MRB


setLineFormat
有一些错误
??
setLineFormat
has some error
??


这篇关于使用C#修改RichTextBox中的行距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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