Probleme d'interception des evenements

Résolu
pigeon95efrei Messages postés 9 Date d'inscription mardi 11 mai 2004 Statut Membre Dernière intervention 7 novembre 2007 - 26 mars 2007 à 01:22
pigeon95efrei Messages postés 9 Date d'inscription mardi 11 mai 2004 Statut Membre Dernière intervention 7 novembre 2007 - 27 mars 2007 à 12:17
Bonjour j'ai un probleme avec wndproc pour intercepter les evenements. Quand j'ai un formulaire vierge, tout fonctionne niquel!

Mais quand je lui ajoute un bouton par exemple. Plus rien ne fonctionne. Je comprends pas.

Voilà ma source;

using System;
using System.Drawing;
using System.Windows.Forms;

namespace csTempWindowsApplication1
{
    public class Form1 : System.Windows.Forms.Form
    {
       
        private int iChar;
        private System.Windows.Forms.Button button1;

        // Constant value was found in the "windows.h" header file.
       
        private const int WM_KEYDOWN = 0x0100;

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
       
        public Form1()
        {
            InitializeComponent();
            this.Size = new System.Drawing.Size(300,300);
            this.Text = "Form1";
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        }

       

        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(48, 48);
            this.button1.Name = "button1";
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click_1);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.ResumeLayout(false);

        }

       
        protected override void WndProc(ref Message m)
        {
           
            if (m.Msg != WM_KEYDOWN)
            {
                base.WndProc(ref m);
            }
            else
            {MessageBox.Show("1");
                iChar = m.WParam.ToInt32();
                switch(iChar)
                {
                    case (int)Keys.D0:
                        this.button1.Text="1";
                        break;

                    case (int)Keys.D1:
                        MessageBox.Show("1");
                        break;

                    case (int)Keys.D2:
                        MessageBox.Show("2");
                        break;
                    default:
                        //Make sure that you pass unhandled messages back to the default message handler.
                        base.WndProc(ref m);
                        break;
                   
                }
            }
           
       
        }

        private void button1_Click_1(object sender, System.EventArgs e)
        {
            MessageBox.Show("ok");
        }
    }
}

3 réponses

Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
26 mars 2007 à 11:51
Pour un simple WM_KEYDOWN un this.KeyPreview =  true devrait suffir.

Ca aurait été sympa de répondre ici, vu que ça t'a fait avancer dans ton code j'ai l'impression.

http://www.csharpfr.com/infomsg_PROBLEME-AVEC-WNDPROC_909660.aspx#1
3
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
26 mars 2007 à 10:20
voila qui devrait répondre à ta question :
http://www.csharpfr.com/codes/IMESSAGEFILTER-EVENEMENTS-MOUSEMOVE-MOUSEENTER-MOUSELEAVE-NIVEAU-CONTROLE-SANS_35443.aspx

Sébastien FERRAND (blog)
Consultant Indépendant
[Microsoft Visual C# MVP]
0
pigeon95efrei Messages postés 9 Date d'inscription mardi 11 mai 2004 Statut Membre Dernière intervention 7 novembre 2007
27 mars 2007 à 12:17
Ok merci pour votre aide. Ca m'a permis de résoudre le pb
0
Rejoignez-nous