Laden...

Meine erstes graphisches Entwicklertool

Letzter Beitrag vor 17 Jahren 20 Posts 7.899 Views
Meine erstes graphisches Entwicklertool

Hallo,

ich habe das
[Tutorial] Zeichnen in Windows-Programmen (Paint/OnPaint, PictureBox) mir angeschaut und habe mir eine Appplikation geschrieben.

Das Porgramm hat ein paar Macken/Probleme, wieso zeichnet das Programm die Punkte und die Linien auf das PictureBox sehr langsam, die mit den MouseEvents ausgeführt werden? Wie kann ich die Verzögerung beheben?

Lösung: Doublebuffer = true setzen in oPictureBox 🙂

Hier der Code von mir:



/////////////////////////////////////////////////////////////////
// Die abgeleiteten Klassen  /Form1.Designer.cs
/////////////////////////////////////////////////////////////////

namespace mcGraphic
{
    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.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.slblX = new System.Windows.Forms.ToolStripStatusLabel();
            this.slblY = new System.Windows.Forms.ToolStripStatusLabel();
            this.cmbFadenkreuz = new System.Windows.Forms.ComboBox();
            this.lblFadenkreuz = new System.Windows.Forms.Label();
            this.lblGraphicBackColor = new System.Windows.Forms.Label();
            this.cmbPbGraphic = new System.Windows.Forms.ComboBox();
            this.gBoxColor = new System.Windows.Forms.GroupBox();
            this.pbGreen = new System.Windows.Forms.PictureBox();
            this.pbYellow = new System.Windows.Forms.PictureBox();
            this.pbBlue = new System.Windows.Forms.PictureBox();
            this.pbRed = new System.Windows.Forms.PictureBox();
            this.gBoxObject = new System.Windows.Forms.GroupBox();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.txtX = new System.Windows.Forms.TextBox();
            this.txtY = new System.Windows.Forms.TextBox();
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.tsDrawLine = new System.Windows.Forms.ToolStripButton();
            this.oButton1 = new mcGraphic.graphic.oButton();
            this.pbGraphic = new mcGraphic.graphic.oPictureBox();
            this.statusStrip1.SuspendLayout();
            this.gBoxColor.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pbGreen)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbYellow)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbBlue)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbRed)).BeginInit();
            this.gBoxObject.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.toolStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pbGraphic)).BeginInit();
            this.SuspendLayout();
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.slblX,
            this.slblY});
            this.statusStrip1.Location = new System.Drawing.Point(0, 544);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(792, 22);
            this.statusStrip1.TabIndex = 1;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // slblX
            // 
            this.slblX.Name = "slblX";
            this.slblX.Size = new System.Drawing.Size(32, 17);
            this.slblX.Text = "X:     ";
            // 
            // slblY
            // 
            this.slblY.Name = "slblY";
            this.slblY.Size = new System.Drawing.Size(32, 17);
            this.slblY.Text = "Y:     ";
            // 
            // cmbFadenkreuz
            // 
            this.cmbFadenkreuz.FormattingEnabled = true;
            this.cmbFadenkreuz.Location = new System.Drawing.Point(623, 24);
            this.cmbFadenkreuz.Name = "cmbFadenkreuz";
            this.cmbFadenkreuz.Size = new System.Drawing.Size(121, 21);
            this.cmbFadenkreuz.TabIndex = 2;
            this.cmbFadenkreuz.SelectedIndexChanged += new System.EventHandler(this.cmbFadenkreuz_SelectedIndexChanged);
            // 
            // lblFadenkreuz
            // 
            this.lblFadenkreuz.AutoSize = true;
            this.lblFadenkreuz.Location = new System.Drawing.Point(623, 8);
            this.lblFadenkreuz.Name = "lblFadenkreuz";
            this.lblFadenkreuz.Size = new System.Drawing.Size(63, 13);
            this.lblFadenkreuz.TabIndex = 3;
            this.lblFadenkreuz.Text = "Fadenkreuz";
            // 
            // lblGraphicBackColor
            // 
            this.lblGraphicBackColor.AutoSize = true;
            this.lblGraphicBackColor.Location = new System.Drawing.Point(623, 48);
            this.lblGraphicBackColor.Name = "lblGraphicBackColor";
            this.lblGraphicBackColor.Size = new System.Drawing.Size(95, 13);
            this.lblGraphicBackColor.TabIndex = 4;
            this.lblGraphicBackColor.Text = "Graphic Backcolor";
            // 
            // cmbPbGraphic
            // 
            this.cmbPbGraphic.FormattingEnabled = true;
            this.cmbPbGraphic.Location = new System.Drawing.Point(623, 64);
            this.cmbPbGraphic.Name = "cmbPbGraphic";
            this.cmbPbGraphic.Size = new System.Drawing.Size(121, 21);
            this.cmbPbGraphic.TabIndex = 5;
            this.cmbPbGraphic.SelectedIndexChanged += new System.EventHandler(this.cmbPbGraphic_SelectedIndexChanged);
            // 
            // gBoxColor
            // 
            this.gBoxColor.Controls.Add(this.pbGreen);
            this.gBoxColor.Controls.Add(this.pbYellow);
            this.gBoxColor.Controls.Add(this.pbBlue);
            this.gBoxColor.Controls.Add(this.pbRed);
            this.gBoxColor.Location = new System.Drawing.Point(623, 91);
            this.gBoxColor.Name = "gBoxColor";
            this.gBoxColor.Size = new System.Drawing.Size(121, 85);
            this.gBoxColor.TabIndex = 6;
            this.gBoxColor.TabStop = false;
            // 
            // pbGreen
            // 
            this.pbGreen.BackColor = System.Drawing.Color.Green;
            this.pbGreen.Location = new System.Drawing.Point(61, 12);
            this.pbGreen.Name = "pbGreen";
            this.pbGreen.Size = new System.Drawing.Size(20, 20);
            this.pbGreen.TabIndex = 3;
            this.pbGreen.TabStop = false;
            // 
            // pbYellow
            // 
            this.pbYellow.BackColor = System.Drawing.Color.Yellow;
            this.pbYellow.Location = new System.Drawing.Point(89, 12);
            this.pbYellow.Name = "pbYellow";
            this.pbYellow.Size = new System.Drawing.Size(20, 20);
            this.pbYellow.TabIndex = 2;
            this.pbYellow.TabStop = false;
            // 
            // pbBlue
            // 
            this.pbBlue.BackColor = System.Drawing.Color.Blue;
            this.pbBlue.Location = new System.Drawing.Point(5, 12);
            this.pbBlue.Name = "pbBlue";
            this.pbBlue.Size = new System.Drawing.Size(20, 20);
            this.pbBlue.TabIndex = 1;
            this.pbBlue.TabStop = false;
            this.pbBlue.Click += new System.EventHandler(this.pbBlue_Click);
            // 
            // pbRed
            // 
            this.pbRed.BackColor = System.Drawing.Color.Red;
            this.pbRed.Location = new System.Drawing.Point(33, 12);
            this.pbRed.Name = "pbRed";
            this.pbRed.Size = new System.Drawing.Size(20, 20);
            this.pbRed.TabIndex = 0;
            this.pbRed.TabStop = false;
            this.pbRed.Click += new System.EventHandler(this.pbRed_Click);
            // 
            // gBoxObject
            // 
            this.gBoxObject.Controls.Add(this.pictureBox1);
            this.gBoxObject.Location = new System.Drawing.Point(623, 182);
            this.gBoxObject.Name = "gBoxObject";
            this.gBoxObject.Size = new System.Drawing.Size(121, 79);
            this.gBoxObject.TabIndex = 7;
            this.gBoxObject.TabStop = false;
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlDark;
            this.pictureBox1.Location = new System.Drawing.Point(6, 13);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(22, 21);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // txtX
            // 
            this.txtX.Location = new System.Drawing.Point(623, 342);
            this.txtX.Name = "txtX";
            this.txtX.Size = new System.Drawing.Size(100, 20);
            this.txtX.TabIndex = 8;
            this.txtX.Text = "20";
            // 
            // txtY
            // 
            this.txtY.Location = new System.Drawing.Point(623, 368);
            this.txtY.Name = "txtY";
            this.txtY.Size = new System.Drawing.Size(100, 20);
            this.txtY.TabIndex = 9;
            this.txtY.Text = "20";
            // 
            // toolStrip1
            // 
            this.toolStrip1.Dock = System.Windows.Forms.DockStyle.Left;
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsDrawLine});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(24, 544);
            this.toolStrip1.TabIndex = 11;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // tsDrawLine
            // 
            this.tsDrawLine.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            this.tsDrawLine.Image = ((System.Drawing.Image)(resources.GetObject("tsDrawLine.Image")));
            this.tsDrawLine.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.tsDrawLine.Name = "tsDrawLine";
            this.tsDrawLine.Size = new System.Drawing.Size(29, 20);
            this.tsDrawLine.Text = "tsDrawLine";
            this.tsDrawLine.Click += new System.EventHandler(this.tsDrawLine_Click);
            // 
            // oButton1
            // 
            this.oButton1.Location = new System.Drawing.Point(626, 411);
            this.oButton1.Name = "oButton1";
            this.oButton1.Size = new System.Drawing.Size(115, 97);
            this.oButton1.TabIndex = 10;
            this.oButton1.Text = "button1";
            this.oButton1.UseVisualStyleBackColor = true;
            this.oButton1.MouseLeave += new System.EventHandler(this.button1_MouseLeave);
            this.oButton1.Click += new System.EventHandler(this.button1_Click);
            this.oButton1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.button1_MouseMove);
            // 
            // pbGraphic
            // 
            this.pbGraphic.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pbGraphic.Location = new System.Drawing.Point(42, 24);
            this.pbGraphic.Name = "pbGraphic";
            this.pbGraphic.Size = new System.Drawing.Size(550, 506);
            this.pbGraphic.TabIndex = 0;
            this.pbGraphic.TabStop = false;
            this.pbGraphic.Click += new System.EventHandler(this.pbGraphic_Click);
            this.pbGraphic.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pbGraphic_MouseMove);
            this.pbGraphic.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pbGraphic_MouseClick);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(792, 566);
            this.Controls.Add(this.toolStrip1);
            this.Controls.Add(this.oButton1);
            this.Controls.Add(this.txtY);
            this.Controls.Add(this.txtX);
            this.Controls.Add(this.gBoxObject);
            this.Controls.Add(this.gBoxColor);
            this.Controls.Add(this.cmbPbGraphic);
            this.Controls.Add(this.lblGraphicBackColor);
            this.Controls.Add(this.lblFadenkreuz);
            this.Controls.Add(this.cmbFadenkreuz);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.pbGraphic);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "mcGraphic";
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.gBoxColor.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pbGreen)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbYellow)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbBlue)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbRed)).EndInit();
            this.gBoxObject.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pbGraphic)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private mcGraphic.graphic.oPictureBox pbGraphic;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.ToolStripStatusLabel slblX;
        private System.Windows.Forms.ToolStripStatusLabel slblY;
        private System.Windows.Forms.ComboBox cmbFadenkreuz;
        private System.Windows.Forms.Label lblFadenkreuz;
        private System.Windows.Forms.Label lblGraphicBackColor;
        private System.Windows.Forms.ComboBox cmbPbGraphic;
        private System.Windows.Forms.GroupBox gBoxColor;
        private System.Windows.Forms.PictureBox pbBlue;
        private System.Windows.Forms.PictureBox pbRed;
        private System.Windows.Forms.PictureBox pbGreen;
        private System.Windows.Forms.PictureBox pbYellow;
        private System.Windows.Forms.GroupBox gBoxObject;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.TextBox txtX;
        private System.Windows.Forms.TextBox txtY;
        private mcGraphic.graphic.oButton oButton1;
        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.ToolStripButton tsDrawLine;
    }
}




