我要用调用API的PostMessange(),而他的函数原形是
LRESULT SendMessage(
                HWND hWnd,      // handle of destination window
                UINT Msg,       // message to send
                WPARAM wParam,  // first message parameter
                LPARAM lParam   // second message parameter
               );
我在C#该怎么传递参数呢?
C#中好象没有某些类型。我找了下别人写的,他在C#写的变成:
static extern int SendMessage(IntPtr hWnd,int msg,int wParam,ref int lParam);
为什么是这样呢
HWND->IntPtr
UINT-->int
WPARAM-->int
LPARAM-->ref int
这是为什么呢?把参数类型都变了还能用吗?为什么要变成这种类型?(固定的?)那如果我用别的API,然后参数也有上述类型的,我也必须(可行?)按照上述规律转换吗?

解决方案 »

  1.   

    [StructLayout(LayoutKind.Sequential)]
        public struct MSG 
        {
            public IntPtr hwnd;
            public int message;
            public IntPtr wParam;
            public IntPtr lParam;
            public int time;
            public int pt_x;
            public int pt_y;
        }    [StructLayout(LayoutKind.Sequential)]
        public struct PAINTSTRUCT
        {
            public IntPtr hdc;
            public int fErase;
            public Rectangle rcPaint;
            public int fRestore;
            public int fIncUpdate;
            public int Reserved1;
            public int Reserved2;
            public int Reserved3;
            public int Reserved4;
            public int Reserved5;
            public int Reserved6;
            public int Reserved7;
            public int Reserved8;
        }    [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }    [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int x;
            public int y;
        }    [StructLayout(LayoutKind.Sequential)]
        public struct SIZE
        {
            public int cx;
            public int cy;
        }    [StructLayout(LayoutKind.Sequential, Pack=1)]
        public struct BLENDFUNCTION
        {
            public byte BlendOp;
            public byte BlendFlags;
            public byte SourceConstantAlpha;
            public byte AlphaFormat;
        }    [StructLayout(LayoutKind.Sequential)]
        public struct TRACKMOUSEEVENTS
        {
            public uint cbSize;
            public uint dwFlags;
            public IntPtr hWnd;
            public uint dwHoverTime;
        }    [StructLayout(LayoutKind.Sequential)]
        public struct LOGBRUSH
        {
            public uint lbStyle; 
            public uint lbColor; 
            public uint lbHatch; 
        }
      

  2.   

    其实并不是参数数据类型变了,数据类型没变,只是变了代表数据类型的名称。因为每个语言中的数据类型用的代表符不一样。如果你的API是C写的,用的是HWND,这个类型在C#用IntPtr表示,而在VB中又是Long代表。
      

  3.   

    可以,因为中个API中HWND参数要的就是句柄,而句柄就是整数类型。所以int也行!!!