日期:2014-05-17  浏览次数:21123 次

C#(API)Sendmessage参数问题
方法SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM IParam)中
第一个参数:控件句柄hWnd已知,
第二个参数:WM_LBUTTONDOWN
第三个参数:MK_LBUTTON 
问题是最后一个参数,
若已知int型坐标X,Y,通过移位之类的运算怎样转化成lParam?

------解决方案--------------------
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, int lParam);

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0201)
            {
                int xPos = m.LParam.ToInt32() & 0xFFFF;
                int yPos = (m.LParam.ToInt32()) >> 16;

                Text = xPos.ToString() + "  " + yPos.ToString();
            }
            base.WndProc(ref m);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Point p = new Point(101, 201);
            int point = p.X + (p.Y << 16); 
            SendMessage(this.Handle, 0x0201, IntPtr.Zero, point);
        }