Précédent   Forum du club des développeurs et IT Pro > Dotnet > Développement Windows > Windows Forms
Windows Forms Forum d'entraide sur le développement Windows Forms & Applications Windows. Avant de poster -> Articles Windows Forms
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse
 
Outils de la discussion
Publicité
'
Vieux 30/12/2012, 16h49   #1
genius4evers
Membre éclairé
 
Homme Fernando HIJAZI
Ingénieur de déploiement réseaux
Inscription : janvier 2011
Messages : 354
Détails du profil
Informations personnelles :
Nom : Homme Fernando HIJAZI
Localisation : France

Informations professionnelles :
Activité : Ingénieur de déploiement réseaux
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : janvier 2011
Messages : 354
Points : 325
Points : 325
Par défaut Deplacer un cercle ?

Salut.

je suis entraine de faire un deplacement du cercle dans le windows-form ,
je connais comment peut on dessiner le cercle avec la fonction graphics

Code :
1
2
3
 
Graphics g;
g.DrawEllipse(....,...,...,...);
comment peut faire bouger les cercles d'une facone aleatoire ?

merci en avance
genius4evers est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 30/12/2012, 23h40   #2
genius4evers
Membre éclairé
 
Homme Fernando HIJAZI
Ingénieur de déploiement réseaux
Inscription : janvier 2011
Messages : 354
Détails du profil
Informations personnelles :
Nom : Homme Fernando HIJAZI
Localisation : France

Informations professionnelles :
Activité : Ingénieur de déploiement réseaux
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : janvier 2011
Messages : 354
Points : 325
Points : 325
Plus precisement je veux creer un cercle dans une panel a l'aide d'une fonction aleatoire sur x (on commence toujours du point le plus haut) c.a.d meme y , mais apres on fait deplacer sur y seulement.
merci.
genius4evers est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 31/12/2012, 10h39   #3
sisqo60
Membre Expert
 
Avatar de sisqo60
 
Homme
Consultant informatique
Inscription : février 2006
Messages : 731
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Indre et Loire (Centre)

Informations professionnelles :
Activité : Consultant informatique

Informations forums :
Inscription : février 2006
Messages : 731
Points : 1 150
Points : 1 150
Bonjour,

Il faut que tu stockes l'emplacement de ton ellipse. Exemple :

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
 
private Random _alea = new Random();
private int _directionX = 1;
private int _directionY = 1;
public int PositionX {get;private set;}
public int PositionY {get;private set;}
public Pen MonStylo {get; private set;}
private void DrawEllipseInt(PaintEventArgs e)
{
    // Create or use pen.
    MonStylo = MonStylo ?? new Pen(Color.Black, 3);
 
    PositionY += _alea.Next(1, 10) * _directionY;
    // Si on dépace les limites du panel, on change la direction de Y pour rester à l'écran
    if (PositionY > (MyPanel.Location.Y + MyPanel.Height) || PositionY < 0)
        _directionY *= -1;
    PositionX += _alea.Next(1, 10) * _directionX;
    // Si on dépace les limites du panel, on change la direction de X pour rester à l'écran
    if (PositionX > (MyPanel.Location.X + MyPanel.Width) || PositionY < 0)
        _directionY *= -1;
 
 
    // Draw ellipse to screen.
    e.Graphics.DrawEllipse(MonStylo, PositionX, PositionY, 100, 100);
}
C'est quelque chose de ce genre, mais attention à ne pas dépacer la zone de ton panel. Je t'ai fait un petit bout de code sans tester et sans Visual Studio.

Bon dév.
__________________
Un âne se croit savant parce qu'on le charge de livres (proverbe américain)

N'oubliez pas de avant de
Pas de question techniques par MP, c'est contre la philosophie du forum
sisqo60 est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 31/12/2012, 22h20   #4
genius4evers
Membre éclairé
 
Homme Fernando HIJAZI
Ingénieur de déploiement réseaux
Inscription : janvier 2011
Messages : 354
Détails du profil
Informations personnelles :
Nom : Homme Fernando HIJAZI
Localisation : France

