Search code examples
windowsdelphidelphi-xelazarustranslucency

Making other applications window's translucent in Delphi


Good Day all

I searched the web for any directions as to if this is possible but to no avail. I need to write an application that will allow me to select another application and by doing so make the selected application translucent and on-top (like a ghost image overlay).

Is this at all possible with Delphi? I am using Delphi XE and Lazarus. If anybody could just please point me in the general direction of where to start I will be much obliged.

Thanks in advance,


Solution

  • You can do this but is not recommended, because this kind of behavior must be handled by the own application. anyway if you insist because do you have a very good reason to do this, here i leave the code to set the transparency of a window and Make a windows Top Most, just to show how can be done.

    Transparency

    you must use the SetWindowLong function with the WS_EX_LAYERED flag and the SetLayeredWindowAttributes function with LWA_ALPHA to set the transparency.

    Procedure SethWndTrasparent(hWnd: HWND;Transparent:boolean);
    var
     l        : Longint;
     lpRect   : TRect;
    begin
        if Transparent then
        begin
          l := GetWindowLong(hWnd, GWL_EXSTYLE);
          l := l or WS_EX_LAYERED;
          SetWindowLong(hWnd, GWL_EXSTYLE, l);
          SetLayeredWindowAttributes(hWnd, 0, 180, LWA_ALPHA);
        end
        else
        begin
          l := GetWindowLong(hWnd, GWL_EXSTYLE);
          l := l xor WS_EX_LAYERED;
          SetWindowLong(hWnd, GWL_EXSTYLE, l);
          GetWindowRect(hWnd, lpRect);
          InvalidateRect(hWnd, lpRect, true);
        end;
    end;
    

    Make a windows Top Most

    You must use the SetWindowPos function passing the HWND_TOPMOST value which places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.

    Procedure SethWndOnTop(hWnd: HWND);
    var
      lpRect   : TRect;
    begin
      if GetWindowRect(hWnd,lpRect) then
      SetWindowPos(hWnd , HWND_TOPMOST, lpRect.left, lpRect.top, lpRect.Right-lpRect.left, lpRect.Bottom-lpRect.Top, SWP_SHOWWINDOW);
    end;