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 :

MouseWheel non récupéré


Sujet :

C#

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 5
    Par défaut MouseWheel non récupéré
    Bonjour,

    Je n'arrive pas à récupérer l'événement MouseWheel sur un panel. L'exécution ne passe pas dans le MouseWheel pourtant mon code me semble correct (abonnement, etc, ...). Il doit manquer quelque chose d'important mais je ne trouve pas quoi ???

    D'avance merci.

    Voici mon code :

    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
    namespace TESTS
    {
        partial class Form2
        {
            /// <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 Windows Form
     
            /// <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()
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.panel1.Location = new System.Drawing.Point(64, 86);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(374, 215);
                this.panel1.TabIndex = 0;
                this.panel1.Tag = true;
                this.panel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(panel1_MouseWheel);
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(515, 420);
                this.Controls.Add(this.panel1);
                this.Name = "Form2";
                this.Tag = true;
                this.Text = "Form2";
                this.ResumeLayout(false);
     
            }
     
     
            void panel1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                throw new System.Exception("The method or operation is not implemented.");
            }
     
            #endregion
     
            private System.Windows.Forms.Panel panel1;
        }
    }

  2. #2
    Expert confirmé

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2006
    Messages
    3 580
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Septembre 2006
    Messages : 3 580
    Par défaut
    salut

    une solution:

    Le code suivant fonctionne:

    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
    public partial class ExtPanel : Panel
        {
            const int WM_MOUSEWHEEL = 0x020A;
            public ExtPanel()
            {
                InitializeComponent();
                SetStyle(ControlStyles.Selectable, true);
                SetStyle(ControlStyles.UserMouse, true);
            }
            protected override void OnMouseWheel(MouseEventArgs e)
            {
                Trace.WriteLine("Mouse Wheel");
                base.OnMouseWheel(e);
            }
        }

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 5
    Par défaut
    Merci TheMonz21 pour ta réponse mais je souhaite rester dans un contexte d'abonnement aux événements.

    Microsoft donne un exemple basé sur le même principe que le mien (il utilise l'abonnement et n'utilise pas OnMouseWheel) et il fonctionne, mais je ne vois aucune vraie différence entre leur exemple et mon code (pourtant elle est forcément quelque part).
    Je cherche donc ce qui manque dans mon code pour que ça fonctionne.

    Code de Microsoft :

    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
    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
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    namespace MouseEvent
    {
        public class Form1 : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Panel panel1;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Label label4;
            private System.Windows.Forms.Label label5;
            private System.Windows.Forms.Label label6;
            private System.Windows.Forms.Label label7;
            private System.Windows.Forms.Label label8;
            private System.Windows.Forms.Label label9;
            private System.Windows.Forms.Button clearButton;
            private System.Drawing.Drawing2D.GraphicsPath mousePath;
            private System.Windows.Forms.GroupBox groupBox1;
     
            private int fontSize = 20;        
     
            [STAThread]
            static void Main() 
            {
                Application.Run(new Form1());
            }
     
            public Form1()
            {            
                mousePath = new System.Drawing.Drawing2D.GraphicsPath();
     
                this.panel1 = new System.Windows.Forms.Panel();
                this.label1 = new System.Windows.Forms.Label();
                this.clearButton = new System.Windows.Forms.Button();
                this.label2 = new System.Windows.Forms.Label();
                this.label3 = new System.Windows.Forms.Label();
                this.label4 = new System.Windows.Forms.Label();
                this.label5 = new System.Windows.Forms.Label();
                this.label6 = new System.Windows.Forms.Label();
                this.label7 = new System.Windows.Forms.Label();
                this.label8 = new System.Windows.Forms.Label();
                this.label9 = new System.Windows.Forms.Label();
                this.groupBox1 = new System.Windows.Forms.GroupBox();
     
                // Mouse Events Label
                this.label1.Location = new System.Drawing.Point(24, 504);
                this.label1.Size = new System.Drawing.Size(392, 23);
                // DoubleClickSize Label
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(24, 48);
                this.label2.Size = new System.Drawing.Size(35, 13);
                // DoubleClickTime Label
                this.label3.AutoSize = true;
                this.label3.Location = new System.Drawing.Point(24, 72);
                this.label3.Size = new System.Drawing.Size(35, 13);
                // MousePresent Label
                this.label4.AutoSize = true;
                this.label4.Location = new System.Drawing.Point(24, 96);
                this.label4.Size = new System.Drawing.Size(35, 13);
                // MouseButtons Label
                this.label5.AutoSize = true;
                this.label5.Location = new System.Drawing.Point(24, 120);
                this.label5.Size = new System.Drawing.Size(35, 13);
                // MouseButtonsSwapped Label
                this.label6.AutoSize = true;
                this.label6.Location = new System.Drawing.Point(320, 48);
                this.label6.Size = new System.Drawing.Size(35, 13);
                // MouseWheelPresent Label
                this.label7.AutoSize = true;
                this.label7.Location = new System.Drawing.Point(320, 72);
                this.label7.Size = new System.Drawing.Size(35, 13);
                // MouseWheelScrollLines Label
                this.label8.AutoSize = true;
                this.label8.Location = new System.Drawing.Point(320, 96);
                this.label8.Size = new System.Drawing.Size(35, 13);
                // NativeMouseWheelSupport Label
                this.label9.AutoSize = true;
                this.label9.Location = new System.Drawing.Point(320, 120);
                this.label9.Size = new System.Drawing.Size(35, 13);
     
                // Mouse Panel
                this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                    | System.Windows.Forms.AnchorStyles.Right);
                this.panel1.BackColor = System.Drawing.SystemColors.ControlDark;
                this.panel1.Location = new System.Drawing.Point(16, 160);
                this.panel1.Size = new System.Drawing.Size(664, 320);
                this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp);
                this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
                this.panel1.MouseEnter += new System.EventHandler(this.panel1_MouseEnter);
                this.panel1.MouseHover += new System.EventHandler(this.panel1_MouseHover);
                this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
                this.panel1.MouseLeave += new System.EventHandler(this.panel1_MouseLeave);
                this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
                this.panel1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseWheel);
     
                // Clear Button
                this.clearButton.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
                this.clearButton.Location = new System.Drawing.Point(592, 504);
                this.clearButton.TabIndex = 1;
                this.clearButton.Text = "Clear";
                this.clearButton.Click += new System.EventHandler(this.clearButton_Click);
     
                // GroupBox
                this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                    | System.Windows.Forms.AnchorStyles.Right);
                this.groupBox1.Location = new System.Drawing.Point(16, 24);
                this.groupBox1.Size = new System.Drawing.Size(664, 128);
                this.groupBox1.Text = "System.Windows.Forms.SystemInformation";
     
                // Set up how the form should be displayed and add the controls to the form.
                this.ClientSize = new System.Drawing.Size(696, 534);
                this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                            this.label9,this.label8,this.label7,this.label6,
                                            this.label5,this.label4,this.label3,this.label2,
                                            this.clearButton,this.panel1,this.label1,this.groupBox1});
                this.Text = "Mouse Event Example";
     
                // Displays information about the system mouse.
                label2.Text = "SystemInformation.DoubleClickSize: " + SystemInformation.DoubleClickSize.ToString();
                label3.Text = "SystemInformation.DoubleClickTime: " + SystemInformation.DoubleClickTime.ToString();
                label4.Text = "SystemInformation.MousePresent: " + SystemInformation.MousePresent.ToString();
                label5.Text = "SystemInformation.MouseButtons: " + SystemInformation.MouseButtons.ToString();
                label6.Text = "SystemInformation.MouseButtonsSwapped: " + SystemInformation.MouseButtonsSwapped.ToString();
                label7.Text = "SystemInformation.MouseWheelPresent: " + SystemInformation.MouseWheelPresent.ToString();
                label8.Text = "SystemInformation.MouseWheelScrollLines: " + SystemInformation.MouseWheelScrollLines.ToString();
                label9.Text = "SystemInformation.NativeMouseWheelSupport: " + SystemInformation.NativeMouseWheelSupport.ToString();
     
            }
     
            private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) 
            {
                // Update the mouse path with the mouse information
                Point mouseDownLocation = new Point(e.X, e.Y);
     
                string eventString = null;
                switch (e.Button) {
                    case MouseButtons.Left:
                        eventString = "L";
                        break;
                    case MouseButtons.Right:
                        eventString = "R";
                        break;
                    case MouseButtons.Middle:
                        eventString = "M";
                        break;
                    case MouseButtons.XButton1:
                        eventString = "X1";
                        break;
                    case MouseButtons.XButton2:
                        eventString = "X2";
                        break;
                    case MouseButtons.None:
                    default:
                        break;
                }
     
                if (eventString != null) 
                {
                    mousePath.AddString(eventString, FontFamily.GenericSerif, (int)FontStyle.Bold, fontSize, mouseDownLocation, StringFormat.GenericDefault);
                }
                else 
                {
                    mousePath.AddLine(mouseDownLocation,mouseDownLocation);
                }
                panel1.Focus();
                panel1.Invalidate();
            }
     
            private void panel1_MouseEnter(object sender, System.EventArgs e) 
            {
                // Update the mouse event label to indicate the MouseEnter event occurred.
                label1.Text = sender.GetType().ToString() + ": MouseEnter";
            }
     
            private void panel1_MouseHover(object sender, System.EventArgs e) 
            {
                // Update the mouse event label to indicate the MouseHover event occurred.
                label1.Text = sender.GetType().ToString() + ": MouseHover";
            }
     
            private void panel1_MouseLeave(object sender, System.EventArgs e) 
            {
                // Update the mouse event label to indicate the MouseLeave event occurred.
                label1.Text = sender.GetType().ToString() + ": MouseLeave";
            }
     
            private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                // Update the mouse path that is drawn onto the Panel.
                int mouseX = e.X;
                int mouseY = e.Y;
     
                mousePath.AddLine(mouseX,mouseY,mouseX,mouseY);
            }
     
            private void panel1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                // Update the drawing based upon the mouse wheel scrolling.
     
                int numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
                int numberOfPixelsToMove = numberOfTextLinesToMove * fontSize;
     
                if (numberOfPixelsToMove != 0) {
                    System.Drawing.Drawing2D.Matrix translateMatrix = new  System.Drawing.Drawing2D.Matrix();
                    translateMatrix.Translate(0, numberOfPixelsToMove);
                    mousePath.Transform(translateMatrix);
                }
                panel1.Invalidate();
            }
            private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
            {
                Point mouseUpLocation = new System.Drawing.Point(e.X, e.Y);
     
                // Show the number of clicks in the path graphic.
                int numberOfClicks = e.Clicks;
                mousePath.AddString("    " + numberOfClicks.ToString(), 
                            FontFamily.GenericSerif, (int)FontStyle.Bold, 
                            fontSize, mouseUpLocation, StringFormat.GenericDefault);
     
                panel1.Invalidate();
            }
     
            private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
            {    
                // Perform the painting of the Panel.
                e.Graphics.DrawPath(System.Drawing.Pens.DarkRed, mousePath);
            }
     
            private void clearButton_Click(object sender, System.EventArgs e)
            {
                // Clear the Panel display.
                mousePath.Dispose();
                mousePath = new System.Drawing.Drawing2D.GraphicsPath();
                panel1.Invalidate();
            }
        }
    }

  4. #4
    Expert confirmé Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Par défaut
    Bien que l'event MouseWheel soit défini pour un Panel, je pense que cet event ne sera jamais activé car le Panel ne peut pas recevoir le Focus.

  5. #5
    Expert confirmé

    Homme Profil pro
    Chef de projet NTIC
    Inscrit en
    Septembre 2006
    Messages
    3 580
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Septembre 2006
    Messages : 3 580
    Par défaut
    Je t'ai donné une solution... et elle répond à ton besoin...

    Le Panel ne gère pas le Mousewheel (c'est pour cela qu'on ne le voit pas dans les Event exposés par la fenetre de propriété).

    L'event existe par défaut car le Panel hérite d'un control qui possède le MouseWheel mais sans ScrollBar, pas de mouseWheel...

    A toi de voir si avoir une solution est quelque chose de concevable ou si tu préfères chercher autre chose "qui n'existe" à priori pas...

    Au pire, tu te crées ton propre panel qui intègre ma solution et qui expose le MouseWheel..

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Février 2011
    Messages
    5
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2011
    Messages : 5
    Par défaut
    Merci pour ces informations.

    Je viens de trouver la solution à mon problème : comme dit Graffito , il faut que le Panel ait le focus (il peut l'avoir en mettant par exemple dans le code de MonPanel_MouseEnter la ligne MonPanel.Pocus() ) pour qu'il réagisse au MouseWheel (avec abonnement) et c'est bon.

    Merci pour votre aide,
    Thierry

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

Discussions similaires

  1. [EJB] NullPointerException, EJB non récupéré
    Par AlexX54 dans le forum Java EE
    Réponses: 8
    Dernier message: 11/03/2011, 14h25
  2. Valeurs du fichier de config non récupérées
    Par grinder59 dans le forum C#
    Réponses: 1
    Dernier message: 30/01/2009, 18h07
  3. donnée non récupérée
    Par *alexandre* dans le forum JSF
    Réponses: 9
    Dernier message: 07/11/2008, 21h40
  4. Texte d'un textarea non récupéré
    Par philippef dans le forum Langage
    Réponses: 4
    Dernier message: 02/07/2008, 14h47
  5. Données de formulaire non récupérées
    Par Christophe P. dans le forum Struts 1
    Réponses: 5
    Dernier message: 02/09/2006, 11h33

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