Informations professionnelles :
Activité : Ingénieur de déploiement réseaux
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : janvier 2011
Messages : 354
Points : 325
Points : 325
Bonjour
je vous remerci pour votre aide .
si je veux mettre des nombres par example 3-5 ou 4+8 ,.... comme le raindrop jeux , comment peux-t-on les remplir dans le cercle ?

Merci.
genius4evers est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 01/01/2013, 12h59   #5
sisqo60
Membre Expert
 
Avatar de sisqo60
 
Homme
Consultant informatique
Inscription : février 2006
Messages : 731
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Indre et Loire (Centre)

Informations professionnelles :
Activité : Consultant informatique

Informations forums :
Inscription : février 2006
Messages : 731
Points : 1 150
Points : 1 150
Et bien il va falloir faire des calculs. Tu sais où tu traces ta courbe, il faut que tu calcules l'emplacement de tes opérandes et de ton opérateur pour qu'ils restent dans ta boule, ou faire un composant qui t'abstrairas de cette complexité, mais il faut le coder et ce n'est pas compliqué...

Bon dév.
__________________
Un âne se croit savant parce qu'on le charge de livres (proverbe américain)

N'oubliez pas de avant de
Pas de question techniques par MP, c'est contre la philosophie du forum
sisqo60 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 01/01/2013, 21h59   #6
genius4evers
Membre éclairé
 
Homme Fernando HIJAZI
Ingénieur de déploiement réseaux
Inscription : janvier 2011
Messages : 354
Détails du profil
Informations personnelles :
Nom : Homme Fernando HIJAZI
Localisation : France

Informations professionnelles :
Activité : Ingénieur de déploiement réseaux
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : janvier 2011
Messages : 354
Points : 325
Points : 325
Bonjour
j'ai essaye a faire ce code
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
 
public Pen MonStylo { get; private set; }    
 private void button1_Click(object sender, EventArgs e)
        {
            string text = "1+1";
            Bitmap bm = new Bitmap(50, 50);
            MonStylo = MonStylo ?? new Pen(Color.Black, 3);
            using (Font font = new Font("Comic Sans MS", 12, GraphicsUnit.Point))
            using (Graphics g = panel1.CreateGraphics())
            {
 
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                StringFormat stringFormat = new StringFormat()
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Near
                };
 
                Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height);
                // measure how large the text is on the Graphics object with the current font size
                SizeF s = g.MeasureString(text, font);
                // calculate how to scale the font to make the text fit
                float fontScale = Math.Max(s.Width / rect.Width, s.Height / rect.Height);
                using (Font fontForDrawing = new Font(font.FontFamily, font.SizeInPoints / fontScale, GraphicsUnit.Point))
                {
                    g.DrawString(text, fontForDrawing, Brushes.Black, 15,15);                    
                    g.DrawEllipse(MonStylo, 5, 5, 100, 100);
 
                }
dans ce cas on obtient "1+1" dans le cercle , mon question : comment peut-faire "1+1" et le cercle comme un seul objet pour les deplacer ? ou comment peut-on deplacer le cercle avec les nombres ?

Merci.
Bonne soiree
genius4evers est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 02/01/2013, 21h26   #7
sisqo60
Membre Expert
 
Avatar de sisqo60
 
Homme
Consultant informatique
Inscription : février 2006
Messages : 731
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Indre et Loire (Centre)

Informations professionnelles :
Activité : Consultant informatique

Informations forums :
Inscription : février 2006
Messages : 731
Points : 1 150
Points : 1 150
Citation:
Envoyé par sisqo60 Voir le message
[...] faire un composant qui t'abstrairas de cette complexité [...]
Je me répète, tu peux faire un composant (Un composant est une classe "graphique" qui hérite de Control), en surchargeant la méthode OnPaint, tu peux lui définir une apparence personnalisée. Ce control peut être arrondi (regarde du côté des régions). Comme c'est une classe, tu peux lui ajouter des méthodes et des propriétés...

Avec ces éléments, tu as tous les moyens d'y arriver

