简体   繁体   English

如何在文本框中设置光标的位置

[英]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. 我应该这样做的原因是因为虽然我可以使winform文本框高度可调,但我仍然无法垂直对齐文本框中的文本。 So I decided I have to draw the text OnPaint event. 所以我决定要绘制文本OnPaint事件。 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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