[英]How to set location of cursor in Textbox
我正在制作一個可調節高度且可垂直對齊的文本框,其中包含以下代碼。 我應該這樣做的原因是因為雖然我可以使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);
}
}
盡管漢斯的建議,我繼續說,因為文本框將包含簡單的類似數字的文本。 正如漢斯所說,我們沒有太多選擇來處理原始光標和生動的顯示文本。 因此,我使用以下擴展方法並禁用雙擊和按鍵更新來隱藏它們。
[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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.