SizeGrip, Version 1.2 (alt)

Hinweis: dies ist nicht die neueste Version!

Zurück zur Übersicht

Datei: SizeGripThemed.pas

{
  SizeGripThemed.pas

  Delphi component to add a size grip (like if you use a status bar) to the
  lower right corner of any TWinControl (like TForm). "SizeGripThemed.pas"
  is the version using the currently selected visual style. See the included
  README.txt for more information and how to use it.

  This unit has been separated from SizeGrip.pas to avoid linking of
  UxTheme if not necessary.

  Version 1.2 - always find the most current version at
  http://flocke.vssd.de/prog/code/pascal/sizegrip/

  Copyright (C) 2005 Volker Siebert <flocke@vssd.de>
  All rights reserved.

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.
}

unit SizeGripThemed;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SizeGrip;

type
  TSizeGripThemed = class(TSizeGrip)
  private
    FThemeWindow: HWND;
    FThemeHandle: THandle;
    FThemed: boolean;
    procedure CheckTheme;
    procedure SetThemed(const Value: boolean);
  protected
    procedure GetGripRect(var Rect: TRect); override;
    procedure PaintIt(DC: HDC; const Rect: TRect); override;
    procedure NewWndProc(var Msg: TMessage); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Themed: boolean read FThemed write SetThemed default true;
  end;

procedure Register;

implementation

uses
  UxTheme;

{ TSizeGripThemed }

constructor TSizeGripThemed.Create(AOwner: TComponent);
begin
  InitThemeLibrary;
  FThemed := true;

  inherited;
end;

destructor TSizeGripThemed.Destroy;
begin
  inherited;

  CheckTheme;
  FreeThemeLibrary;
end;

{ Check for the current theme
}
procedure TSizeGripThemed.CheckTheme;
var
  ThemesOk: boolean;
begin
  ThemesOk := UseThemes and
              FThemed and
              (TargetControl <> nil) and
              TargetControl.HandleAllocated;

  if FThemeHandle <> 0 then
    if (not ThemesOk) or (FThemeWindow <> TargetControl.Handle) then
    begin
      CloseThemeData(FThemeHandle);
      FThemeHandle := 0;
      FThemeWindow := 0;
    end;

  if FThemeHandle = 0 then
    if ThemesOk then
    begin
      FThemeWindow := TargetControl.Handle;
      FThemeHandle := OpenThemeData(FThemeWindow, 'STATUS');
    end;
end;

procedure TSizeGripThemed.SetThemed(const Value: boolean);
begin
  if FThemed <> Value then
  begin
    InvalidateGrip;
    FThemed := Value;
    CheckTheme;
    UpdateGrip;
  end;
end;

{ The new Window procedure for the attached target control.
}
procedure TSizeGripThemed.NewWndProc(var Msg: TMessage);
begin
  // $031A = WM_THEMECHANGED
  if Msg.Msg = $031A then
    if FThemeHandle <> 0 then
      begin
        CloseThemeData(FThemeHandle);
        FThemeHandle := 0;
        FThemeWindow := 0;
      end;

  inherited NewWndProc(Msg);
end;

{ Calculate the size grip's rectangle
}
procedure TSizeGripThemed.GetGripRect(var Rect: TRect);
var
  DC: HDC;
  Size, Size2: TSize;
begin
  CheckTheme;

  if FThemeHandle <> 0 then
  begin
    Size.cx := GetSystemMetrics(SM_CXVSCROLL);
    Size.cy := GetSystemMetrics(SM_CYHSCROLL);

    DC := GetDC(FThemeWindow);
    try
      // 3 = SP_GRIPPER
      if GetThemePartSize(FThemeHandle, DC, 3, 0, nil, TS_TRUE, Size2) = S_OK then
      begin
        if Size2.cx > Size.cx then
          Size.cx := Size2.cx;
        if Size2.cy > Size.cy then
          Size.cy := Size2.cy;
      end;
    finally
      ReleaseDC(FThemeWindow, DC);
    end;

    Rect := TargetControl.ClientRect;
    Rect.Left := Rect.Right - Size.cx;
    Rect.Top := Rect.Bottom - Size.cy;
  end
  else
    inherited GetGripRect(Rect);
end;

{ Paint the size grip
}
procedure TSizeGripThemed.PaintIt(DC: HDC; const Rect: TRect);
begin
  CheckTheme;

  // 3 = SP_GRIPPER
  if (FThemeHandle = 0) or
     (DrawThemeBackground(FThemeHandle, DC, 3, 0, Rect, @Rect) <> S_OK) then
    inherited PaintIt(DC, Rect);
end;

{ Register }

procedure Register;
begin
  RegisterComponents('System', [TSizeGripThemed]);
end;

end.
Flocke's Garage
Valid HTML 4.01 Transitional Valid CSS!
(C) 2005-2018 Volker Siebert.
Creative Commons-LizenzvertragDer gesamte Inhalt dieser Webseite steht unter einer Creative Commons-Lizenz (sofern nicht anders angegeben).