【发布时间】:2015-01-15 12:10:46
【问题描述】:
我有一个 WinForms 应用程序、C#、.NET 4.0。
我在Form 上有一个Multiline TextBox。
除了通常的文字和TextBox 本身,我想在TextBox 的角落看到一个三角形:
为此,我通过以下方式覆盖 TextBox 的 WndProc 方法:
private const int WM_PAINT = 0x000f;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_PAINT:
base.WndProc(ref m);
paintInnerButton();
break;
default:
base.WndProc(ref m);
break;
}
}
private void paintInnerButton()
{
Point innerButtonLocation = ClientRectangle.Location + (ClientSize - UITools.InnerButtonSize);
var innerButtonRect = new Rectangle(innerButtonLocation, UITools.InnerButtonSize);
drawTriangle(CreateGraphics(), BackColor, innerButtonRect.Location);
}
private static void drawTriangle(Graphics gr, Color backColor, Point location)
{
Color innerButtonColor = _activeInnerButtonColor;
Point[] points = {
new Point(location.X, location.Y + InnerButtonSize.Height), // LEFT BOTTOM
new Point(location.X + InnerButtonSize.Width, location.Y), // RIGHT TOP
new Point(location.X + InnerButtonSize.Width, location.Y + InnerButtonSize.Height) // RIGHT BOTTOM
};
using (var brush = new LinearGradientBrush(
new Point(
location.X + (int)(InnerButtonSize.Width / 2.0),
location.Y + (int)(InnerButtonSize.Height / 2.0)),
new Point(location.X - 1 + InnerButtonSize.Width, location.Y + InnerButtonSize.Height),
backColor,
innerButtonColor
))
{
gr.FillPolygon(brush, points);
}
}
问题来了,当有长文本时,我按下键盘上的向下按钮将其向下滚动。出现几个较小的三角形:
任何想法,为什么会发生这种情况以及如何克服它?
【问题讨论】:
-
你不能得到这个可靠的。 TextBox 犯下的罪行可以追溯到 1980 年代,当时 Windows 必须在 386sux 机器上运行。它不使用 WM_PAINT 进行绘制。这会挫败任何在 ownerdraw 上的尝试。可以使覆盖控件的透明顶层窗口之类的东西起作用。
标签: c# winforms textbox ownerdrawn