如何自定义绘制一个TextBox保证金? [英] How to custom-draw a margin in a TextBox?
本文介绍了如何自定义绘制一个TextBox保证金?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要画一个边界线在一个WinForms TextBox中80个字符。下面是我尝试过,在我的文本框的子类:
I want to draw a margin line at 80 characters in a WinForms TextBox. Here is what I've tried, in my TextBox subclass:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
const int WM_PAINT = 0x00F;
if (m.Msg == WM_PAINT) {
DrawMargin();
}
}
void DrawMargin()
{
using (Pen pen = new Pen(Color.Gray, 1)) {
using (Graphics graphics = CreateGraphics()) {
float charWidth = graphics.MeasureString("M", Font).Width;
graphics.DrawLine(pen, charWidth * 80, 0, charWidth * 80, Height);
}
}
}
有至少有三个问题是:
- 当用户输入一些文字,该行的一部分被冲压出来(变白)。
- 当用户用鼠标选择一些文本,在上述情况发生一次。 当文本框滚动
- 在该行闪烁。
我注意到,TED记事本(它使用一个Win32编辑控制)是能够利用的余量没有任何问题,所以它似乎有可能做我想做的。谁能告诉我如何?
I notice that TED Notepad (which uses a Win32 EDIT control) is able to draw a margin without any problems, so it seems that it's possible to do what I want. Could anyone advise me how?
推荐答案
据我所知,这样做的最好的方法就是把一个WinForms.Panel在文本框:
As far as I can tell, the best way of doing this is simply to place a WinForms.Panel over the TextBox:
class FooTextBox : TextBox
{
public FooTextBox()
{
margin = new Panel();
margin.Enabled = false;
margin.BackColor = Color.LightGray;
margin.Top = 0;
margin.Height = ClientSize.Height;
margin.Left = <whatever>;
margin.Width = 1;
Controls.Add(margin);
}
Panel margin;
}
由于面板未启用,它没有考虑鼠标输入
Since the panel is not Enabled, it doesn't take mouse input.
这篇关于如何自定义绘制一个TextBox保证金?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文