tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
4
ZUGRIFFE
854
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    10110010 10110010 ist offline Mitglied Bronze
    Registriert seit
    Nov 2005
    Beiträge
    42
    Hallo leute,

    Ich habe in meinem Project die OnPaint Methode überschrieben.

    Das problem ist nur, dass der dabei nie zum ende kommt, da er immer wieder von forne beginnt.

    MFG 01

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    
    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 Mandelbrot
    {
        public partial class Form1 : Form
        {
            private bool painted = false;
            private const int MaxIterations = 50;
     
            //Region
            private const float Min_X = -2.2F;
            private const float Max_X = 1F;
            private const float Min_Y = -1.2F;
            private const float Max_Y = 1.2F;
            
            //Startpunkt
            private float X_Min = Min_X;
            private float X_Max = Max_X;
            private float Y_Min = Min_Y;
            private float Y_Max = Max_Y;
     
            public Form1()
            {
                InitializeComponent();
            }
     
            protected override void OnPaint(PaintEventArgs e)
            {
                if (!painted)
                {
                    base.OnPaint(e);
                    painted = true;
     
                    const float Max_Squared = 2.0F;
     
                    GetProportion();
     
                    int count;
                    float XC, YC, dXC, dYC, XZ, YZ, XZ2, YZ2;
     
                    int wi = this.Width;
                    int hi = this.Height;
                    dXC = (X_Max - X_Min) / (wi - 1);
                    dYC = (Y_Max - Y_Min) / (hi - 1);
     
                    XC = X_Min;
                    for (float i = 0F; i <= wi - 1; i++)
                    {
                        YC = Y_Min;
                        for (float j = 0F; j <= hi - 1; j++)
                        {
                            XZ = 0;
                            YZ = 0;
                            XZ2 = 0;
                            YZ2 = 0;
                            count = 0;
                            while (count < MaxIterations && XZ < Max_Squared && YZ < Max_Squared)
                            {
                                XZ2 = XZ * XZ;
                                YZ2 = YZ * YZ;
                                YZ = 2 * YZ * XZ + YC;
                                XZ = XZ2 - YZ2 + XC;
                                count++;
                            }
                            //Color C = Color.FromArgb(count, count, count);
                            //Pen P=new Pen(C);
                            Pen P = new Pen(Color.Black);
                            if (!(count < MaxIterations))
                            {
                                e.Graphics.DrawLine(P, i, j, i + 0.1F, j + 0.1F);
                            }
     
                            YC = YC + dYC;
                        }
                        XC = XC + dXC;
                    }
                    painted = false;
                }
            }
     
            private void GetProportion()
            {
                float Proportion, FormProportion;
                float hi, wi, mid;
     
                Proportion = (Y_Max - Y_Min) / (X_Max - X_Min);
                FormProportion = float.Parse(this.Height.ToString()) / float.Parse(this.Width.ToString());
                if (Proportion > FormProportion)
                {
                    wi = (Y_Max - Y_Min) / FormProportion;
                    mid = (X_Min + X_Max) / 2F;
                    X_Min = mid - wi / 2F;
                    X_Max = mid + wi / 2F;
                }
                else
                {
                    hi = (X_Max - X_Min) * FormProportion;
                    mid = (Y_Min + Y_Max) / 2F;
                    Y_Min = mid - hi / 2F;
                    Y_Max = mid + hi / 2F;
                }
            }
     
            private void Form1_Resize(object sender, EventArgs e)
            {
                this.Invalidate();
            }
        }
    }
     

  2. #2
    Ch Tutorials.de Gastzugang
    Hallo,

    du sagst beim Einstieg deiner Funktion " painted = true;" aber überschreibst es am Ende wieder mit " painted = false;" somit ist kein Abbruch möglich und "if (!painted)" ist immer gegeben. Ein Rückgabeparameter wäre nicht schlecht.

    Code :
    1
    2
    3
    
    protected override bool OnPaint(PaintEventArgs e)
     
    return painted;

    Gruss
     

  3. #3
    Avatar von Nico Graichen
    Nico Graichen Nico Graichen ist offline aka gemballa
    tutorials.de Moderator
    Registriert seit
    Dec 2003
    Ort
    Pulheim (NRW)
    Beiträge
    3.898
    Blog-Einträge
    34
    Hi

    Hab deinen Code grad kopiert. Das Programm funktioniert ohne Probleme. Der Code wird einmal ausgeführt, wenn kein weiteres Zeichnen von Nöten ist.

    Wie hast du denn festgestellt, das das Paint mehrfach ausgeführt wird?
     
    Grüße Nico
    ----------------------
    Xing
    ----------------------
    Zitat Zitat von Mark Twain (1835-1910)
    Es gibt drei Dinge, die eine Frau aus dem Nichts hervorzaubern kann: einen Hut, einen Salat und einen Ehekrach.
    Zitat Zitat von Mike Wilson - Biographie über Larry Ellison (CEO Oracle)
    The Difference Between God and Larry Ellison: God Doesn't Think He's Larry Ellison

  4. #4
    Avatar von Spyke
    Spyke Spyke ist offline Capoeirista
    Registriert seit
    Oct 2002
    Beiträge
    931
    Und die Variable painted ist sinnlos.

    Solltest du diese am Ende doch mal auf true setzen , verschwindet dein gezeichnes eh bei der nächsten Aufforderung zum neuzeichnen des Formulars.

    Wenn du diesen Zustand also aus irgend einem Grund halten musst, zeichne auf ein Bitmap.

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    Bitmap bmp=new Bitmap(width, height)
    using(Graphics g=Graphics.FromImage(bmp))
    {
       g.Draw(....); //zeichnen
     
       g.Flush(); //Zeichen Operationen ausführen
    }
     
    form.BackGroundImage=bmp;
     
    www.iv-interactive.de - Projektewebsite
    WikiParser - aktuelles Projekt

  5. #5
    10110010 10110010 ist offline Mitglied Bronze
    Registriert seit
    Nov 2005
    Beiträge
    42
    Hallo leute,

    habe das Painted herrausgenommen.

    Das Property "DoubleBuffered = true;" hat mir gehollfen, da das Bild erst berechnet wird befor es angezeigt bekommt.

    Hier der ganze Quellcode:

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    
    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 Mandelbrot
    {
        public partial class Form1 : Form
        {
            private const int MaxIterations = 255;
     
            //Region
            private const float Min_X = -2.2F;
            private const float Max_X = 1F;
            private const float Min_Y = -1.2F;
            private const float Max_Y = 1.2F;
            
            //Startpunkt
            private float X_Min = Min_X;
            private float X_Max = Max_X;
            private float Y_Min = Min_Y;
            private float Y_Max = Max_Y;
     
            public Form1()
            {
                DoubleBuffered = true;
                InitializeComponent();
            }
     
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
     
                const float Max_Squared = 2.0F;
     
                GetProportion();
     
                int count;
                float XC, YC, dXC, dYC, XZ, YZ, XZ2, YZ2;
     
                int wi = this.Width;
                int hi = this.Height;
                dXC = (X_Max - X_Min) / (wi - 1);
                dYC = (Y_Max - Y_Min) / (hi - 1);
     
                XC = X_Min;
                for (float i = 0F; i <= wi - 1; i++)
                {
                    YC = Y_Min;
                    for (float j = 0F; j <= hi - 1; j++)
                    {
                        XZ = 0;
                        YZ = 0;
                        XZ2 = 0;
                        YZ2 = 0;
                        count = 0;
                        while (count < MaxIterations && XZ < Max_Squared && YZ < Max_Squared)
                        {
                            XZ2 = XZ * XZ;
                            YZ2 = YZ * YZ;
                            YZ = 2F * YZ * XZ + YC;
                            XZ = XZ2 - YZ2 + XC;
                            count++;
                        }
                        Color C = Color.FromArgb(10, count, 10);
                        Pen P = new Pen(C);
                        //Pen P = new Pen(Color.Black);
                        //if (!(count < MaxIterations))
                        //{
                        e.Graphics.DrawLine(P, i, j, i + 0.1F, j + 0.1F);
                        //}
     
                        YC = YC + dYC;
                    }
                    XC = XC + dXC;
                }
            }
     
            private void GetProportion()
            {
                float Proportion, FormProportion;
                float hi, wi, mid;
     
                Proportion = (Y_Max - Y_Min) / (X_Max - X_Min);
                FormProportion = float.Parse(this.Height.ToString()) / float.Parse(this.Width.ToString());
                if (Proportion > FormProportion)
                {
                    wi = (Y_Max - Y_Min) / FormProportion;
                    mid = (X_Min + X_Max) / 2F;
                    X_Min = mid - wi / 2F;
                    X_Max = mid + wi / 2F;
                }
                else
                {
                    hi = (X_Max - X_Min) * FormProportion;
                    mid = (Y_Min + Y_Max) / 2F;
                    Y_Min = mid - hi / 2F;
                    Y_Max = mid + hi / 2F;
                }
            }
     
            private void Form1_Resize(object sender, EventArgs e)
            {
                this.Invalidate();
            }
        }
    }

    Aber wenn ich das Propperty herrausnehme kann er das nicht zuende Zeichnen.
    Das ist allerdings nur bei mir. Auf der Arbeit funktioniert alles perfect!

    MFG 01
     

Ähnliche Themen

  1. GDI+ Grafik / OnPaint
    Von TheShihan im Forum VisualStudio & MFC
    Antworten: 7
    Letzter Beitrag: 27.02.08, 10:04
  2. OnPaint event hook
    Von KainPlan im Forum C/C++
    Antworten: 5
    Letzter Beitrag: 26.02.08, 11:44
  3. BMPs anzeigen durch OnPaint()
    Von BlackD0G im Forum VisualStudio & MFC
    Antworten: 13
    Letzter Beitrag: 01.06.06, 16:47
  4. Bewegung in PaintBox/OnPaint auslösen?
    Von r_Alf im Forum Borland CBuilder und VCL
    Antworten: 2
    Letzter Beitrag: 10.04.06, 12:18
  5. OnPaint() selbst auslösen
    Von ensae im Forum VisualStudio & MFC
    Antworten: 3
    Letzter Beitrag: 27.02.04, 18:28