/////////////////////////////////////////////////////////////////
// Die abgeleiteten Klassen  /Form1.cs
/////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using mcGraphic.graphic;

namespace mcGraphic
{
    public partial class Form1 : Form
    {
        const int iWidth = 800;
        const int iHeight = 640;

        private Point MousePoint;
        private Point MousePoint1;
        private Point MousePoint2;

        int countDrawLines;
        int x1 = 0;
        int x2 = 0;
        int y1 = 0;
        int y2 = 0;

        string[] colorNames;
        string[] colorNames2;

        #region Pinsel & Stifte Farben

        // Roter Pinsel
        Brush brushRed = new SolidBrush(Color.Red);
        // Blauer Pinsel
        Brush brushSkyblue = new SolidBrush(Color.SkyBlue);
        // Pinsel Violet
        Brush brushBlue = new SolidBrush(Color.Blue);
        // Pinsel Blue
        Brush brushViolet = new SolidBrush(Color.Violet);
        // Roter Stift
        Pen penRed = new Pen(Color.Black,3);
        // Schwarzer Stift
        Pen penBlack = new Pen(Color.Black,3);
        // Stift in SkyBlue
        Pen penSkyBlue = new Pen(Color.SkyBlue,3);
        
        #endregion

        List<cGraphicObject> listGraphicObject = new List<cGraphicObject>();

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            foreach (cGraphicObject go in listGraphicObject)
            {
                go.Draw(e.Graphics);
            }
        }

