Link to home
Start Free TrialLog in
Avatar of ginsonic
ginsonicFlag for Romania

asked on

CombineRgn function

I wish to display a triangle in a picture (Image1 ) filled with Image2 .

How can I use CombineRgn to do that ?

I have , until now , this test code :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Image2: TImage;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var  bmp:TBitmap;
begin
bmp:=TBitmap.Create;
bmp.Height:=70;
bmp.Width:=70;

 combineRgn(bmp.Handle,Image2.Canvas.Handle,Image1.Canvas.Handle,RGN_AND);
 Image1.Canvas.Draw(0,0,bmp);
 Application.ProcessMessages;
end;

end.

 
ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ginsonic

ASKER

Can U send me a project file with this ?
I will increase the points to 150 .
My e-mail is nick@radioterra.ro
Thanks in advance.
Avatar of doncov
doncov

See example;
unit Unit1;

{The transparent form effect is done with Regions.
 First create a region that encompasses the entire form.
 Then, find the client area of the form (Client vs. non-Client) and
 combine with the full region with RGN_DIFF to make the borders
 and title bar visible.  Then create a region for each of the
 controls and combine them with the original (FullRgn) region.}

{From various posts in the newsgroups - based on some famous
 author I'm sure, but I first saw the post by Kerstin Thaler...}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Panel1: TPanel;
    Button2: TButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    { Private declarations }
    procedure DoVisible;
    procedure DoInvisible;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  FullRgn, ClientRgn, CtlRgn : THandle;

implementation

{$R *.DFM}

procedure TForm1.DoInvisible;
var
  AControl : TControl;
  A, Margin, X, Y, CtlX, CtlY : Integer;
begin
  Margin := ( Width - ClientWidth ) div 2;
  //First, get form region
  FullRgn := CreateRectRgn(0, 0, Width, Height);
  //Find client area region
  X := Margin;
  Y := Height - ClientHeight - Margin;
  ClientRgn := CreateRectRgn( X, Y, X + ClientWidth, Y + ClientHeight );
  //'Mask' out all but non-client areas
  CombineRgn( FullRgn, FullRgn, ClientRgn, RGN_DIFF );

  //Now, walk through all the controls on the form and 'OR' them
  // into the existing Full region.
  for A := 0 to ControlCount - 1 do begin
    AControl := Controls[A];
    if ( AControl is TWinControl ) or ( AControl is TGraphicControl )
        then with AControl do begin
      if Visible then begin
        CtlX := X + Left;
        CtlY := Y + Top;
        CtlRgn := CreateRectRgn( CtlX, CtlY, CtlX + Width, CtlY + Height );
        CombineRgn( FullRgn, FullRgn, CtlRgn, RGN_OR );
      end;
    end;
  end;
  //When the region is all ready, put it into effect:
  SetWindowRgn(Handle, FullRgn, TRUE);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //Clean up the regions we created
  DeleteObject(ClientRgn);
  DeleteObject(FullRgn);
  DeleteObject(CtlRgn);
end;

procedure TForm1.DoVisible;
begin
  //To restore complete visibility:
  FullRgn := CreateRectRgn(0, 0, Width, Height);
  CombineRgn(FullRgn, FullRgn, FullRgn, RGN_COPY);
  SetWindowRgn(Handle, FullRgn, TRUE);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //We start out as a transparent form....
  DoInvisible;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  //This button just toggles between transparent and not trans..
  if Button1.Caption = 'Show Form' then begin
    DoVisible;
    Button1.Caption := 'Hide Form';
  end
  else begin
    DoInvisible;
    Button1.Caption := 'Show Form';
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  //Need to address the transparency if the form gets re-sized.
  //Also, note that Form1 scroll bars are set to VISIBLE/FALSE.
  //I did that to save a little coding here....
  if Button1.Caption = 'Show Form' then
    DoInvisible
  else
    DoVisible;
end;

end.
doncov, this is much better than the other question ;-) (you know, the one with the OEM  cursor). I have no example ready for Nick so he will surely welcome your contribution.


Ciao, Mike
I have this sample .
But I don't understand how work .
Can you help me with a sample for my case ?

Regards,
Nick
Help me ~75%  .
But I had a start point .

Thanks,
Nick
Thank you Nick. The B grading is totally correct. I din't expect to get any point here since doncov provided an example. Doncov would you like to share the points then tell me and I post a dummy question for you?

Ciao, Mike
The  doncov's  example don't help me .
I have this sample from UNDU . But :(

I use only this

  Rgn := CreateRectRgnIndirect(R);
  SelectClipRgn(bmp.Canvas.Handle,Rgn);
  DeleteObject(Rgn);

from your code , Lischke .
But if you wish to share is your choice , your points .  :)

Thanks again
Aha, well, thank you Nick, good to know...

Ciao, Mike