Custom Caret for WinForms TextBox

前端 未结 4 581
暗喜
暗喜 2020-11-30 09:43

I\'m developing a custom HyperTerminal like application in a WinForms .Net 2.0 application. I have a multiline TextBox in a Panel in which you can interact with a hardware

相关标签:
4条回答
  • 2020-11-30 09:51

    These are the list of Native Caret functions provided by Windows you can use them for you application.

        [DllImport("User32.dll")]
        static extern bool CreateCaret(IntPtr hWnd, int hBitmap, int nWidth, int nHeight);
    
        [DllImport("User32.dll")]
        static extern bool SetCaretPos(int x, int y);
    
        [DllImport("User32.dll")]
        static extern bool DestroyCaret();
    
        [DllImport("User32.dll")]
        static extern bool ShowCaret(IntPtr hWnd);
    
        [DllImport("User32.dll")]
        static extern bool HideCaret(IntPtr hWnd);
    

    Refer SharpDevelop, Source Code @ src\Libraries\ICSharpCode.TextEditor\Project\Src\Gui\Caret.cs

    0 讨论(0)
  • 2020-11-30 09:52

    I would use System.Drawing to draw a custom cursor (bitmap), maybe with a timer to let it blink like another cursor.

    Get the current position of the Cursor in pixels and draw a bitmap over that cursor. Can be tricky to find the correct position, but should be doable.

    Have a look here for Owner drawn textbox in winforms.

    0 讨论(0)
  • 2020-11-30 10:05

    Assume a form with a textbox on it:

    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();
        }
    
        private void Form1_Shown(object sender, EventArgs e)
        {
            CreateCaret(textBox1.Handle, IntPtr.Zero, 10, textBox1.Height);
            ShowCaret(textBox1.Handle);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 10:09

    Use:

    richTextBoxConsole.GetPositionFromCharIndex(cursorPos)
    

    Hide the normal caret and draw your own? Not tested, but should work I think.

    0 讨论(0)
提交回复
热议问题