        public Form1()
        {
            InitializeComponent();
            
            Invalidate();

            ClientSize = new Size(iWidth, iHeight);
            DoubleBuffered = true;
            

            DrawGitter();

            colorNames = System.Enum.GetNames(typeof(KnownColor));
            colorNames2 = System.Enum.GetNames(typeof(System.Drawing.KnownColor));
            cmbPbGraphic.Items.AddRange(colorNames);
            cmbFadenkreuz.Items.AddRange(colorNames2);

            countDrawLines = 0;
        }

        private void DrawGitter()
        {
            if (txtX.Text != null || txtY.Text != null)
            {
                int xAbstand = Convert.ToInt16(txtX.Text);
                int yAbstand = Convert.ToInt16(txtY.Text);

                for (int x = 0; x <= 100; ++x)
                {
                    for (int y = 0; y <= 100; ++y)
                    { 

                        pbGraphic.listGraphicObject.Add(new cDrawLine(penBlack, x * xAbstand, y * yAbstand , x * xAbstand, y * yAbstand));
                
                    }
                }

                pbGraphic.Refresh();
            }
            Invalidate();
        }

        private void pbGraphic_MouseMove(object sender, MouseEventArgs e)
        {
            slblX.Text = "X: " + e.X.ToString();
            slblY.Text = "Y: " + e.Y.ToString();
            MousePoint = new Point(e.X, e.Y);
            DrawLines(MousePoint, Color.FromName(cmbFadenkreuz.Text));
            
        }

