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?
 
LischkeConnect With a Mentor Commented:
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
 
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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
All Courses

From novice to tech pro — start learning today.