【问题标题】:How to set location of cursor in Textbox如何在文本框中设置光标位置
【发布时间】:2012-12-21 20:58:38
【问题描述】:

我正在使用以下代码制作一个高度可调且垂直对齐的文本框。我应该这样做的原因是因为虽然我可以使winform文本框高度可调,但我仍然无法垂直对齐文本框中的文本。所以我决定我必须绘制文本 OnPaint 事件。文本框现在显示正确对齐,但光标仍位于文本框顶部。有没有办法控制这个位置?

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);
    }
}

【问题讨论】:

  • 你无法完成这项工作,TextBox 很特别。
  • @HansPassant 看了超过 3500 行的 TextBox 源代码,我同意你的看法。问题是我们的经理不相信我们不能这样做。
  • 不,这不是您可以查看的代码。 TextBox 是本机 Windows EDIT 控件的 .NET 包装类。由于 25 年的 appcompat hack,它肯定有数万行 C 代码。它始于 1980 年代,必须打破规则才能在 386SUX 上运行。尤其是绘画规则。

标签: c# winforms textbox


【解决方案1】:

不顾 Hans 的建议,我继续进行,因为文本框将包含简单的类似数字的文本。正如汉斯所说,我们没有太多选择来处理原始光标和生动显示的文本。所以我用下面的扩展方法和禁用双击和按键更新来隐藏它们。

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

所以最终以下代码可以满足我的目的,但我不能说它是完整的。我可能仍然想出如何绘制自己的光标。希望这可以帮助其他有类似问题的人。

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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2011-03-14
    • 2023-03-15
    相关资源
    最近更新 更多