Public Const GWL_WNDPROC = -4
Public Const GWL_USERDATA = (-21)Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic Function Hook(ByVal hWnd As Long) As Long    Dim pOld As Long
    '指定自定义的窗口过程
    pOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
    '保存原来默认的窗口过程指针
    SetWindowLong hWnd, GWL_USERDATA, pOld
    Hook = pOld
End Function
以上为VB原语句改写成以下C#语句后
        public const short GWL_WNDPROC = -4;
        public const short GWL_USERDATA = (-21);        [DllImport("user32", EntryPoint = "SetWindowLongA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
        public static extern int SetWindowLong(int hWnd, int nIndex, int dwNewLong);        public static int Hook(int hWnd)
        {
            int returnValue;
            int pOld;
            pOld = System.Convert.ToInt32(SetWindowLong(hWnd, System.Convert.ToInt32(GWL_WNDPROC), System.Convert.ToInt32(new int(WindowProc))));
            SetWindowLong(hWnd, System.Convert.ToInt32(GWL_USERDATA), pOld);
            returnValue = pOld;
            return returnValue;
        }下面这句话总是报错,不会改pOld = System.Convert.ToInt32(SetWindowLong(hWnd, System.Convert.ToInt32(GWL_WNDPROC), System.Convert.ToInt32(new int(WindowProc))));
请高手指点

解决方案 »

  1.   

    AddressOf WindowProc在C#中怎么写
      

  2.   

    当前上下文中不存在”WindowProc““int”不包含采用“1”参数的构造函数
      

  3.   

    编译是过去了public const int GWL_WNDPROC  = -4;
            public const int GWL_USERDATA = -21;        public delegate IntPtr NewWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);        private NewWndProc wpr = null;        //备份的默认处理函数
            private IntPtr oldWndProc = IntPtr.Zero;        [DllImport("user32", EntryPoint = "SetWindowLongA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
            public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, NewWndProc dwNewLong);        [DllImport("user32.dll", CharSet = CharSet.Auto)]
            public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);        /// <summary>
            /// 
            /// </summary>
            /// <param name="hWnd"></param>
            /// <param name="msg"></param>
            /// <param name="wParam"></param>
            /// <param name="lParam"></param>
            /// <returns></returns>
            private IntPtr GridControlWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
            {
                IntPtr returnVar = IntPtr.Zero;   
                
                // 你的操作            returnVar = CallWindowProc(oldWndProc, hWnd, msg, wParam, lParam);
                return returnVar;
            }        /// <summary>
            /// 
            /// </summary>
            /// <param name="hWnd"></param>
            /// <returns></returns>
            public IntPtr Hook(IntPtr hWnd)
            {                       
                IntPtr returnValue = IntPtr.Zero;
                wpr = new NewWndProc(this.GridControlWndProc);
                IntPtr pOld = SetWindowLong(hWnd, GWL_WNDPROC, wpr);
                SetWindowLong(hWnd, GWL_USERDATA, (NewWndProc)Marshal.GetDelegateForFunctionPointer(pOld,typeof(NewWndProc)));
                returnValue = pOld;
                return returnValue;
            }
      

  4.   

     public delegate IntPtr NewWndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);要求它为静态的 但把它设置静态后 提示修示符对NewWndProc无效
      

  5.   

    已调试过去,谢谢还有一个小问题,如何调用上面的Hook方法IntPtr 这个类型        private void frmMain_Load(object sender, EventArgs e)
            {
                this.Tag = Dcb.Hook(this.Handle.???);
            }