TextBox自身是没有Paint事件,自己写一个事件,加入上去就行了。 如下: protected override void OnPaint(PaintEventArgs e) { Graphics g = this.textBox1.CreateGraphics(); g.SmoothingMode = SmoothingMode.AntiAlias; //Fill the background use the texture brush //and then apply a white wash g.FillRectangle(new SolidBrush(Color.Red), ClientRectangle); g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle); //Add a Red rectangle and a yellow one that overlaps it g.FillRectangle(new SolidBrush(Color.Red), 20, 20, 50, 50); g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.Yellow)), 40, 40, 50, 50); //Add a circle that is filled with a translucent hatch HatchBrush hb = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Green, Color.FromArgb(100, Color.Yellow)); g.FillEllipse(hb, 250, 10, 100, 100); //Now create a rectangle filled with a gradient brush Rectangle r = new Rectangle(300, 250, 100, 100); LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow,LinearGradientMode.BackwardDiagonal); g.FillRectangle(lb, r); g.ResetTransform(); }
急!!!
谢谢!!!
//---------------------------------------------
// BetterBlockOut.cs ?2001 by Charles Petzold
//---------------------------------------------
using System;
using System.Drawing;
using System.Windows.Forms;class BetterBlockOut: Form
{
bool bBlocking, bValidBox;
Point ptBeg, ptEnd;
Rectangle rectBox; public static void Main()
{
Application.Run(new BetterBlockOut());
}
public BetterBlockOut()
{
Text = "Better Blockout";
BackColor = SystemColors.Window;
ForeColor = SystemColors.WindowText;
}
protected override void OnMouseDown(MouseEventArgs mea)
{
if (mea.Button == MouseButtons.Left)
{
ptBeg = ptEnd = new Point(mea.X, mea.Y); Graphics grfx = CreateGraphics();
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
grfx.Dispose(); bBlocking = true;
}
}
protected override void OnMouseMove(MouseEventArgs mea)
{
if (bBlocking && (mea.Button & MouseButtons.Left) != 0)
{
Graphics grfx = CreateGraphics();
grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
ptEnd = new Point(mea.X, mea.Y);
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
grfx.Dispose();
Invalidate();
}
}
protected override void OnMouseUp(MouseEventArgs mea)
{
if (bBlocking)
{
Graphics grfx = CreateGraphics();
rectBox = Rect(ptBeg, new Point(mea.X, mea.Y));
grfx.DrawRectangle(new Pen(ForeColor), rectBox);
grfx.Dispose(); bBlocking = false;
bValidBox = true;
Invalidate();
}
}
protected override void OnKeyPress(KeyPressEventArgs kpea)
{
if (bBlocking && kpea.KeyChar == '\x001B') // Escape
{
Graphics grfx = CreateGraphics();
grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
grfx.Dispose();
bBlocking = false;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics; if (bValidBox)
grfx.FillRectangle(new SolidBrush(ForeColor), rectBox); if (bBlocking)
grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
}
Rectangle Rect(Point ptBeg, Point ptEnd)
{
return new Rectangle(Math.Min(ptBeg.X, ptEnd.X),
Math.Min(ptBeg.Y, ptEnd.Y),
Math.Abs(ptEnd.X - ptBeg.X),
Math.Abs(ptEnd.Y - ptBeg.Y));
}
}
如下:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = this.textBox1.CreateGraphics();
g.SmoothingMode = SmoothingMode.AntiAlias; //Fill the background use the texture brush
//and then apply a white wash
g.FillRectangle(new SolidBrush(Color.Red), ClientRectangle);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle); //Add a Red rectangle and a yellow one that overlaps it
g.FillRectangle(new SolidBrush(Color.Red), 20, 20, 50, 50);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.Yellow)), 40, 40, 50, 50); //Add a circle that is filled with a translucent hatch
HatchBrush hb = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Green, Color.FromArgb(100, Color.Yellow));
g.FillEllipse(hb, 250, 10, 100, 100); //Now create a rectangle filled with a gradient brush
Rectangle r = new Rectangle(300, 250, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow,LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lb, r); g.ResetTransform();
}