I wanted to make a program that creates a region from a template, and then uses it to cut the window. Here is the program code:

unit Back; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) function RegionFromFile(pict: TPicture; backcolor: TColor): HRGN; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function (pict :TPicture; backcolor: TColor): HRGN; var rgn, resRgn: HRGN; x, y, xFirst: Integer; begin resRgn := CreateRectRgn(0, 0, 0, 0); for y := 0 to pict.Height - 1 do begin x := 0; while x < pict.Width do begin if (pict.Bitmap.Canvas.Pixels[x, y] <> backcolor) then begin xFirst := x; Inc(x); while (x < pict.Width) and (pict.Bitmap.Canvas.Pixels[x, y] <> backcolor) do Inc(x); rgn := CreateRectRgn(xFirst, y, x-1, y+1); CombineRgn(resRgn, resRgn, rgn, RGN_OR); DeleteObject(rgn); end; Inc(x); end; end; RegionFromPicture := resRgn; end; procedure TForm1.FormCreate(Sender: TObject); var pict: TPicture; begin pict := TPicture.Create; pict.LoadFromFile('back.png'); SetWindowRgn(Handle, RegionFromRicture(pict, RGB(255,255,255)), True); end; end. 

PS Template file is stored in back.png.

  • In the OnCreate handler, the prog will not draw anything, you need to write this code in the OnPaint handler, and it looks like it does not understand the png format, you need to use bmp. - DelphiM0ZG

1 answer 1

Firstly, it’s wrong (after a directive):

 function (pict :TPicture; backcolor: TColor): HRGN; 

You need to write this:

 function RegionFromFile(pict :TPicture; backcolor: TColor): HRGN; 

That is, the function name is forgotten. At the end of this function, you need to write its name correctly (or write Result):

 RegionFromFile := resRgn; 

The last line of the FormCreate handler:

 SetWindowRgn(Handle, RegionFromFile(pict, RGB(255,255,255)), True); 

Now I will try to find something else.

  • For some reason, the compiler, when an error occurs, places a cursor in this line: TForm1 = class (TForm) function RegionFromFile (pict: TPicture; backcolor: TColor): HRGN; // Here it says that procedure FormCreate (Sender: TObject); - delphikettle
  • one
    And, in general, the function RegionFromFile (pict: TPicture; backcolor: TColor): HRGN; no need to declare any sections, you can comment it out there. - DelphiM0ZG
  • I tried to compile - I do not plow, why have not figured it out yet. - DelphiM0ZG
  • The program is now launched, but the region did not !!! That is, the shape was both rectangular and remained !!! I have already tried other templates! But it still does not work !!! - delphikettle
  • one
    try to insert a region in OnFormShow or OnFormActivate, it doesn't seem to work on OnFormCreate, although I don’t remember exactly what was long ago - den94