IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Problème pour dessiner un rectangle


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    197
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Septembre 2005
    Messages : 197
    Par défaut Problème pour dessiner un rectangle
    Bonjour,

    J'ai copié collé le code suivant de MSDN afin de dessiner un rectangle. Lorsque j'exécute le programme rien ne se passe lorsque j'appuie sur les boutons de la souris. J'ai fait également l'expérience sans événement de la souris et même résultats.

    Avez-vous une idée pourquoi?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    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 ShemaBlockBuilder
    {
        public partial class Form1 : Form
        {
     
            private Rectangle RcDraw;
            private float PenWidth = 5;
     
            private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
     
                // Determine the initial rectangle coordinates...
     
                RcDraw.X = e.X;
                RcDraw.Y = e.Y;
     
            }
     
            private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
            {
     
                // Determine the width and height of the rectangle...
     
                if (e.X < RcDraw.X)
                {
                    RcDraw.Width = RcDraw.X - e.X;
                    RcDraw.X = e.X;
                }
                else
                {
                    RcDraw.Width = e.X - RcDraw.X;
                }
     
                if (e.Y < RcDraw.Y)
                {
                    RcDraw.Height = RcDraw.Y - e.Y;
                    RcDraw.Y = e.Y;
                }
                else
                {
                    RcDraw.Height = e.Y - RcDraw.Y;
                }
     
                // Force a repaint of the region occupied by the rectangle...
     
                this.Invalidate(RcDraw);
     
            }
     
            private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
     
                // Draw the rectangle...
     
                e.Graphics.DrawRectangle(new Pen(Color.Blue, PenWidth), RcDraw);
     
            }
     
     
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
     
            }
        }
    }

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    Bonjour acheo.
    En C# n'oublie jamais de creer les handlers d'event quand tu fais du copier-coller de code car ils n'existent pas ce qui explique l'absence de reaction du form....

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    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 GrilleCSharp
    {
        public partial class Form4 : Form
        {
            private Rectangle RcDraw;
            private float PenWidth = 5;
            public Form4()
            {
                InitializeComponent();
                //ajoute les handlers du mouse
                this.MouseDown+=new MouseEventHandler(Form4_MouseDown);  
                this.MouseUp += new MouseEventHandler(Form4_MouseUp); 
            }
     
            private void Form4_Load(object sender, EventArgs e)
            {
     
            }
     
            private void Form4_MouseDown(object sender, MouseEventArgs e)
            {
                // Determine the initial rectangle coordinates...
     
                RcDraw.X = e.X;
                RcDraw.Y = e.Y;
            }
     
            private void Form4_MouseUp(object sender, MouseEventArgs e)
            {
                // Determine the width and height of the rectangle...
     
                if (e.X < RcDraw.X)
                {
                    RcDraw.Width = RcDraw.X - e.X;
                    RcDraw.X = e.X;
                }
                else
                {
                    RcDraw.Width = e.X - RcDraw.X;
                }
     
                if (e.Y < RcDraw.Y)
                {
                    RcDraw.Height = RcDraw.Y - e.Y;
                    RcDraw.Y = e.Y;
                }
                else
                {
                    RcDraw.Height = e.Y - RcDraw.Y;
                }
     
                // Force a repaint of the region occupied by the rectangle...
     
                this.Invalidate(RcDraw);
            }
     
            private void Form4_Paint(object sender, PaintEventArgs e)
            {
                // Draw the rectangle...
     
                e.Graphics.DrawRectangle(new Pen(Color.Blue, PenWidth), RcDraw);
     
     
            }
     
     
     
        }
    }
    bon code....................

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Septembre 2005
    Messages
    197
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Septembre 2005
    Messages : 197
    Par défaut
    merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Classe pour dessiner un rectangle
    Par sohafarhat dans le forum Débuter avec Java
    Réponses: 8
    Dernier message: 15/06/2009, 17h46
  2. Problème pour dessiner.
    Par Clydopathe dans le forum C#
    Réponses: 3
    Dernier message: 05/02/2009, 08h45
  3. Fonction pour dessiner un rectangle
    Par Kanjah dans le forum 2D
    Réponses: 3
    Dernier message: 03/02/2008, 22h19
  4. [Débutant] Problème pour dessiner une courbe
    Par torNAdE dans le forum MATLAB
    Réponses: 6
    Dernier message: 16/07/2007, 22h25
  5. Réponses: 7
    Dernier message: 04/06/2006, 17h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo