Z
zlc_168
Unregistered / Unconfirmed
GUEST, unregistred user!
先制作一张图片,背景色为纯色,然后在用以下代码想将窗口制作成图片前景形状,但
运行后,创建出的为空白区域,什么都没有,请各位大侠指正:
unit creatergn;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
procedure FormPaint(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
procedure drawrgn;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
pd:boolean=false;
implementation
{$R *.dfm}
procedure TForm1.drawrgn;
var
temprgn1,temprgn2:hrgn;
x,y,s:integer;
ocl:tcolor;
cc:boolean;
begin
cc:=true; //判断是否为初次创建区域;
ocl:=getpixel(canvas.Handle,0,0); //取得背景颜色
temprgn1:=0;
temprgn2:=0;
try
for y:=0 to height - 1 do
begin
x:=1;
while x<width do
begin
while (x<width) and (getpixel(canvas.Handle,x,y)=ocl) do inc(x); //如果和背景色相同,则递增
s:=x; //将图象开始坐标赋予S
while (x<width) and (getpixel(canvas.Handle,x,y)<>ocl) do inc(x); // 如果和背景色相同,则递增
if cc then //如果是初次,则创建区域1
begin
temprgn1:=CreateRectRgn(s,y,x,y);
cc:=false;
end
else //如果不是初次,则创建区域2
begin
temprgn2:=createrectrgn(s,y,x,y);
end;
CombineRgn(temprgn1,temprgn1,temprgn2,RGN_OR);//合并区域1 和区域2
inc(x);
end;
end;
finally
setwindowrgn(handle,temprgn1,true);
DeleteObject(temprgn1);
DeleteObject(temprgn2);
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
bmp:tbitmap;
begin
bmp:=tbitmap.Create;
try
bmp.LoadFromFile('temp.bmp');
canvas.Draw(0,0,bmp);
height:=bmp.Height;
width:=bmp.Width;
finally
bmp.Free;
end;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button=mbRight then
close;
if (button=mbLeft) and (not pd) then //按下鼠标左键,开始画区域,并切判断是否已经绘制
begin
drawrgn;
pd:=true;
end;
end;
end.
运行后,创建出的为空白区域,什么都没有,请各位大侠指正:
unit creatergn;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
procedure FormPaint(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
procedure drawrgn;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
pd:boolean=false;
implementation
{$R *.dfm}
procedure TForm1.drawrgn;
var
temprgn1,temprgn2:hrgn;
x,y,s:integer;
ocl:tcolor;
cc:boolean;
begin
cc:=true; //判断是否为初次创建区域;
ocl:=getpixel(canvas.Handle,0,0); //取得背景颜色
temprgn1:=0;
temprgn2:=0;
try
for y:=0 to height - 1 do
begin
x:=1;
while x<width do
begin
while (x<width) and (getpixel(canvas.Handle,x,y)=ocl) do inc(x); //如果和背景色相同,则递增
s:=x; //将图象开始坐标赋予S
while (x<width) and (getpixel(canvas.Handle,x,y)<>ocl) do inc(x); // 如果和背景色相同,则递增
if cc then //如果是初次,则创建区域1
begin
temprgn1:=CreateRectRgn(s,y,x,y);
cc:=false;
end
else //如果不是初次,则创建区域2
begin
temprgn2:=createrectrgn(s,y,x,y);
end;
CombineRgn(temprgn1,temprgn1,temprgn2,RGN_OR);//合并区域1 和区域2
inc(x);
end;
end;
finally
setwindowrgn(handle,temprgn1,true);
DeleteObject(temprgn1);
DeleteObject(temprgn2);
end;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
bmp:tbitmap;
begin
bmp:=tbitmap.Create;
try
bmp.LoadFromFile('temp.bmp');
canvas.Draw(0,0,bmp);
height:=bmp.Height;
width:=bmp.Width;
finally
bmp.Free;
end;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if button=mbRight then
close;
if (button=mbLeft) and (not pd) then //按下鼠标左键,开始画区域,并切判断是否已经绘制
begin
drawrgn;
pd:=true;
end;
end;
end.