Subject Re: Creating a set of graphical UI component with GR32
From Ariel R. <ariel31@live.com>
Date Tue, 31 Mar 2009 10:37:53 +0200
Newsgroups graphics32.general

Hi,

Thanks for your answer, but it's possible to draw a transparent TImage32 on
a form by overriding the methode "ExecClearBackgnd".

Cheers,
Ariel

---------------------------------------------------------------------------------------------------------------------------------------------------------

Example below :

(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License
Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is GR_Sprites
*
* The Initial Developer of the Original Code is Riceball LEE
* Portions created by Riceball LEE are Copyright (C) 2004-2007
* All Rights Reserved.
*
* Contributor(s):
*   Michael Haralabos (Transparent TImage32 )
*   Riceball LEE(Mouse and keyboard events supports)
*
* ***** END LICENSE BLOCK ***** *)
unit GR_ImageEx;


interface

{$I GR32.inc}

uses
{$IFDEF CLX}
  Qt, Types, QControls, QGraphics, QForms, QConsts,
  {$IFDEF LINUX}Libc,{$ENDIF}
  {$IFDEF MSWINDOWS}Windows,{$ENDIF}
{$ELSE}
  Windows, Messages, Controls, Graphics, Forms,
  //Dialogs,
{$ENDIF}
  Classes, SysUtils,
  GR32_Image, GR32_Layers, GR32;

type
  TImage32Ex = class(TImage32)
  private
    FTransparent: Boolean;

    procedure SetTransparent(const Value: Boolean);
    procedure WMKillFocus(var Message: TMessage); message WM_KILLFOCUS;
  protected
    procedure WndProc(var Message: TMessage);override;
  public
    procedure ExecClearBackgnd(Dest: TBitmap32; StageNum: Integer);
override;
  published
    property Enabled;
    property Transparent: Boolean read FTransparent write SetTransparent;
  end;

procedure Register;

implementation

Type
  TLayerHack = class(TCustomLayer);
  TMyShiftState  = (ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle,
ssDouble);
  TMyShiftStates = set of TMyShiftState;

//Not double click in shift state
function IsMouseButtonDown(Shift: TMyShiftStates; Button: TMyShiftState):
Boolean;
begin
  Result := (Button in Shift) and not (ssDouble in Shift);
end;

procedure TImage32Ex.ExecClearBackgnd(Dest: TBitmap32; StageNum: Integer);
var
  P: TPoint;
  SaveIndex: Integer;
begin
  if FTransparent and Assigned(Parent) and
     not (Assigned(Bitmap) and (BitmapAlign = baTile)) then
  begin
    SaveIndex := SaveDC(Dest.Handle);
    GetViewportOrgEx(Dest.Handle, P);
    SetViewportOrgEx(Dest.Handle, P.X - Left, P.Y - Top, nil);
    IntersectClipRect(Dest.Handle, 0, 0, Parent.ClientWidth,
Parent.ClientHeight);
    Parent.Perform(WM_ERASEBKGND, Dest.Handle, 0);
    Parent.Perform(WM_PAINT, Dest.Handle, 0);
    RestoreDC(Dest.Handle, SaveIndex);
  end
  else
    inherited;
end;

procedure TImage32Ex.SetTransparent(const Value: Boolean);
begin
  if FTransparent <> Value then
  begin
    FTransparent := Value;
    Invalidate;
  end;
end;

procedure TImage32Ex.WMKillFocus(var Message: TMessage);
begin
  DoExit;
  inherited;
end;

procedure TImage32Ex.WndProc(var Message: TMessage);
begin
  case Message.Msg of
    WM_MOUSEFIRST..WM_MOUSELAST: if IsControlMouseMsg(TWMMouse(Message))
then
    begin
      //first pass it to self to prevent from be processed by children  .
      Dispatch(Message);
      //exit;
    end
    //else ShowMessage(IntToStr(TWMMouse(Message).XPos))
    ;
  end;

  inherited;

end;

procedure Register;
begin
  RegisterComponents('Graphics32', [TImage32Ex]);
end;

end.


"stevensmith" <noone@home.com> a écrit dans le message de groupe de
discussion : gqrg64$qki$1@news.graphics32.org...
>
> "Ariel R." <ariel31@live.com> wrote in message
> news:gqr2bt$hfp$1@news.graphics32.org...
>> Hi all,
>>
>> I would like to create a set of graphical UI components with GR32 that
>> can be used like any standard components in the VCL Editor (without
>> having to write any code) and that would support per-pixel alpha
>> blending, but I don't know what is the best way to do it.
>> Is that better to use the TCustomImage32 as the base of all components or
>> is that better to use the TBitmapLayer object instead and then paint on a
>> unique TCustomImage32 ?
>>
>> And by the way, is there any frameworks that look like what I would to do
>> ?
>>
>> Thanks :)
>>
>> Cheers,
>> Ariel
> Alpha blending will only work when drawing to timage32..you cant  make a
> timage32 transparent on the parent tform..to do that would mean blending
> the timage32 pixels with the underlying forms pixels which may be possible
> but not easily done..any components made from layers or tbitmap32 can only
> be drawn onto a timage32..anyone can correct me if i'm wrong but thats how
> i understand it.
>
>