Winform RichTextBox 通过API的方式进行设置

   1:  public partial class Form1 : Form
   2:  {
   3:      public const int WM_USER = 0x0400;
   4:      public const int EM_GETPARAFORMAT = WM_USER + 61;
   5:      public const int EM_SETPARAFORMAT = WM_USER + 71;
   6:      public const long MAX_TAB_STOPS = 32;
   7:      public const uint PFM_LINESPACING = 0x00000100;
   8:      [StructLayout(LayoutKind.Sequential)]
   9:      private struct PARAFORMAT2
  10:      {
  11:          public int cbSize;
  12:          public uint dwMask;
  13:          public short wNumbering;
  14:          public short wReserved;
  15:          public int dxStartIndent;
  16:          public int dxRightIndent;
  17:          public int dxOffset;
  18:          public short wAlignment;
  19:          public short cTabCount;
  20:          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
  21:          public int[] rgxTabs;
  22:          public int dySpaceBefore;
  23:          public int dySpaceAfter;
  24:          public int dyLineSpacing;
  25:          public short sStyle;
  26:          public byte bLineSpacingRule;
  27:          public byte bOutlineLevel;
  28:          public short wShadingWeight;
  29:          public short wShadingStyle;
  30:          public short wNumberingStart;
  31:          public short wNumberingStyle;
  32:          public short wNumberingTab;
  33:          public short wBorderSpace;
  34:          public short wBorderWidth;
  35:          public short wBorders;
  36:      } 
  37:   
  38:      public Form1()
  39:      {
  40:          InitializeComponent();
  41:      } 
  42:   
  43:      private void MenuItemExit_Click(object sender, EventArgs e)
  44:      {
  45:          Application.Exit();
  46:      } 
  47:   
  48:      private void MenuItemOpen_Click(object sender, EventArgs e)
  49:      {
  50:          if (openFileDialog1.ShowDialog() == DialogResult.OK)
  51:          {
  52:              richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.PlainText);
  53:          }
  54:      } 
  55:   
  56:      [DllImport("user32", CharSet = CharSet.Auto)]
  57:      private static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref PARAFORMAT2 lParam);
  58:   
  59:      private void Form1_Load(object sender, EventArgs e)
  60:      {
  61:          PARAFORMAT2 fmt = new PARAFORMAT2();
  62:          fmt.cbSize = Marshal.SizeOf(fmt);
  63:          fmt.bLineSpacingRule = 4;
  64:          fmt.dyLineSpacing = 400;//可修改的行高数值。我使用的300,感觉较为合适,这个500有点宽了!
  65:          fmt.dwMask = PFM_LINESPACING;
  66:          SendMessage(new HandleRef(this.richTextBox1, richTextBox1.Handle), EM_SETPARAFORMAT, 4, ref fmt);
  67:   
  68:   
  69:          richTextBox1.BackColor = Color.FromArgb(231, 244, 254);
  70:      }
  71:  }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/firebird2010/archive/2009/11/30/4907923.aspx

posted @ 2011-05-02 18:09  文明的天空  阅读(263)  评论(0编辑  收藏  举报