我正在使用C#表单创建一个程序,它在另一个应用程序窗口的顶部充当叠加层。整个表格几乎是一个透明的PictureBox,横跨整个区域,并在这里和那里绘制形状。
现在,我只能与覆盖层完全透明的底层窗口进行交互,如何使非透明区域不拦截鼠标事件?
为清楚起见,这是一个截图:
Skype是底层应用程序。我的叠加层绘制了蓝色(和灰色)框。我需要点击框下方的链接。
不幸的是,我没有代码可以显示,因为我不确定该程序的哪个部分实际处理这样的事情。
感谢。
答案 0 :(得分:-1)
您可以尝试订阅形状点击事件,然后通过某些Windows API将事件转发到“重叠”窗口。如果你有一个指向应用程序主窗口的指针(要么是通过Process对象自己启动它,要么是通过其他方式得到它),你可以简单地将鼠标事件发送到该窗口。
以下示例采用屏幕的当前点并将其发送到Skype的第一个实例。
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[StructLayout(LayoutKind.Explicit)]
struct LParamLocation
{
[FieldOffset(0)]
int Number;
[FieldOffset(0)]
public short X;
[FieldOffset(2)]
public short Y;
public static implicit operator int(LParamLocation p)
{
return p.Number;
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
var process = Process.GetProcessesByName("skype");
LParamLocation points = new LParamLocation();
points.X = (short)PointToScreen(e.Location).X;
points.Y = (short)PointToScreen(e.Location).Y;
SendMessage(process[0].MainWindowHandle, 0x201, 0, points); //MouseLeft down message
SendMessage(process[0].MainWindowHandle, 0x202, 0, points); //MouseLeft up message
}
另外,您可以尝试添加窗口样式以告诉它通过所有鼠标事件。
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
int style = GetWindowLong(Handle, -20);
style |= 0x00000020; // Enables Pass-Through of events
style |= 0x00080000; // Enables Pass-Through to layered windows
SetWindowLong(Handle, -20, style);
}