Evénement n'importe ou sur l'écran

hibouman Messages postés 13 Date d'inscription dimanche 1 juin 2003 Statut Membre Dernière intervention 21 juin 2006 - 24 mai 2006 à 09:50
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 - 26 mai 2006 à 11:08
Je débute en c# et j'aurais aimé quelque chose dans ce genre là:



quelque_soit_l'endroit_sur_l'écran.Click += new EventHandler(écran_Click);
...
static void  écran_Click(object sender, EventArgs e)
{
   MessageBox.Show("click sur l'écran !!");
}

Bref, qu'un message soit affiché quelque soit l'endroit ou l'on click sans même ne faire apparaitre une Form.

Merci !

7 réponses

cs_Killan Messages postés 46 Date d'inscription jeudi 10 janvier 2002 Statut Membre Dernière intervention 5 février 2010
24 mai 2006 à 10:15
N'y a t il pas une API windows qui permet de connaître l'état de la souris et de s'accrocher dessus avec des événements ? Du moins je chercherais là dedans.

Bien à toi,

Killan
www.daaboo.net
0
hibouman Messages postés 13 Date d'inscription dimanche 1 juin 2003 Statut Membre Dernière intervention 21 juin 2006
24 mai 2006 à 13:15
Merci.
Ca me parait bizarre quand même pour un langage aussi récent et puissant qu'on ne puisse pas se contenter de quelques lignes de codes et donc être obligé de passer par les API.
Mais bon, peut-être...
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
24 mai 2006 à 18:41
Salut, oui c'est même plus "lourd"que ça, faut carrément installer un hook, non pas que ce soit compliqué mais ça demande de connaître P/Invoke et surtout un hook mal écrit peut ralentir le système.

Regardes les fonctions Win32 :

SetWindowsHookEx
UnhookWindowsHookEx
CallNextHookEx

Si ton application, est une application multimedia, utilise DirectInput en mode background.

Si tu as besoin des coordonnées d'écran de la souris de manière très ponctuelle tu peux utiliser la méthode "très moche" du timer, il s'agit de tester la propriété Cursor.Postion dans un timer :(

Autre méthode assez élégante mais pas très connue c'est l'API RawInput, j'avais fait un wrapper managé mais je l'ai jamais terminé.. Je vais voir si je peux poster un snippet sur Codyx avant ce week-end.
0
hibouman Messages postés 13 Date d'inscription dimanche 1 juin 2003 Statut Membre Dernière intervention 21 juin 2006
24 mai 2006 à 23:35
Merci bien , je v tester tout ça !!
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
hibouman Messages postés 13 Date d'inscription dimanche 1 juin 2003 Statut Membre Dernière intervention 21 juin 2006
25 mai 2006 à 00:12
Je me suis dit qu'en plaçant un thread ça pourrait marcher:
Voilà ce que ça donne:

using System;
using System.Windows.Forms;
using System.Threading;

public class essai
{
  public static void Main()
  {
    Thread Thread1;
    Thread1 = new Thread(new ThreadStart(ThreadLoop));
    Thread1.Start();
  }
  public static void ThreadLoop()
  {
    while (Thread.CurrentThread.IsAlive)
    {
      MouseButtons mbs = Control.MouseButtons;
      if ((mbs & MouseButtons.Left) != 0)
    {
      MessageBox.Show("click");
    }
  }
 }
}

Maintenant quelques soit l'endroit ou l'on click, l'ordi le signalera par un messagebox.
Bon c ptetre pas encore tres propre mais au moins je suis sur une piste !
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
26 mai 2006 à 03:36
Comme la question revient souvent, j'ai fait un exemple de hook pour la souris.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security;

public partial class MainForm : Form
{
    #region Win32


    [ StructLayout( LayoutKind.Sequential ) ]
    private struct MSLLHOOKSTRUCT
    {
        public int X;
        public int Y;
        public int Data;
        public int Flags;
        public int Time;
        public UIntPtr Extra;
    }


    [ DllImport( "user32.dll", SetLastError = true ), SuppressUnmanagedCodeSecurity ]
    private static extern IntPtr SetWindowsHookEx( int hook, HookProc callback, IntPtr module, uint threadID );


    [ DllImport( "user32.dll", SetLastError = true ), SuppressUnmanagedCodeSecurity ]
    private static extern bool UnhookWindowsHookEx( IntPtr hHook );


    [ DllImport( "user32.dll" ), SuppressUnmanagedCodeSecurity ]
    private static extern IntPtr CallNextHookEx( IntPtr hHook, int code, UIntPtr wParam, IntPtr lParam );


    private delegate IntPtr HookProc( int code, UIntPtr wParam, IntPtr lParam );
   
    private const int WH_MOUSE_LL = 14;
    private const int HC_ACTION = 0;


    #endregion Win32


    private IntPtr hHook = IntPtr.Zero;
    private HookProc hookProc = null;


    public MainForm( )
    {
        InitializeComponent( );
    }


    protected override void OnLoad( EventArgs e )
    {
        base.OnLoad( e );


        hookProc = new HookProc( LowLevelMouseProc );
        hHook = SetWindowsHookEx( WH_MOUSE_LL, hookProc, Marshal.GetHINSTANCE( this.GetType( ).Module ), 0 );
           
        if ( hHook == IntPtr.Zero ) // Le hook ne s'installe pas en mode DEBUG.
            MessageBox.Show( "Impossible d'installer le hook.", "Erreur" );
    }


    protected override void OnFormClosed( FormClosedEventArgs e )
    {
        base.OnFormClosed( e );


        if ( hHook != IntPtr.Zero )
        {
            if ( !UnhookWindowsHookEx( hHook ) )
                MessageBox.Show( "Impossible de désinstaller le hook.", "Erreur" );


            hHook = IntPtr.Zero;
            hookProc = null;
        }
    }


    // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/lowlevelmouseproc.asp
    private IntPtr LowLevelMouseProc( int code, UIntPtr wParam, IntPtr lParam )
    {
        if ( code == HC_ACTION )
        {
            unsafe
            {
                MSLLHOOKSTRUCT* p = ( MSLLHOOKSTRUCT* )lParam;
                Point screenPos = new Point( p->X, p->Y );
                Point clientPos = this.PointToClient( screenPos );
                this.Text = screenPos + " - " + clientPos; // Pour l'exemple.
            }
        }
       
        return CallNextHookEx( hHook, code, wParam, lParam );
    }
}
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
26 mai 2006 à 11:08
Faut le mettre sur codyx ca lutinore, ca va pouvoir resservir

Mx
MVP C# 
0
Rejoignez-nous