        private void DrawLines(Point p, Color c)
        {
            Pen pen = new Pen(c);
            Point startDia1 = new Point(p.X-7, p.Y-7);
            Point endDia1 = new Point(p.X+7, p.Y+7);
            Point startDia2 = new Point(p.X-7, p.Y+7);
            Point endDia2 = new Point(p.X+7, p.Y-7);

            // Fadenkreuz zeichnen
            pbGraphic.listGraphicObject.Add(new cDrawLine(pen, startDia1, endDia1));
            pbGraphic.listGraphicObject.Add(new cDrawLine(pen, startDia2, endDia2));

            Point startHor2 = new Point(p.X - 10, p.Y);
            Point endHor2 = new Point(p.X + 10, p.Y);
            Point startVert2 = new Point(p.X, p.Y + 10);
            Point endVert2 = new Point(p.X, p.Y - 10);

            // Fadenkreuz zeichnen
            pbGraphic.listGraphicObject.Add(new cDrawLine(pen, startHor2, endHor2));
            pbGraphic.listGraphicObject.Add(new cDrawLine(pen, startVert2, endVert2));

            int iX = Convert.ToInt16(p.X);
            int iY = Convert.ToInt16(p.Y);
            pbGraphic.Refresh();
        }

        private void cmbPbGraphic_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbPbGraphic.Text = cmbPbGraphic.BackColor.ToString();
            int sel = cmbPbGraphic.SelectedIndex;
            string itemSel = cmbPbGraphic.Items[sel].ToString();
            pbGraphic.BackColor = Color.FromName(itemSel);
            
            if (itemSel == "Transparent")
            {
                cmbPbGraphic.BackColor = Color.White;
            }
            else
            {
                cmbPbGraphic.BackColor = Color.FromName(itemSel);
            }
        }

        private void cmbFadenkreuz_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbFadenkreuz.Text = cmbFadenkreuz.BackColor.ToString();
            int sel = cmbFadenkreuz.SelectedIndex;
            string itemSel = cmbFadenkreuz.Items[sel].ToString();
            if (itemSel == "Transparent")
            {
            }
            else
            {
                cmbFadenkreuz.BackColor = Color.FromName(itemSel);
            }
        }

        private void pbGraphic_MouseClick(object sender, MouseEventArgs e)
        {
            slblX.Text = "X: " + e.X.ToString();
            slblY.Text = "Y: " + e.Y.ToString();

            if (countDrawLines == 0)
            {
                MousePoint1 = new Point(e.X, e.Y);
                countDrawLines = 1;
            }
            else if (countDrawLines == 1)
            {
                MousePoint2 = new Point(e.X, e.Y);
                countDrawLines = 2;
            }

            if (tsDrawLine.Checked == true && countDrawLines == 2)
            {
                // Linie zeichnen
                pbGraphic.listGraphicObject.Add(new cDrawLine(penSkyBlue, MousePoint1, MousePoint2));
                pbGraphic.Refresh();
                countDrawLines = 0;
            
            }
            else if (tsDrawLine.Checked == false)
            {
                MousePoint = new Point(e.X, e.Y);
                DrawPoint(MousePoint, Color.Red, 0);
                countDrawLines = 0;
            }

        }

        private void DrawPoint(Point p, Color c, Int16 s)
        {
            Pen pen = new Pen(c);

            Point startDia1 = new Point(p.X - 1, p.Y  - 1);
            Point endDia1 = new Point(p.X + 1, p.Y + 1);
            Point startDia2 = new Point(p.X - 1, p.Y + 1);
            Point endDia2 = new Point(p.X + 1, p.Y -1);

            pbGraphic.listGraphicObject.Add(new cDrawLine(pen, startDia1, endDia1));
            pbGraphic.listGraphicObject.Add(new cDrawLine(pen, startDia2, endDia2));

            Point startHor2 = new Point(p.X - 2, p.Y);
            Point endHor2 = new Point(p.X + 2, p.Y);
            Point startVert2 = new Point(p.X, p.Y + 2);
            Point endVert2 = new Point(p.X, p.Y - 2);

            pbGraphic.listGraphicObject.Add(new cDrawLine(pen, startHor2, endHor2));
            pbGraphic.listGraphicObject.Add(new cDrawLine(pen, startVert2, endVert2));
            pbGraphic.Refresh();
        }

        private void pbRed_Click(object sender, EventArgs e)
        {
            // Rechteck füllen
            pbGraphic.listGraphicObject.Add(new cFillRectangle(brushRed, new Rectangle(50, 10, 70, 50)));
            //pbGraphic.listGraphicObject.Add(new cDrawRectangle(penBlack, new Rectangle(0, 0, 74, 49)));
            pbGraphic.Refresh();
        }

        private void pbBlue_Click(object sender, EventArgs e)
        {
            // Rechteck füllen
            pbGraphic.listGraphicObject.Add(new cFillRectangle(brushBlue, new Rectangle(150, 110, 50, 50)));
            //pbGraphic.listGraphicObject.Add(new cDrawRectangle(penBlack, new Rectangle(0, 0, 74, 49)));
            //pbGraphic.BackColor = Color.Bisque;
            pbGraphic.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DrawGitter();
        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            oButton1.listGraphicObject.Add(new cFillRectangle(brushRed, new Rectangle(0, 0, oButton1.Width, oButton1.Height)));
            oButton1.listGraphicObject.Add(new cDrawString("Mitte", brushSkyblue, new Font("Verdana", 14), oButton1.Width / 4, oButton1.Height/4));
            
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            oButton1.listGraphicObject.Add(new cFillRectangle(brushViolet, new Rectangle(0, 0, oButton1.Width, oButton1.Height)));
            oButton1.listGraphicObject.Add(new cDrawString("Weg", new SolidBrush(Color.Black), new Font("Comic Sans", 16), 5, 1));
        }

        private void pbGraphic_Click(object sender, EventArgs e)
        {

        }

        private void tsDrawLine_Click(object sender, EventArgs e)
        {
            if (tsDrawLine.Checked == false && tsDrawLine.Pressed == true)
            {
                tsDrawLine.Checked = true;
            }
            else if (tsDrawLine.Checked == true && tsDrawLine.Pressed == true)
            {
                tsDrawLine.Checked = false;
            }
        }
    }
}


