C# richTextBox

<span style="font-size:14px;">避免闪烁的方法</span>
1、WM_SETREDRAW办法
SendMessage(this.richTextBox1.Handle, WM_SETREDRAW, 0, IntPtr.Zero);
 this.richTextBox1.SelectionStart = 0;
 this.richTextBox1.SelectionLength = m_curIndex;
 m_curIndex++;
 SendMessage(this.richTextBox1.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
 this.richTextBox1.Refresh();
 2、LockWindowUpdate办法
LockWindowUpdate(this.richTextBox1.Handle.ToInt32());
 this.richTextBox1.SelectionStart = 0;
 this.richTextBox1.SelectionLength = m_curIndex;
 m_curIndex++;
 LockWindowUpdate(0);
 this.richTextBox1.Refresh();
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //this.button1
            Button btn = (Button)sender;

            string strTrace;
            strTrace = "hello, world";
            Trace.WriteLine(strTrace);

            timerPrintMessage.Interval = 100;
            timerPrintMessage.Enabled = true;
            timerPrintMessage.Tag = "沉默的时光。Things can only get better...";
            timerPrintMessage.Start();
        }
        

        static int timerCount = 0;
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
        private const int WM_SETREDRAW = 0xB; 
        private void timerPrintMessage_Tick(object sender, EventArgs e)
        {    
            string strMessage = timerPrintMessage.Tag as string;

            strMessage = strMessage.Substring(0, strMessage.Length - 3);    
            switch (timerCount++ % 3)
            {
                case 0:
                    strMessage +=  "...";
                    break;
                case 1:
                    strMessage +=  ".";
                    break;
                case 2:
                    strMessage +=  "..";
                    break;
            } 

            PrintMessage(strMessage, false);
        }

        static int posPrev = 0;
        public int PosPrev
        {
            get { return posPrev; }
            set { posPrev = value; }
        }

        public void PrintMessage(string strMessage, bool newLine = true)
        {
            SendMessage(this.richTextBoxDisplayMessage.Handle, WM_SETREDRAW, 0, IntPtr.Zero);      
            string strCurTime;
            strCurTime = DateTime.Now.ToString();
            if (!newLine)
            {
                richTextBoxDisplayMessage.Text = richTextBoxDisplayMessage.Text.Substring(0, posPrev);
            }
            string strLine = strCurTime + " " + strMessage;
            richTextBoxDisplayMessage.AppendText(strLine + Environment.NewLine);
            posPrev = richTextBoxDisplayMessage.Text.IndexOf(strLine);

            SendMessage(this.richTextBoxDisplayMessage.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
            richTextBoxDisplayMessage.Refresh();
            richTextBoxDisplayMessage.ScrollToCaret();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timerPrintMessage.Stop();
        }

    }
}



你可能感兴趣的:(C# richTextBox)