WinForms richtextbox的自定义插入符, [英] Custom Caret for WinForms richtextbox,

查看:69
本文介绍了WinForms richtextbox的自定义插入符,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在开发Windows应用程序,因为我需要更改richtextbox的插入标记,即控件内显示的垂直线,是任何人都知道如何解决此问题的方法,请帮帮我.

谢谢.

Hi everybody,
I am developing a windows application, in that i need to change the caret of the richtextbox, ie the vertical line that shown inside the control, is anybody know how to solv this, pls help me.

Thanking you.

推荐答案

您必须进行一些子类化,但这是可能的.但是,我不确定如何更改插入符号的宽度...即,如果插入符号存在,字符之间的间距.

例如,将图像添加到您的资源中.就我而言,它被称为"arrow_down.png".然后,创建一个新类.我叫我CustomCaret.此类继承了System.Windows.Forms中的NativeWindow类.
You''d have to do some subclassing, but it''s possible. Though, I''m not sure how to change the caret width...ie, the space between characters if the caret is there.

As an example, add an image to your resources. In my case, it was called "arrow_down.png". Then, create a new class. I called mine CustomCaret. This class inherits the NativeWindow class found in System.Windows.Forms.
class CustomCaret : NativeWindow 



然后,您必须从"user32.dll"定义两个函数:CreateCaretShowCaret.类定义为:



Then you have to define two functions from "user32.dll": CreateCaret and ShowCaret. The class definitions are:

[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);



您需要关注一些特定的消息... WM_PAINT和WM_REFLECT + WM_ NOTIFY.这些值的



There are specific messages you''re going to be concerned with...the WM_PAINT and WM_REFLECT + WM_ NOTIFY. These have values of

const int WM_USER = 0x0400;
const int WM_NOTIFY = 0x004E;
const int WM_REFLECT = WM_USER + 0x1C00;
const int WM_PAINT = 0xF;



然后,当您收到这些消息时,请调用CreateCaretShowCaret.

但是,要真正获得呼叫,在创建新的CustomCaret对象时,传入要与CustomCaret一起使用的TextBox并将其句柄分配给CustomCaret.这也是从资源中获取位图的地方.



Then, when you get those messages, you call the CreateCaret and ShowCaret.

To actually get the calls, though, when you create the new CustomCaret object, pass in the TextBox that you want to use the CustomCaret with and assign it''s handle to the CustomCaret. This is also where you will get the bitmap from the resources.

public CustomCaret(RichTextBox CallingTextBox)
{
     this.AssignHandle( CallingTextBox.Handle);
     _assembly = Assembly.GetExecutingAssembly();
     _imageStream = _assembly.GetManifestResourceStream("TestFormCSharp.Resources.arrow_down.png");
     myCaret = new Bitmap(_imageStream);
}



_assembly,_imageStream和myCaret定义为



_assembly, _imageStream, and myCaret are defined as

Bitmap myCaret;
Assembly _assembly;
Stream _imageStream;



完整的代码如下:



The full code looks like:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Drawing;

namespace TestFormCSharp
{
    class CustomCaret : NativeWindow 
    {
        const int WM_USER = 0x0400;
        const int WM_NOTIFY = 0x004E;
        const int WM_REFLECT = WM_USER + 0x1C00;
        const int WM_PAINT = 0xF;
        Bitmap myCaret;
        Assembly _assembly;
        Stream _imageStream;

        [DllImport("user32.dll")]
        static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
        [DllImport("user32.dll")]
        static extern bool ShowCaret(IntPtr hWnd);

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if ((m.Msg == (WM_REFLECT + WM_NOTIFY)) || (m.Msg == WM_PAINT))
            {
                CreateCaret(this.Handle, myCaret.GetHbitmap(), 0,0);
                ShowCaret(this.Handle);
            }
        }

        public CustomCaret(RichTextBox CallingTextBox)
        {
            this.AssignHandle( CallingTextBox.Handle);
            _assembly = Assembly.GetExecutingAssembly();
            _imageStream = _assembly.GetManifestResourceStream("TestFormCSharp.Resources.arrow_down.png");
            myCaret = new Bitmap(_imageStream);
        }
    }
}



这将适用于任何位图. CreateCaret确实有传入的大小和宽度值,但是如果使用位图句柄,它将忽略这些值,这就是为什么我将它们设置为0的原因.正如我所说,这根本不会调整间距.



如果回答有帮助或回答了您的问题,请标记为.



This will work with any bitmap. The CreateCaret does have a size and width value passed in, but if the bitmap handle is used, it ignores those values, which is why I have set them to 0. As I said, this will not adjust the spacing at all.



If the response is helpful or answers your question, please mark it as so.


您好,
我尝试了您的代码,但出现错误,即该项目不包含"AssignHandle"的定义,因此请帮助我找到解决方案.
谢谢.
Hi,
I tried ur code,But i got an error ie, this project does not contain a definition for ''AssignHandle'', So pls help me for finding the solution.
Thanking you.


感谢您的回复,我会尽力解决并尽快与您联系,再次非常感谢您
Thank u for ur reply, i will try disa and contact u soon, once again thank u verymuch


这篇关于WinForms richtextbox的自定义插入符,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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