前段时间,我做了一个panel,有鼠标动作,可以拖动,但我在panel中加上一个label后发现,label的区域鼠标就不能操作了,例如拖动,聚焦等,请问如何在label上操作panel呢?
后经过刘威的指点,采用如下方法:[DllImport("USER32.DLL ", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam
);
private void label1_MouseDown(object sender, MouseEventArgs e)
{
int cupos = (e.Location.X) | (e.Location.Y) << 16;
SendMessage(this.panel1.Handle, WM_LBUTTONDOWN, 0, cupos);
}采用消息的方法,成功了,但新的问题又出来了,鼠标点击后鼠标乱跑,并不在Label控件处,而是在左上角,我想知道是为什么,另外如何改呢?
谢谢。
后经过刘威的指点,采用如下方法:[DllImport("USER32.DLL ", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam
);
private void label1_MouseDown(object sender, MouseEventArgs e)
{
int cupos = (e.Location.X) | (e.Location.Y) << 16;
SendMessage(this.panel1.Handle, WM_LBUTTONDOWN, 0, cupos);
}采用消息的方法,成功了,但新的问题又出来了,鼠标点击后鼠标乱跑,并不在Label控件处,而是在左上角,我想知道是为什么,另外如何改呢?
谢谢。
{
int cupos = (e.Location.X) | (e.Location.Y) << 16;
SendMessage(this.panel1.Handle, WM_LBUTTONDOWN, 0, cupos);
}
是我以前没有考虑这个问题,原因是:此时的MouseEventArgs e记录的坐标是鼠标在当前Lable上的位置【(比如说:(5,4))】,而SendMessage是往Panel上面发送的时候传送了这个坐标【要求鼠标在Panel的(5,4)位置按下】。
所以要解决这个问题,你只需要知道Lable的上、左和Panel的上、左的距离差,加到e.Location.X和e.Location.Y上面即可。
int cupos = (e.Location.X + panel.Left) | (e.Location.Y + panel.Top)<<16;
SendMessage(temP.Handle, WM_LBUTTONDOWN, 0, cupos);
非常感谢你,我很高兴。