/////////////////////////////////////////////////////////////////
// Die abgeleiteten Klassen  /graphic/cGraphicObject.cs
/////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace myGraphic.graphic
{
    public class cDrawLine : cGraphicObject
    {
        protected Pen _pen;
        private Point _pt1;
        private Point _pt2;
       
        public cDrawLine(Pen pen, int x1, int y1, int x2, int y2)
            : base(pen)
        {
            _pt1 = new Point(x1, y1);
            _pt2 = new Point(x2, y2);
            _pen = pen;
        }

        public cDrawLine(Pen pen, Point pt1, Point pt2)
            : base(pen)
        {
            _pt1 = pt1;
            _pt2 = pt2;
            _pen = pen;
        }

        public override void Draw(Graphics g)
        {
            g.DrawLine(_pen, _pt1, _pt2);
        }
    }

    public class cDrawRectangle : cGraphicObject
    {
        protected Pen _pen;
        private Rectangle _rect;

        public cDrawRectangle(Pen pen, Rectangle rect)
            : base(pen)
        {
            _rect = rect;
            _pen = pen;
        }

        public override void Draw(Graphics g)
        {
            g.DrawRectangle(_pen, _rect);
        }
    }

    public class cFillRectangle : cGraphicObject
    {
        private Brush _brush;
        private Rectangle _rect;

        public cFillRectangle(Brush brush, Rectangle rect)
            : base(brush)
        {
            _rect = rect;
            _brush = brush;
        }

        public override void Draw(Graphics g)
        {
            g.FillRectangle(_brush, _rect);
        }
    }

    class cDrawString : cGraphicObject
    {
        private string _string;
        private Brush _brush;
        private Font _font;
        private Point _pt;
       
        public cDrawString(String string1, Brush brush1, Font font1, int x1, int y1)
            : base(brush1)
        {
            _string = string1;
            _brush = brush1;
            _font = font1;
            _pt = new Point(x1, y1);
        }

        public override void Draw(Graphics g)
        {
            g.DrawString(_string, _font, _brush,  _pt );
        }
    }

    public abstract class cGraphicObject
    {
        protected Pen _pen;
        protected Brush _brush;

        public cGraphicObject(Pen pen)
        {
            _pen = pen;
        }

        public cGraphicObject(Brush brush)
        {
            _brush = brush;
        }

        public abstract void Draw(Graphics g);
    }

    #region class oButton
    class oButton : Button
    {
        public List<cGraphicObject> listGraphicObject = new List<cGraphicObject>();

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            foreach (cGraphicObject go in listGraphicObject)
            {
                go.Draw(e.Graphics);
                
            }
        }

        public oButton()
        {
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |
            ControlStyles.DoubleBuffer, true);
        }
    }
    #endregion

    class oPictureBox : PictureBox
    {
        public List<cGraphicObject> listGraphicObject = new List<cGraphicObject>();

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            foreach (cGraphicObject go in listGraphicObject)
            {
                go.Draw(e.Graphics);
            }
        }
        
        public oPictureBox()
        {
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |
            ControlStyles.DoubleBuffer, true);
        }
    }

}

Gruß Steel

1. Googlen 2. Boardsuche benutzen 3. Überlegen 4. Posten

Ich sage es nur äußerst ungerne,aber darf man in Foren/wo auch immer eine klitzekleine Frage stellen,ohne dass gleich ein Oberlehrer mit der obligatorischen "Google suchen"-Antwort kommt?

Hallo SteelWarrior,

schön das du eine Lösung gefunden und auch hier geschrieben hast. Sowas freut! Noch besser ist es, die Lösung als Antwort zu posten, damit der Thread nicht mehr in den Themen ohne Antwort auftaucht. Ist aber durch diesen Beitrag auch erledigt. 🙂

