Salut,

J'essai d'intercepter les messages d'une fenêtre externe à mon application.
J'ai fait un wrapper que voici:

Code c# : 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
 
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using OpenNETCF.Windows.Forms;
using System.Windows.Forms;
 
namespace WindowHookWrapper
{
    public struct HTCTOUCH_WPARAM
    {
        public byte Up;    //0=KeyDown,1=KeyUp
        public byte Where; //Where the click occurs (left pane, wheel, right pane)
        //Pos for the Right Pane click
        public byte xPosRP;
        public byte yPosRP;
    }
 
    class WindowHooker : IDisposable
    {
        const int GW_WNDPROC = -4;
 
        delegate int WndProcHandler(IntPtr hwnd, int message, int wParam, int lParam);
 
        [DllImport("coredll")]
        extern static IntPtr SetWindowLong(IntPtr hWnd, int dwId, IntPtr newLong);
 
        [DllImport("coredll")]
        extern static int CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, int wParam, int lParam);
 
        [DllImport("coredll")]
        extern static bool IsWindow(IntPtr hWnd);
 
        IntPtr pOldProc;
        IntPtr pHandle;
 
        WndProcHandler WndProcH;
 
        private int WndProc(IntPtr hWnd, int uMsg, int wParam, int lParam)
        {
            return CallWindowProc(pOldProc, hWnd, uMsg, wParam, lParam);
        }
 
        public WindowHooker(IntPtr handle)
        {
            pOldProc = IntPtr.Zero;
            pHandle = IntPtr.Zero;
            WndProcH = new WndProcHandler(WndProc);
            if (handle != IntPtr.Zero)
            {
                HookWindow(handle);
            }
        }
 
        public void UnHookWindow()
        {
            if( pOldProc != IntPtr.Zero && pHandle != IntPtr.Zero )
            {
                if( IsWindow( pHandle ) )
                {
                    try
                    {
                        SetWindowLong(pHandle, GW_WNDPROC, pOldProc);
                    }
                    catch
                    {
                        // Unable to setwindowlong...
                    }
                }
            }
            pOldProc = IntPtr.Zero;
            pHandle = IntPtr.Zero;
        }
 
        public void HookWindow(IntPtr handle)
        {
            if ((pOldProc == IntPtr.Zero) && (pHandle == IntPtr.Zero))
            {
                if (IsWindow(handle))
                {
                    pOldProc = SetWindowLong(handle, GW_WNDPROC, Marshal.GetFunctionPointerForDelegate(WndProcH));
                    pHandle = handle;
                }
            }
            else
            {
                UnHookWindow();
                HookWindow(handle);
            }
        }
 
        public void Dispose()
        {
 
        }
    }
}

Et je l'utilise de cette manière:

Code C# : 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
 
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace WindowHookWrapper
{
    public partial class Form1 : Form
    {
        [DllImport("coredll")]
        extern static int GetClassName(IntPtr hWnd, StringBuilder sBuffer, int nChars);
 
        [DllImport("coredll")]
        extern static IntPtr GetForegroundWindow();
 
        [DllImport("coredll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
 
        WindowHooker myHooker;
        Timer tTimer;
        StringBuilder sBuff;
        short nChars = 256;
 
        public Form1()
        {
            InitializeComponent();
 
            myHooker = new WindowHooker(IntPtr.Zero);
 
            sBuff = new StringBuilder(nChars);
 
            tTimer = new Timer();
            tTimer.Interval = 1000;
            tTimer.Tick += new EventHandler(tTimer_Tick);
            tTimer.Enabled = true;
        }
 
        IntPtr handle = IntPtr.Zero;
        void tTimer_Tick(object sender, EventArgs e)
        {
            if (handle == IntPtr.Zero)
                handle = GetForegroundWindow();
 
            if (!GetForegroundWindow().Equals(handle))
            {
                handle = GetForegroundWindow();
                if (GetClassName(GetForegroundWindow(), sBuff, 256) > 0)
                {
                    if (sBuff.ToString().CompareTo("ApplicationClass") == 0)
                    {
                        myHooker.HookWindow(handle);
                    }
                }
            }
 
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            ShowWindow(this.Handle, 6); // Minimize
        }
    }
}

Mais mon logiciel fait tout crasher quand je ferme cette dite application.

Quelqu'un a une idée ?

Merci d'avance

P.S.: J'utilise ce code sur un PDA sous Windows Mobile 6