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();
        }

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值