herbivore

Hallo herbivore!

Ja klar mache ich das.
Ich bastele an einem graphischen Entwicklertool, es soll meinen eignen Bedürfnissen angepasst werden.

Was soll das graphische Entwicklertool können:

  • Graphische Elemente auf dem Picturebox zeichnen
  • Positionen änderen können mit Mouseevents,
  • Daten in XML Dateien speichern und laden
  • Projekte in einer Treeview anzeigen
  • ...

wenn du oder jemand Ideen dazu hat, bitte posten

Gruß Steel

1. Googlen 2. Boardsuche benutzen 3. Überlegen 4. Posten

Ich sage es nur äußerst ungerne,aber darf man in Foren/wo auch immer eine klitzekleine Frage stellen,ohne dass gleich ein Oberlehrer mit der obligatorischen "Google suchen"-Antwort kommt?

Für mich klingt das alles sehr nach Eclipse + RCP + EMF, ohne mich jetzt im Speziellen mit diesen Dingen beschäftigt zu haben. Aber Du bist ja hier, weil Du das mit C# machen willst und nicht mit Java nehme ich an.

Sarkusmus ist, wenn nichts mehr hilft, außer Lachen.

Das Programm soll und wird in CSharp programmiert werden.

aus Wikipedia.de: Eclipse ist ein Open-Source-Framework zur Entwicklung von Rich-Client-Applikationen. Die bekannteste Verwendung ist die Nutzung als Entwicklungsumgebung (IDE), diese umfaßt unter der Bezeichnung Eclipse SDK die Eclipse Platform, Werkzeuge zur Java-Entwicklung (Java Development Tools JDT) und die Umgebung zur Entwicklung von Eclipse-Plugins (Plug-in Development Environment PDE). Andere bekannte Software auf Basis des Eclipse-Frameworks ist beispielsweise der IBM Workplace Client als Weiterentwicklung von Lotus Notes.

zu RCP UND EMF kann ich nichts sagen, kenne ich nicht.

1. Googlen 2. Boardsuche benutzen 3. Überlegen 4. Posten

Ich sage es nur äußerst ungerne,aber darf man in Foren/wo auch immer eine klitzekleine Frage stellen,ohne dass gleich ein Oberlehrer mit der obligatorischen "Google suchen"-Antwort kommt?

Hallo SteelWarrior,

in Zeiten von google, wikipedia und Co sollte es kein Problem sein, das herauszufinden.

herbivore

Ja, meine Aussage war mehr so gemeint, dass es ja nicht in Frage kommt, wenn Du auf jeden Fall C# einsetzen willst / musst, da Du beim Einsatz des Eclipse-Frameworks (nicht der Entwicklungsumgebung) ein Java-Programm schreiben müsstest.

Nur der Vollständigkeit halber:
RCP - Rich-Client-Platform
EMF - Eclipse Modelling Framework

Sarkusmus ist, wenn nichts mehr hilft, außer Lachen.

Lösung: Doublebuffer = true setzen in oPictureBox 🙂

Nö, aber sicher ein Performancegewinn.
Was soll das Ding im Allgemeinen leisten? Wenn's das war ist es ok(beim Aufziehen einer Figur mags flackern ohne Buffer, aber nicht langsam werden), soll ein komplexeres Ergebnis(Programmfeatures?) rauskommen, werden weitere Probleme hinzukommen(error by design).
Die Lösung kann also ok sein oder nicht.
Der Code lässt jedenfalls auf weitergehende Ambitionen schliessen, daher die Nachfrage(inkl. Gesülze).

Wieder was dazu gelernt!
Bevor ich mit der Auflistung der Todos des Programms anfange, werde ich noch warten müssen Gedanken hierfür zu Opfern, andere Projekte haben ihr Vorrang. Ich habe mehrere Projekte in der ich dieses hier anwenden werde 😉

Projektbeispiele:

  • 2D Animation von Gebäudeautomatisierung
  • 2D Animation von verschiedenen Möglichkeiten
  • 2D Animation von Spielen

3D wag ich mich noch nicht!

1. Googlen 2. Boardsuche benutzen 3. Überlegen 4. Posten

