| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3463 人关注过本帖
标题:关于GDI+的问题。我的paint 事件哪里错了呢?为什么最小化以后,所绘的图像 ...
取消只看楼主 加入收藏
jinghuayuanl
Rank: 1
来 自:河南洛阳
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-12-18
结帖率:50%
收藏
 问题点数:0 回复次数:2 
关于GDI+的问题。我的paint 事件哪里错了呢?为什么最小化以后,所绘的图像还是消失了?
程序还没有写完。为什么每次最小化以后,以前所绘的图像都消失了,是paint事件错了吗?
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections;

namespace shixun
{

    public partial class Form1 : Form
    {
        public int n;           //图形形状
        Point p1 = Point.Empty, p2 = Point.Empty;
        Color A=Color.Black, B;             //线条颜色,填充颜色
        bool moused = false;    //记下鼠标是否按下
        public ArrayList array = new ArrayList();    //重绘
        public int Xt=1;          //线条粗细
        int k;


        public Form1()
        {
            InitializeComponent();
        }
        private void radioButLine_CheckedChanged(object sender, EventArgs e)
        {
            n = 1;
        }

        private void radioButRecetangle_CheckedChanged(object sender, EventArgs e)
        {
            n = 2;
        }

        private void radioButEllipse_CheckedChanged(object sender, EventArgs e)
        {
            n = 3;
        }

        private void radioButRece_CheckedChanged(object sender, EventArgs e)
        {
            n = 4;
        }

        private void radioButEllip_CheckedChanged(object sender, EventArgs e)
        {
            n = 5;
        }


        private void 线条颜色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog Cdh = new ColorDialog();
            Cdh.ShowDialog();
            A = Cdh.Color;

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            switch (n)
            {
                case 1:
                    if (moused)
                    {
                        Graphics g = this.pictureBox1.CreateGraphics();
                        Pen pen = new Pen(Color.White, Xt);
                        g.DrawLine(pen, p1, p2);                  //清除前一根直线
                        p2.X = e.X; p2.Y = e.Y;
                        pen = new Pen(A, 1);
                        g.DrawLine(pen, p1, p2);
                        g.Dispose();
                    }
                    break;
                case 2:
                    if (moused)
                    {
                        Graphics g1 = this.pictureBox1.CreateGraphics();
                        Pen pen1 = new Pen(Color.White, Xt);
                        g1.DrawRectangle(pen1, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
                        p2.X = e.X; p2.Y = e.Y;
                        float b, c;
                        b = p2.X - p1.X;
                        c = p2.Y - p1.Y;
                        Pen pen2 = new Pen(A, 1);
                        g1.DrawRectangle(pen2, p1.X, p1.Y, b, c);
                        g1.Dispose();
                    }
                    break;
                case 3:
                    if (moused)
                    {
                        Graphics g1 = this.pictureBox1.CreateGraphics();
                        Pen pen3 = new Pen(Color.White, Xt);
                        g1.DrawRectangle(pen3, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
                        p2.X = e.X; p2.Y = e.Y;
                        float b, c;
                        b = p2.X - p1.X;
                        c = p2.Y - p1.Y;
                        Pen pen4 = new Pen(A, Xt);
                        g1.DrawRectangle(pen4, p1.X, p1.Y, b, c);
                        g1.Dispose();
                    }
                    break;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Cline ob = new Cline();
            Pen pen = new Pen(A);
            pen.Width = Xt;
            Graphics g = this.pictureBox1.CreateGraphics();
            switch (n)
            {
                case 1:
                    p2.X = e.X;
                    p2.Y = e.Y;
                    g.DrawLine(pen, p1, p2);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 1;
                    array.Add(ob);
                    break;
                case 2:
                    p2.X = e.X; p2.Y = e.Y;
                    float Rectangleb, Rectanglec;
                    Rectangleb = p2.X - p1.X;
                    Rectanglec = p2.Y - p1.Y;
                    g.DrawRectangle(pen, p1.X, p1.Y, Rectangleb, Rectanglec);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 2;
                    array.Add(ob);
                    break;
                case 3:
                    p2.X = e.X; p2.Y = e.Y;
                    float Roundb, Roundc;
                    Roundb = p2.X - p1.X;
                    Roundc = p2.Y - p1.Y;
                    g.DrawEllipse(pen, p1.X, p1.Y, Roundb, Roundc);
                    moused = false;
                    ob.x1 = p1.X; ob.y1 = p1.Y; ob.x2 = p2.X; ob.y2 = p2.Y; ob.s = pen.Width; ob.c = A; ob.k = 3;
                    array.Add(ob);
                    break;
            }  
        }



        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            p1.X = e.X;
            p1.Y = e.Y;
            moused = true;
        }

        private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            array.Clear();
            this.Invalidate();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.pictureBox1.CreateGraphics();
            Cline ob;
            for (int i = 0; i < array.Count; i++)
            {

                ob = (Cline)array[i];
                Pen pen = new Pen(ob.c);
                k = ob.k;
                pen.Width = ob.s;
                if (k == 1)
                {
                    g.DrawLine(pen, new Point(ob.x1, ob.y1), new Point(ob.x2, ob.y2));
                }
                if (k == 2)
                {
                    g.DrawRectangle(pen,
                    ob.x1, ob.y1,
                    ob.x2 - ob.x1, ob.y2 - ob.y1);
                }
                if (k == 3)
                {
                    g.DrawEllipse(pen,
                    ob.x1, ob.y1,
                   ob.x2 - ob.x1, ob.y2 - ob.y1);
                }
            }
        }



    }
    [Serializable()]
    public class Cline                       //记录所绘图形
    {
        public Color c;
        public float s;
        public int k;
        public int x1;
        public int y1;
        public int x2;
        public int y2;
    }
}
搜索更多相关主题的帖子: Point 
2010-12-26 11:17
jinghuayuanl
Rank: 1
来 自:河南洛阳
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-12-18
收藏
得分:0 
回复 2楼 wangnannan
GDI+.rar (41.77 KB)
2010-12-26 12:23
jinghuayuanl
Rank: 1
来 自:河南洛阳
等 级:新手上路
帖 子:21
专家分:0
注 册:2010-12-18
收藏
得分:0 
回复 6楼 c1_wangyf
为什么我一加那个pictureBox1.Refresh();语句,直线怎么就画不上了呢?(代码和你的一样)如果删除后,最小化以后,还是直线没有了。
还有我把那个绘图换到form1上,然后,然后在private void Form1_Paint(object sender, PaintEventArgs e)中加入picturebox1_paint事件中同样的代码,结果最小化以后图像还是会在。
为什么换到picturebox1中,就不行了呢?
2010-12-27 12:55
快速回复:关于GDI+的问题。我的paint 事件哪里错了呢?为什么最小化以后,所绘的 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034190 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved