如何在C#中挂钩多个窗口?

时间:2022-10-05 23:52:18

I am trying to create an application that makes a window (external to the app) transparent when it loses focus. Most of things (getting window id, seting transparent, etc.) would be easy, except one thing - how do I hook windows?

我正在尝试创建一个应用程序,当它失去焦点时,使窗口(应用程序外部)透明。大多数事情(获取窗口ID,设置透明等)都很容易,除了一件事 - 我如何挂钩窗户?

1 个解决方案

#1


You can use interop. Use SendMessage() function to send your window a custom message. The window can then call SetLayeredWindowAttributes() once your receive that message to change its transparency.

你可以使用互操作。使用SendMessage()函数向您的窗口发送自定义消息。一旦您收到该消息以改变其透明度,窗口就可以调用SetLayeredWindowAttributes()。

The other thing is you really should be able to make the window turn ITSELF transparent when it loses focus by listening for WM_KILLFOCUS

另一件事是你真的应该能够通过听WM_KILLFOCUS使窗口失去焦点时使窗口变为透明

EDIT:

Latch onto the Deactivate and Activated events in C#.

锁定C#中的Deactivate和Activated事件。

    private void Form1_Deactivate( object sender, EventArgs e )
    {
      this.Opacity = 0.5 ;
    }

    private void Form1_Activated( object sender, EventArgs e )
    {
      this.Opacity = 1.0 ;
    }

#1


You can use interop. Use SendMessage() function to send your window a custom message. The window can then call SetLayeredWindowAttributes() once your receive that message to change its transparency.

你可以使用互操作。使用SendMessage()函数向您的窗口发送自定义消息。一旦您收到该消息以改变其透明度,窗口就可以调用SetLayeredWindowAttributes()。

The other thing is you really should be able to make the window turn ITSELF transparent when it loses focus by listening for WM_KILLFOCUS

另一件事是你真的应该能够通过听WM_KILLFOCUS使窗口失去焦点时使窗口变为透明

EDIT:

Latch onto the Deactivate and Activated events in C#.

锁定C#中的Deactivate和Activated事件。

    private void Form1_Deactivate( object sender, EventArgs e )
    {
      this.Opacity = 0.5 ;
    }

    private void Form1_Activated( object sender, EventArgs e )
    {
      this.Opacity = 1.0 ;
    }