Bon dév.
__________________
Un âne se croit savant parce qu'on le charge de livres (proverbe américain)

N'oubliez pas de avant de
Pas de question techniques par MP, c'est contre la philosophie du forum
sisqo60 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/01/2013, 22h49   #8
MABROUKI
Membre Expert
 
mebarek
Inscription : avril 2008
Messages : 1 041
Détails du profil
Informations personnelles :
Nom : mebarek
Âge : 53

Informations forums :
Inscription : avril 2008
Messages : 1 041
Points : 1 542
Points : 1 542
bonjour genius4evers

Comme dit par sisqo60,il faut "abstraire" ton objet qui se deplace,rebondit sur les bords du panel.....autrement dit le "factoriser" ....
Le meilleur candidat dans un tel cas est un "custom control"....

voici un customshape control qui pourrait repondre à ton souci:

code .cs du customshape :
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
 
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;
using System.Drawing.Text;
 
namespace WinCustomShape
{
    public partial class CustomShape : Control
    {
        private Pen MonStylo; 
        public CustomShape()
        {
            InitializeComponent();
            text = "1+1";
            this.Location = new Point(10, 10);
            this.Size = new Size(110, 110);
            this.ColorStylo = Color.Black;
            this.SetStyle(
                ControlStyles.AllPaintingInWmPaint
                | ControlStyles.UserPaint
                | ControlStyles.OptimizedDoubleBuffer 
                | ControlStyles.ResizeRedraw, true);
        }
 
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Bitmap bm = new Bitmap(50, 50);
            MonStylo =  new Pen(ColorStylo, 3);
            Font font = new Font("Comic Sans MS", 12, GraphicsUnit.Point);
            Graphics g = pe.Graphics;
 
 
            g.TextRenderingHint = TextRenderingHint.AntiAlias;
            StringFormat stringFormat = new StringFormat()
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Near
            };
 
            Rectangle rect = new Rectangle(0, 0, bm.Width, bm.Height);
            // measure how large the text is on the Graphics object with the current font size
            SizeF s = g.MeasureString(text, font);
            // calculate how to scale the font to make the text fit
            float fontScale = Math.Max(s.Width / rect.Width, s.Height / rect.Height);
            using (Font fontForDrawing = new Font(font.FontFamily, font.SizeInPoints / fontScale, GraphicsUnit.Point))
            {
                g.DrawString(text, fontForDrawing, Brushes.Black, 15,15);
                g.DrawEllipse(MonStylo, 5, 5, 100, 100);
 
            }
            MonStylo.Dispose(); 
            bm.Dispose();
            font.Dispose();
 
 
        }
 
 
        [Browsable (true)]
        [Category ("Cercle")]
        public Color  ColorStylo { get;  set; }
 
        [Browsable(true)]
        [Category("Cercle")]
        public string text { get;  set; }
 
 
    }
}
code du winform qui l'utilise:
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
 
//1 ajouter un timer sur le form
//2 generer tout le projet
//3 executer.................
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 WinCustomShape
{
    public partial class Form1 : Form
    {
        private Random _alea = new Random();
        private int _directionX = 1;
        private int _directionY = 1;
        private int PositionX = 0;
        private int PositionY = 0;
        private CustomShape shp;
 
        public Form1()
        {
            InitializeComponent();
 
        }
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
             if (shp == null)
            {
                shp = new CustomShape();
                PositionX = _alea.Next(1, 10);
                PositionY = _alea.Next(1, 10); 
                shp.Location = new Point(PositionX, PositionY);
                this.Controls.Add(shp);
                this.timer1.Interval = 200; 
                this.timer1.Enabled = true;
                this.timer1.Start();
 
            }
            else
             {
                 shp = null;
                 this.timer1.Enabled = false;
 
             }
 
        }
 
 
 
        private void timer1_Tick(object sender, EventArgs e)
        {
 
 
            MoveShape();
 
        }
 
        private void MoveShape()
        {
            shp.BackColor = Color.Blue;
 
            // Si on dépace les limites du panel, on change la direction de Y pour rester à l'écran
            shp.Top += _alea.Next(1, 10) * _directionY;
            if (shp.Top + shp.Height > this.Height || shp.Top < 0)
                _directionY *= -1;
 
 
 
            // Si on dépace les limites du panel, on change la direction de X pour rester à l'écran
            shp.Left += _alea.Next(1, 10) * _directionX;
            if (shp.Left + shp.Width > this.Width || shp.Left < 0)
                _directionX *= -1;
 
 
        }
 
 
    }
}
bon code..........
MABROUKI est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 07/01/2013, 08h48   #9
sisqo60
Membre Expert
 
Avatar de sisqo60
 
Homme
Consultant informatique
Inscription : février 2006
Messages : 731
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : France, Indre et Loire (Centre)

Informations professionnelles :
Activité : Consultant informatique

Informations forums :
Inscription : février 2006
Messages : 731
Points : 1 150
Points : 1 150
Bonjour,

J'ai également réalisé le composant (custom control) pour m'amuser mais je gardais le code sous la main pour plus tard, si tu avais encore des difficultés, mais ma version. Il s'appelle boule

Code du Boule.Designer.cs :
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
    partial class Boule
    {
        /// <summary>
        /// Variable nécessaire au concepteur.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary> 
        /// Nettoyage des ressources utilisées.
        /// </summary>
        /// <param name="disposing">true si les ressources managées doivent être supprimées*; sinon, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Code généré par le Concepteur de composants
 
        /// <summary>
        /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
        /// le contenu de cette méthode avec l'éditeur de code.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }
 
        #endregion
    }
Code du custom control (Boule.cs):
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
    public partial class Boule : Control
    {
        public Boule()
        {
            InitializeComponent();
        }
 
        public Boule(IContainer container)
        {
            container.Add(this);
 
            InitializeComponent();
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
 
            System.Drawing.Drawing2D.GraphicsPath maForme =
                            new System.Drawing.Drawing2D.GraphicsPath();
 
            System.Drawing.Rectangle newRectangle = this.ClientRectangle;
 
 
            using (Pen fond = new Pen(this.BackColor))
            {
                e.Graphics.DrawEllipse(fond, newRectangle);
            }
            using (Brush b = new LinearGradientBrush(new Point(0, 0), new Point(Height, Width), BackColor, Color.White))
            {
                e.Graphics.FillEllipse(b, this.ClientRectangle);
            }
            using (Brush stylo = new SolidBrush(this.ForeColor))
            {
                SizeF taille = e.Graphics.MeasureString(this.Text, this.Font);
                e.Graphics.DrawString(this.Text, this.Font, stylo, new RectangleF(new PointF((Width - taille.Width) / 2, (Height - taille.Height) / 2), taille));
            }
 
            maForme.AddEllipse(newRectangle);
 
            Region = new System.Drawing.Region(maForme);
        }
 
        [Localizable(true), Bindable(true)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                if (this.Text != value)
                {
                    base.Text = value;
                    this.Invalidate();
                }
            }
        }
    }
Voila
__________________
Un âne se croit savant parce qu'on le charge de livres (proverbe américain)

N'oubliez pas de avant de
Pas de question techniques par MP, c'est contre la philosophie du forum
sisqo60 est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 07/01/2013, 19h32   #10
genius4evers
Membre éclairé
 
Homme Fernando HIJAZI
Ingénieur de déploiement réseaux
Inscription : janvier 2011
Messages : 354
Détails du profil
Informations personnelles :
Nom : Homme Fernando HIJAZI
Localisation : France

Informations professionnelles :
Activité : Ingénieur de déploiement réseaux
Secteur : High Tech - Opérateur de télécommunications

Informations forums :
Inscription : janvier 2011
Messages : 354
Points : 325
Points : 325
Merci
Bonne soiree
genius4evers est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Cette discussion est résolue.
Outils de la discussion

Navigation rapide


Fuseau horaire GMT +2. Il est actuellement 16h13.


 
 
 
 
Partenaires

Hébergement Web