简体   繁体   中英

How to set location of cursor in Textbox

I am making a height-adjustable and vertically alignable textbox with following code. The reason why I should do this is because although I can make winform textbox height-adjustable, I still can't vertically align the text in the textbox. So I decided I have to draw the text OnPaint event. The textbox is showing correct alignment now, but cursor is still located on top of textbox. Is there any way to control this position as well?

public class TextBoxHeightAdjustable : System.Windows.Forms.TextBox
{

    public TextBoxHeightAdjustable()
    {
        this.AutoSize = false;
        this.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        // This never runs no matter what I try!
        base.OnPaint(e);
        // Create a StringFormat object with the each line of text, and the block 
        // of text centered on the page.
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;

        e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle, stringFormat);
    }
}

In spite of Hans' advice, I proceeded since the textbox will contain simple numeric-like text. As Hans said, there is not much option for us to deal with original cursor and lively displayed text. So I hided them with following extension method and by disabling double-clicking and keypress update.

    [DllImport("user32.dll")]
    static extern bool HideCaret(IntPtr hWnd);
    public static void HideCaret(this TextBox textBox)
    {
        HideCaret(textBox.Handle);
    }

So eventually following code works for my purpose but I can't say it's complete. I may still figure out how to draw my own cursor. Hope this helps others with similar issue.

public class TextBoxHeightAdjustable : System.Windows.Forms.TextBox
{
    const int WM_DBLCLICK = 0xA3;
    const int WM_LBUTTONDBLCLK = 0x203;


    public TextBoxHeightAdjustable() : base()
    {
        this.AutoSize = false;
        this.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
        this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
    }
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        //base.OnKeyPress(e);
        if (e.KeyChar == (char)Keys.Back)
        {
            Text = Text.Remove(Text.Length-1);
        } 
        else
        {
            Text += e.KeyChar;
        }
        e.Handled = true;
    }
    protected override void WndProc(ref Message m)
    {
        if ((m.Msg == WM_DBLCLICK) || (m.Msg == WM_LBUTTONDBLCLK))
        {
        }
        else
        {
            base.WndProc(ref m);
        }
    }
    protected override void OnTextChanged(System.EventArgs args)
    {
        //KeyEventArgs kpe = (KeyEventArgs) args;
        //this.Font = new Font(this.Font.FontFamily, 0);
        using (Graphics g = this.CreateGraphics())
        {
            g.FillRectangle(Brushes.White, ClientRectangle);
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
            g.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle, stringFormat);

        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        this.HideCaret();
        e.Graphics.FillRectangle(Brushes.White, ClientRectangle);
        // This never runs no matter what I try!
        //base.OnPaint(e);
        // Create a StringFormat object with the each line of text, and the block 
        // of text centered on the page.
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;

        e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), ClientRectangle, stringFormat);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM