日期:2014-05-19  浏览次数:21526 次

C#如何实现VB中AddressOf功能
下面是VB实现代码,用C#如何实现呢?   先谢了

Private   Declare   Function   SetWindowLong   Lib   "user32 "   Alias   "SetWindowLongA "   (ByVal   hwnd   As   Long,   ByVal   nIndex   As   Long,   ByVal   dwNewLong   As   Long)   As   Long

Sub
    Dim   lpPrevWndProc   As   Long
       
    lpPrevWndProc   =   SetWindowLong(hwnd,   GWL_PROC,   AddressOf   MyWindowProc)
End   Sub

Private   Function   MyWindowProc(ByVal   hwnd   As   Long,   ByVal   uMsg   As   Long,   ByVal   wParam   As   Long,   ByVal   lParam   As   Long)   As   Long
        ...
End   Function

------解决方案--------------------
阅读 System.Runtime.InteropServices.Marshal
------解决方案--------------------
楼主,用不着这样做,你可以直接重写窗体的WndProc()函数。这个函数就是用来处理Windows消息循环的!

还有,在C#中实现回调函数用CallBack;
using System;
using System.Runtime.InteropServices;

public delegate bool CallBack(int hwnd, int lParam);

public class EnumReportApp {

[DllImport( "user32 ")]
public static extern int EnumWindows(CallBack x, int y);

public static void Main()
{
CallBack myCallBack = new CallBack(EnumReportApp.Report);//这里定义一个回调。
EnumWindows(myCallBack, 0);
}

public static bool Report(int hwnd, int lParam) {
Console.Write( "Window handle is ");
Console.WriteLine(hwnd);
return true;
}
}
------解决方案--------------------
正好做过同样的事情
public delegate Int32 WndProcHandler(IntPtr hWnd,UInt32 msg,Int32 wParam,Int32 lParam);
private WndProcHandler m_pNewWndProc = null;

m_pNewWndProc = new WndProcHandler(WndProcFunc);
Debug.Assert(m_pNewWndProc != null);
dwRet = SetWindowLong(m_hMsgWnd,GWL_WNDPROC,m_pNewWndProc);

private Int32 WndProcFunc(IntPtr hWnd,UInt32 msg,Int32 wParam,Int32 lParam)
{
....
return 0;
}

可以参考一下:
http://blog.csdn.net/Mittermeyer/archive/2007/04/27/1586867.aspx