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.

 
LVL 9
ginsonicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LischkeCommented:
Hi ginsonic,

regions are really easy once you know those few little tricks. For the CombineRgn you need to pass regions not device contexts. Hence your call cannot work. As you want to use CombineRgn I think you need the regions you want to combine separately too. I recommend that you first try rectangular regions as they are really easy to create:

var
  R: TRect;
  Rgn: HRGN;

begin
  R := Rect(20, 20, 120, 120);
  Rgn := CreateRectRgnIndirect(R);
  SelectClipRgn(bmp.Canvas.Handle, Rgn);
  DeleteObject(Rgn);
end;

You see here the rectangle (no need to explain that I think). From this a rectangular region is created and selected as new clip area in bmp. After the Select* call you don't need Rgn any longer (it is copied) and you can safely delete it. Now consider you want to make a triangular region. This is not much harder. You need to use CreatePolygonRgn (with tree points of course). Now select this into your bitmap to clip out all you don't want to see.

Is this explanation enough?

Ciao, Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ginsonicAuthor Commented:
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.
0
doncovCommented:
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.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

LischkeCommented:
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
0
ginsonicAuthor Commented:
I have this sample .
But I don't understand how work .
Can you help me with a sample for my case ?

Regards,
Nick
0
ginsonicAuthor Commented:
Help me ~75%  .
But I had a start point .

Thanks,
Nick
0
LischkeCommented:
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
0
ginsonicAuthor Commented:
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
0
LischkeCommented:
Aha, well, thank you Nick, good to know...

Ciao, Mike
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.