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!
.Net, Win32, Windows Mobile