改变RichTextBox光标(caret)的形状

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    
public partial class Form1 : Form
    {
        [DllImport(
"user32.dll")]
        
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);

        [DllImport(
"user32.dll")]
        
static extern bool ShowCaret(IntPtr hWnd);

        
public Form1()
        {
            InitializeComponent();
            richTextBox1.GotFocus 
+= new EventHandler(richTextBox1_GotFocus);
        }

        
private void richTextBox1_GotFocus(object sender, EventArgs e)
        {
            
this.BeginInvoke(new MethodInvoker(ChangeCaret));
        }

        
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
        {
            
this.ChangeCaret();
        }

        
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
        {
            
this.ChangeCaret();
        }

        
private void ChangeCaret()
        {
            CreateCaret(
this.richTextBox1.Handle, IntPtr.Zero, 1015);
            ShowCaret(
this.richTextBox1.Handle);
        }

        
private void richTextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            
this.ChangeCaret();
        }
    }
}
posted @ 2008-12-16 16:46  h2appy  阅读(542)  评论(0编辑  收藏  举报