Gérer sa propre fonction de messages avec le Compact Framework 2.0 (Pour Smartphone)

30. mai 2008

Contrairement aux Windows Forms, il n'est pas directement possible avec le .NET CF 2.0(SmartPhone) de surcharger la WndProc de ses formulaires.

Hors c'est parfois super utile!

Dans mon cas je voulais déclencher du code dès que l'utilisateur touche l'écran ou le clavier sans avoir à pourrir mon code avec des verrues de partout.

Le résultat c'est cadeau pour vous :) :

public partial class RootForm : Form
{
    private const int GWL_WNDPROC = -4;

    delegate IntPtr WndProcHandler(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);

    [DllImport("coredll.dll", EntryPoint = "GetWindowLong")]
    private static extern IntPtr GetWindowLong(IntPtr hwnd, int index);

    [DllImport("coredll.dll")]
    static extern int SetWindowLong(IntPtr hwnd, int index, IntPtr wndProc);

    [DllImport("coredll.dll")]
    static extern IntPtr CallWindowProc(IntPtr prevWndFunc, IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam);

    private IntPtr prevWndProc = IntPtr.Zero;
    private WndProcHandler wndProc;

    public RootForm()
    {
        InitializeComponent();

        wndProc = new WndProcHandler(WndProc);

        prevWndProc = GetWindowLong(this.Handle, GWL_WNDPROC);

        int success = SetWindowLong(this.Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(wndProc));
    }

    private IntPtr WndProc(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        // Mon code juste ici!
        return CallWindowProc(prevWndProc, hwnd, msg, wParam, lParam);
    } 
}

 

Hope this help!

Bookmark and Share

.Net, Win32, Windows Mobile

Commentaires

Gwenojenn
20/10/2008 19:33:20 #
Merci pour le bout de code !
Vraiment très pratique ...
Par contre j'ai un pb , j'essai de chopper le WM_CopyData ... mais j'y arrive pas...

private const int WM_COPYDATA = 0x4A;

private IntPtr WndProc(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam)
        {

            switch (msg)
            {
                case WM_COPYDATA:
                  ...
                break;
            }

mais il n'y passe jamais...

si tu as une idée, je suis preneur !

G.

Ajouter un commentaire




  Country flag

biuquote
  • Commentaire
  • Aperçu immédiat
Loading