Search code examples
c++cwinapialpha-transparencyclick-through

How to get focus back on transparent (ClickThrough enabeled) Window


I am working on an application in which a window is transparent initially,then on a key press (say shift+tab) window should be Not ClickThrough. Code which I use to get ClickThrough is as follow:

_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); 

Now, any solution to get Not ClickThrough? I google it but never find any one.


Solution

  • Yes I have done this using registering HotKeys: WndProc is as follows:

    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);
        }
    
    
    }
    

    Register HotKeys as follows:

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