首页  编辑  

截获控件消息

Tags: /C#/API调用/   Date Created:

using System ;

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 WindowsApplication1

{

    public partial class Form1 : Form

   {

        public Form1()

       {

           InitializeComponent();

       }

        private delegate IntPtr WndProcCallBack ( IntPtr hwnd, int Msg, IntPtr wParam, IntPtr lParam);

       [ DllImport ( "user32.dll" , SetLastError = true )]

        private static extern int SetWindowLong( IntPtr hWnd, int nIndex, int dwNewLong);

       [ DllImport ( "user32.dll" , SetLastError = true )]

        private static extern int GetWindowLong( IntPtr hWnd, int nIndex);

       [ DllImport ( "User32.dll" )]

        private static extern IntPtr CallWindowProc( IntPtr lpPrevWndFunc, IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        private const int GWL_WNDPROC = - 4 ;

        private IntPtr prevWndFunc;

        private const int WM_MOUSEMOVE = 0x0200 ;

        private IntPtr newWndProc( IntPtr hWnd, int iMsg, IntPtr wParam, IntPtr lParam)

       {

            switch (iMsg)

           {

                case WM_MOUSEMOVE:

                   Text = "MouseMove: " + DateTime . Now . ToString();

                    break ;

           }

            return CallWindowProc(prevWndFunc, hWnd, iMsg, wParam, lParam);

       }

        private void Form1_Load( object sender, EventArgs e)

       {

           prevWndFunc = new IntPtr (GetWindowLong(button1 . Handle, GWL_WNDPROC));

           Text = prevWndFunc . ToString();

            WndProcCallBack vWndProcCallBack = new WndProcCallBack (newWndProc);

           SetWindowLong(button1 . Handle, GWL_WNDPROC,

                Marshal . GetFunctionPointerForDelegate(vWndProcCallBack) . ToInt32());

       }

        private void Form1_FormClosed( object sender, FormClosedEventArgs e)

       {

           SetWindowLong(button1 . Handle, GWL_WNDPROC, prevWndFunc . ToInt32());

           prevWndFunc = IntPtr . Zero;

       }

   }

}

//----------------------------------

using System . Runtime . InteropServices;

[ DllImport ( "user32.dll" )]

public static extern IntPtr GetWindow( IntPtr hWnd, uint uCmd);

public const uint GW_CHILD = 5 ;

private delegate IntPtr WndProcCallBack ( IntPtr hwnd, int Msg,

    IntPtr wParam, IntPtr lParam);

[ DllImport ( "user32.dll" )]

private static extern int SetWindowLong( IntPtr hWnd, int nIndex, int dwNewLong);

[ DllImport ( "user32.dll" )]

private static extern int GetWindowLong( IntPtr hWnd, int nIndex);

[ DllImport ( "User32.dll" )]

private static extern IntPtr CallWindowProc( IntPtr lpPrevWndFunc,

    IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

private int GWL_WNDPROC = - 4 ;

private static IntPtr prevWndFunc;

private IntPtr newWndProc( IntPtr hWnd, int iMsg, IntPtr wParam, IntPtr lParam)

{

    const int WM_CONTEXTMENU = 0x007B ;

    switch (iMsg)

   {

        case WM_CONTEXTMENU:

            return IntPtr . Zero;

   }

    return CallWindowProc(prevWndFunc, hWnd, iMsg, wParam, lParam);

}

private IntPtr comboBoxEdit;

private WndProcCallBack wndProcCallBack;

private void Form1_Load( object sender , EventArgs e)

{

   comboBoxEdit = GetWindow(comboBox1 . Handle, GW_CHILD);

   prevWndFunc = new IntPtr (GetWindowLong(comboBoxEdit, GWL_WNDPROC));

   wndProcCallBack = new WndProcCallBack (newWndProc);

   SetWindowLong(comboBoxEdit, GWL_WNDPROC,

       ( int ) Marshal . GetFunctionPointerForDelegate(wndProcCallBack));

}

//----------------------------------

using System . Runtime . InteropServices;

[ DllImport ( "user32.dll" )]

public static extern IntPtr GetWindow( IntPtr hWnd, uint uCmd);

public const uint GW_CHILD = 5 ;

public class SubWindow : NativeWindow

{

    protected override void WndProc( ref Message m)

   {

        const int WM_CONTEXTMENU = 0x007B ;

        switch (m . Msg)

       {

            case WM_CONTEXTMENU: return;

       }

        base . WndProc( ref m);

   }

}

private void Form1_Load( object sender , EventArgs e)

{

    SubWindow vSubWindow = new SubWindow ();

   

   vSubWindow . AssignHandle(GetWindow(comboBox1 . Handle, GW_CHILD));

}