如何重新聚焦于透明(ClickThrough启用)窗口

时间:2018-07-23 14:05:30

标签: c++ c winapi alpha-transparency click-through

我正在使用一个最初是透明的窗口的应用程序,然后在按键(例如shift+tab)上的窗口应该是 Not ClickThrough。我用来获取ClickThrough的代码如下:

_hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT, 
            TEXT("Example"), 
            title, WS_BORDER,
            GetSystemMetrics(SM_CXSCREEN) / 2 - _width / 2,
            GetSystemMetrics(SM_CYSCREEN) / 2 - _height / 2,
            _width, _height,
            NULL, NULL,
            NULL, NULL);
int opacity = 70;
SetLayeredWindowAttributes(_hwnd, 0, (255 * opacity) / 100, LWA_ALPHA); 

现在,有什么解决办法可以 ClickThrough吗?我用谷歌搜索,但没有找到。

1 个答案:

答案 0 :(得分:0)

是的,我通过注册 HotKeys 完成了此操作: WndProc 如下:

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    switch (message)
    {
    //break;
    case WM_HOTKEY:
    {
        switch (wparam)
        {
        case 1:// Close Window
            PostQuitMessage(0);
            break;
        case 2://Disable ClickThrough
            SetWindowLong(hwnd, -20, WS_EX_LAYERED); //-16 for window style
            break;
        case 3://enable ClickThrough
            SetWindowLong(hwnd, -20, WS_EX_LAYERED | WS_EX_TRANSPARENT); 
            break;
        }
    }
    break;
    case WM_CLOSE:
    {
        DestroyWindow(hwnd);
        return 0;
    }
    break;
    default:
        return DefWindowProc(hwnd, message, wparam, lparam);
    }


}

注册热键,如下所示:

RegisterHotKey(_hwnd, 1, MOD_SHIFT, 0x43); //shift + c
RegisterHotKey(_hwnd, 2, MOD_SHIFT, 0x44); //shift + d
RegisterHotKey(_hwnd, 3, MOD_SHIFT, 0x45); //shift + e