Ich sage es nur äußerst ungerne,aber darf man in Foren/wo auch immer eine klitzekleine Frage stellen,ohne dass gleich ein Oberlehrer mit der obligatorischen "Google suchen"-Antwort kommt?

Und wer endeckt den Fehler hier?
Ich selber habe es gefunden und oben im Quellcode entfernt 😉


...
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            foreach (cGraphicObject go in listGraphicObject)
            {
                go.Draw(e.Graphics);
            }
        }
...
    public class cFillRectangle : cGraphicObject
    {
        private Brush _brush;
        private Rectangle _rect;

        public cFillRectangle(Brush brush, Rectangle rect)
            : base(brush)
        {
            _rect = rect;
            _brush = brush;
        }

        public override void Draw(Graphics g)
        {
            g.FillRectangle(_brush, _rect);
            g.Dispose();
        }
    }
...

1. Googlen 2. Boardsuche benutzen 3. Überlegen 4. Posten

Ich sage es nur äußerst ungerne,aber darf man in Foren/wo auch immer eine klitzekleine Frage stellen,ohne dass gleich ein Oberlehrer mit der obligatorischen "Google suchen"-Antwort kommt?

Hallo SteelWarrior

wenn du so fragst, g.Dipose. Zerstöre nie ein Objekt, dass du nicht selbst erzeugt hast oder für das dir der Erzeuger nicht explizit die Verantwortung übertragen hat.

herbivore

Treffer, herbivore.
Ich habe angefangen die MSDN WebCast zu GDI+ anzuschauen und dadurch wurde mir einiges klar!

steel

1. Googlen 2. Boardsuche benutzen 3. Überlegen 4. Posten

Ich sage es nur äußerst ungerne,aber darf man in Foren/wo auch immer eine klitzekleine Frage stellen,ohne dass gleich ein Oberlehrer mit der obligatorischen "Google suchen"-Antwort kommt?

Zu dem Dispose():
Was ich mich schon oft gefragt habe ist, was passiert wenn ich z.B. einen Pen folgendermaßen erstelle?


grfx.DrawLines(new Pen(m_LineColor, m_r4LineWidth), points);

Dann kann ich den ja nicht disposen. Macht das was oder wird er von .Net weggeschmissen sobald das Zecihnen beendet ist?

Hallo jaensen,

(auch wenn diese Frage schon fast einen eigenen Thread wert wäre)

In Kurzform:
Der GC ruft (irgendwann) Dispose auf. "Manuelles disposen" hat den Vorteil, dass die Win32 - Komponenten (Handles) früher freigegeben werden. Das macht vor allem Sinn, wenn sehr viele Pens, etc. erzeugt werden. Aber auch so sollte man lieber den Pen in einem using-Block erstellen.

Das würde ich versuchen zu vermeiden. Man kann ja schreiben:


using (Pen pen = new Pen(m_LineColor, m_r4LineWidth))
{
  grfx.DrawLines(pen, points);
}

Dann wird die Dispose-Methode automatisch aufgerufen.
Wahrscheinlich isses aber gar nicht so schlimm, da ein Pen nur ein paar Byte im Hauptspeicher belegt, und beim Beenden des Programms eh disposed wird.
Wenn das allerdings in einer Schleife geschieht, kann keiner sagen, wie das ausgehen wird.

Weeks of programming can save you hours of planning

Danke, ich werde das dann wohl doch lieber mit using machen.

Hallo jaensen,

noch besser ist, wo immer es möglich ist, Pens und ähnliche Hilfsobjekte außerhalb des OnPaint/Paint zu erzeugen und in den Instanzvariablen zu halten.

herbivore

Hab es so schlampig gelöst weil ich jeweils (z.B. für einen Pen) eine Color und eine Width variable habe, weil man den Pen ansich ja nicht serialisieren kann (zumindest nicht mit [Serializable()]).

Hallo jaensen,

das eine schließt das andere nicht aus. Kennzeichne den Pen als [NonSerialized].

herbivore

Stimmt, das sollte ich machen da ich den Pen ja sowieso öfter benutze und ihn jedesmal neu zu erstellen sicherlich auch ein wenig rechenzeit kostet.