我的PictureBox的问题,急急急!!!

usxue 2003-04-08 02:07:39
我在Form中加了一个PictureBox和TextBox,并在PictureBox中创建了一个Graphics gh,在TextBox中输入坐标点,然后在gh中画图,图形是画出来了,但当我切换一下窗口,就看不到刚才所画的图形了,为什么啊???有什么方法来解决这个问题吗?
还附带的一个问题就是,这些点我怎么才能把他们动态的放在一个Point的数组中?这个数组的长度是未知的,谢谢!!!
谢谢!!!
急急急!!!
:)
...全文
71 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
usxue 2003-04-08
  • 打赏
  • 举报
回复
好的,谢谢你们,
问题解决了,真的谢谢你们了!
呵呵……
Knight94 2003-04-08
  • 打赏
  • 举报
回复
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();
}
「已注销」 2003-04-08
  • 打赏
  • 举报
回复
参考这个
//---------------------------------------------
// 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));
}
}
usxue 2003-04-08
  • 打赏
  • 举报
回复
呵呵……
真的不好意思,我的帮助不能看啊!
怎么办啊??
能贴出来吗??
Knight94 2003-04-08
  • 打赏
  • 举报
回复
Microsoft Visual Studio .NET\FrameworkSDK\Samples\QuickStart\winforms\samples\gdiplus

本来不想给你建议用OnPaint事件做,但看看.net的例子,好像都用它来处理:-(
gogogo 2003-04-08
  • 打赏
  • 举报
回复
你知道怎么使用属性窗口的闪电按钮
usxue 2003-04-08
  • 打赏
  • 举报
回复
我已经在TextBox中加了事件了啊,我怎么加在paint的事件中呢??
急!!!
谢谢!!!
gogogo 2003-04-08
  • 打赏
  • 举报
回复
将你画图的过程转到picturebox的paint的事件中,在画图时强制刷新屏幕就可以了,用ArrayList
usxue 2003-04-08
  • 打赏
  • 举报
回复
有什么解决方案吗??
谢谢!
Knight94 2003-04-08
  • 打赏
  • 举报
回复
至于动态数组用ArrayList
Knight94 2003-04-08
  • 打赏
  • 举报
回复
因为你切换窗口时,窗体重画,那么你以前的画就被删去了。

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