Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#1

LoadLibrary vs GetModuleHandle, eine Verständisfrage

  Alt 14. Mai 2021, 19:22
Hallo, hier mal ein stück code mit einem Ergebnis was mich etwas verwirrt.

gegeben ist dieser Auszug aus meinem Theming Projekt (der Vollständigkeit halber ist hoffentlich alles nötige mit aufgeführt)

In dieser Variante, funktioniert (auch wenn falsch integriert) bei gesetzten ThemeDarkLight es halbwegs so wie ich es erhoffe.
Delphi-Quellcode:
type
  TkzTheme = (ThemeNone, ThemeDarkLight, ThemeSystem, ThemeWallpaper);

  kzTheme = class(TObject)
    ...
  strict private
    FStyleTheme: TkzTheme;
  strict private
    procedure SetDwmMode(const AMode: ShortInt);
    ...
  end;

  AccentPolicy = packed record
    AccentState: Integer;
    AccentFlags: Integer;
    GradientColor: Integer;
    AnimationId: Integer;
  end;

  TWinCompAttrData = packed record
    attribute: THandle;
    pData: Pointer;
    dataSize: ULONG;
  end;

var
  SetWindowCompositionAttribute: function(Wnd: HWND; const AttrData: TWinCompAttrData): BOOL; stdcall = nil;

implementation

procedure kzTheme.SetDwmMode(const AMode: ShortInt);
const
  WCA_ACCENT_POLICY = 19;
  ACCENT_DISABLE = 0;
  ACCENT_ENABLE_GRADIENT = 1;
  ACCENT_ENABLE_TRANSPARENT = 2;
  ACCENT_ENABLE_BLURBEHIND = 3;
  GRADIENT_COLOR = $01000000;
  DrawLeftBorder = $20;
  DrawTopBorder = $40;
  DrawRightBorder = $80;
  DrawBottomBorder = $100;
var
  dwm10: THandle;
  data: TWinCompAttrData;
  accent: AccentPolicy;
begin
  if (not ((AMode = ACCENT_DISABLE) or (AMode = ACCENT_ENABLE_TRANSPARENT) or (AMode = ACCENT_ENABLE_BLURBEHIND))) then
    Exit;
  dwm10 := LoadLibrary(user32);
  try
    @SetWindowCompositionAttribute := GetProcAddress(dwm10, 'SetWindowCompositionAttribute');
    if @SetWindowCompositionAttribute <> nil then
    begin
      accent.AccentState := AMode;
      accent.AccentFlags := DrawLeftBorder or DrawTopBorder or DrawRightBorder or DrawBottomBorder;
      if ((FStyleTheme = ThemeSystem) or (FStyleTheme = ThemeWallpaper)) then
        accent.GradientColor := FColorBackground;
      data.attribute := WCA_ACCENT_POLICY;
      data.dataSize := SizeOf(accent);
      data.pData := @accent;
      SetWindowCompositionAttribute(FForm.Handle, data);
    end;
  finally
    FreeLibrary(dwm10);
  end;
end;

initialization
  SetWindowCompositionAttribute := GetProcAddress(GetModuleHandle(user32), 'SetWindowCompositionAttribute');
end.
himitsu schrieb ich soll auf LoadLibrary verzichten und direkt per GetModuleHandle zugreifen sowie den Schutzblock entfernen, gesagt getan:
Delphi-Quellcode:
procedure kzTheme.SetDwmMode(const AMode: ShortInt);
const
  WCA_ACCENT_POLICY = 19;
  ACCENT_DISABLE = 0;
  ACCENT_ENABLE_GRADIENT = 1;
  ACCENT_ENABLE_TRANSPARENT = 2;
  ACCENT_ENABLE_BLURBEHIND = 3;
  GRADIENT_COLOR = $01000000;
  DrawLeftBorder = $20;
  DrawTopBorder = $40;
  DrawRightBorder = $80;
  DrawBottomBorder = $100;
var
  data: TWinCompAttrData;
  accent: AccentPolicy;
begin
  if (not ((AMode = ACCENT_DISABLE) or (AMode = ACCENT_ENABLE_TRANSPARENT) or (AMode = ACCENT_ENABLE_BLURBEHIND))) then
    Exit;
  @SetWindowCompositionAttribute := GetProcAddress(GetModuleHandle(user32), 'SetWindowCompositionAttribute');
  if @SetWindowCompositionAttribute <> nil then
  begin
    accent.AccentState := AMode;
    accent.AccentFlags := DrawLeftBorder or DrawTopBorder or DrawRightBorder or DrawBottomBorder;
    if ((FStyleTheme = ThemeSystem) or (FStyleTheme = ThemeWallpaper)) then
      accent.GradientColor := FColorBackground;
    data.attribute := WCA_ACCENT_POLICY;
    data.dataSize := SizeOf(accent);
    data.pData := @accent;
    SetWindowCompositionAttribute(FForm.Handle, data);
  end;
end;
Doch nun funktioniert wenn ThemeDarkLight aktiv ist (Hintergrund ist schwarz) es nicht mehr.

Liegt es daran das mein code generell falsch ist oder gibt es doch unterschiede zwischen LoadLibrary und GetModuleHandle die mir momentan noch verborgen sind?

LG
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat