was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

34 Visitors Online


 
...eine Form halb-/transparent machen?
Autor: Lars Pietrowski
[ Tip ausdrucken ]  

Tip Bewertung (31):  
     


// Make your forms 25%, 50% Transparent...

TMyForm = class (TForm);

TrackBar1: TTrackBar;
//..
private
FColorKey: TCOLOR;

end;

const
  
// Use crKey as the transparency color.
  
LWA_COLORKEY = 1;
  // Use bAlpha to determine the opacity of the layered window..
  
LWA_ALPHA     = 2;
  WS_EX_LAYERED = $80000;

implementation

{$R *.DFM}

// The SetLayeredWindowAttributes function sets the opacity and transparency
// color key of a layered window.
// Note : This function is only available with Windows 2000 and XP!
function SetLayeredWindowAttributes(
  // Handle to the layered window.
  
Wnd: hwnd;
  // Pointer to a COLORREF value that specifies the transparency
  // color key to be used when composing the layered window
  
crKey: ColorRef;
  // Alpha value used to describe the opacity of the layered window
  
Alpha: Byte;
  // Specifies an action to take
  // LWA_COLORKEY or LWA_ALPHA
  // This parameter can be one or more of the following values:
  
dwFlags: DWORD): Boolean;
  stdcallexternal 'user32.dll';


procedure TMyForm.TrackBar1Change(Sender: TObject);
  // Trackbar.Position must run at range 1-255...
begin
  
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED);
  SetLayeredWindowAttributes(Handle, ColorToRGB(FColorKey), TrackBar1.Position, LWA_ALPHA);
end;

{******************************************************************}

{ To load the SetLayeredWindowAttributes() function dynamically, use this function:}

function MakeWindowTransparent(Wnd: HWND; nAlpha: Integer = 10): Boolean;
type
  
TSetLayeredWindowAttributes = function(hwnd: HWND; crKey: COLORREF; bAlpha: Byte;
    dwFlags: Longint): Longint; 
  stdcall;
const
  
// Use crKey as the transparency color.
  
LWA_COLORKEY = 1;
  // Use bAlpha to determine the opacity of the layered window..
  
LWA_ALPHA     = 2;
  WS_EX_LAYERED = $80000;
var
  
hUser32: HMODULE;
  SetLayeredWindowAttributes: TSetLayeredWindowAttributes;
  i: Integer;
begin
  
Result := False;
  // Here we import the function from USER32.DLL
  
hUser32 := GetModuleHandle('USER32.DLL');
  if hUser32 <> 0 then
  begin 
@SetLayeredWindowAttributes := GetProcAddress(hUser32, 'SetLayeredWindowAttributes');
    // If the import did not succeed, make sure your app can handle it!
    
if @SetLayeredWindowAttributes <> nil then
    begin
      
// Check the current state of the dialog, and then add the WS_EX_LAYERED attribute
      
SetWindowLong(Wnd, GWL_EXSTYLE, GetWindowLong(Wnd, GWL_EXSTYLE) or WS_EX_LAYERED);
      // The SetLayeredWindowAttributes function sets the opacity and
      // transparency color key of a layered window
      
SetLayeredWindowAttributes(Wnd, 0, Trunc((255 / 100) * (100 - nAlpha)), LWA_ALPHA);
      Result := True;
    end;
  end;
end;

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners