c# 里的picture 如何保留已经画过得东西?

xdgavin 2009-05-02 01:09:05
本人初学, picturebox 已经画好了东西, 但是当最小化, 最大化, 窗口切换以后, 里面的东西就没有了. 请问如何才能让 c# 里的picturebox在做过以上的操作以后还可以保留已经画过得东西? 谢谢了.比如:
///////////////////// Form1.cs////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
g = this.pictureBox1.CreateGraphics();

}
Graphics g;
private void button1_Click(object sender, EventArgs e)
{
Pen myPen = new Pen(Color.Red);

g.DrawLine(myPen, 0, 0, 200, 100);
g.TranslateTransform(100, 100);
g.DrawLine(myPen, 0, 0, 200, 100);
}

private void button2_Click(object sender, EventArgs e)
{
Pen myPen = new Pen(Color.Green);
g.DrawLine(myPen, new Point(), new Point(100));
}
}
}
/////////////////////////////////Form1.Designer.cs
namespace WindowsFormsApplication4
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button2 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(158, 436);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.SystemColors.WindowText;
this.pictureBox1.Location = new System.Drawing.Point(47, 49);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(527, 339);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(313, 436);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(623, 508);
this.Controls.Add(this.button2);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button2;
}
}

/////////////////////////////////Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
...全文
167 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgke 2009-05-05
  • 打赏
  • 举报
回复
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Pen myPen = new Pen(Color.Red);

e.Graphics.DrawLine(myPen, 0, 0, 200, 100);
e.Graphics.TranslateTransform(100, 100);
e.Graphics.DrawLine(myPen, 0, 0, 200, 100);
}



给PICTUREBOX添加 Paint事件
xdgavin 2009-05-05
  • 打赏
  • 举报
回复
没人帮忙么?
LemIST 2009-05-04
  • 打赏
  • 举报
回复
button事件用来记录用户的操作
paint事件里画图
xdgavin 2009-05-04
  • 打赏
  • 举报
回复
picturebox的paint事件代码是:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{

}

双击button1的代码是:

private void button1_Click(object sender, EventArgs e)
{
Pen myPen = new Pen(Color.Red);

g.DrawLine(myPen, 0, 0, 200, 100);
g.TranslateTransform(100, 100);
g.DrawLine(myPen, 0, 0, 200, 100);
}

请问, 如何如何把, button_click 里的 专递给pictureBox1_Paint呢? 那如果有第二个button, button2怎么办呢?
蓝海D鱼 2009-05-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 coconutyf 的回复:]
窗体和其他控件的绘制都是在自身的OnPaint中实现的,屏幕某个区域的显示发生变化时,操作系统会通知控件重绘该区域,而你的的绘制代码是写在button_click事件里的,屏幕发生变化时操作系统是不会通知button需要click一下的,因此别人都重绘了,你期望的绘制并不会发生。

把绘制代码放到picturebox的Pint事件中试一下就明白了。
[/Quote]up
coconutyf 2009-05-03
  • 打赏
  • 举报
回复
窗体和其他控件的绘制都是在自身的OnPaint中实现的,屏幕某个区域的显示发生变化时,操作系统会通知控件重绘该区域,而你的的绘制代码是写在button_click事件里的,屏幕发生变化时操作系统是不会通知button需要click一下的,因此别人都重绘了,你期望的绘制并不会发生。

把绘制代码放到picturebox的Pint事件中试一下就明白了。
xdgavin 2009-05-03
  • 打赏
  • 举报
回复
靠人不如靠自己
xdgavin 2009-05-02
  • 打赏
  • 举报
回复
基础差才到这里来问, 如果不差, 就不用问了. 但是如果别人问了, 我不会说别人基础差.
光宇广贞 2009-05-02
  • 打赏
  • 举报
回复
我明白了一个事情。
你有没有想过为什么一变化就没有显示了?

其实不用最大化最小化,你改变一下窗体大小,就没了。
不是吗?

是因为你把图是PAINT上去的。
你还要先了解一下控件的PAINT事件。
窗体大小的改变会触发控件的PAINT事件。

所以一REPAINT,你原来的东西自然就没有了。

你要做的有两件事:
一,重写控件的PAINT方法,让每次PAINT都把你的图片给重新PAINT上去。但性能上……
二,你把图片读成IMAGE对像,然后赋给picture控件的image属性或者backgroundimage,大概是叫这名儿的属性。

基础太差……
xdgavin 2009-05-02
  • 打赏
  • 举报
回复
没有人帮忙么?
xdgavin 2009-05-02
  • 打赏
  • 举报
回复
什么导入项目? 请明示
sushou2009 2009-05-02
  • 打赏
  • 举报
回复
导入项目~

111,126

社区成员

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

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

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