How to pass mouse events to applications behind mine in C#/Vista?

前端 未结 3 932
心在旅途
心在旅途 2020-12-10 08:45

I am writing a very specialized app in C# that floats as a mostly transparent window over the entire desktop. I want to be able to create and pass mouse events to applicatio

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-10 09:35

    After looking at System hooks and other low level solutions I found a much simpler method.

    First, set the TransparencyKey and BackColor of the form to be the same. This didn't make any visual difference to me as my form was visually transparent already, but this will help in letting mouse events through.

    Second, the following code will let mouse events "fall" through your form.

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
    
            return createParams;
        }
    }
    

    Lastly, set TopMost to be true.

    Your form will now float on top of everything visually, but will give up focus to all other apps.

提交回复
热议问题