不规则窗口问题
加载窗口的事件中打如下代码,照书上打的,但只有一张图,没有什么不规则的,什么回事啊~
register int x,y;
int l,r;
POINT* a;
bool bcount,bheight;
HRGN wndrgn,trgn1,trgn2;
if((a=(POINT *)malloc(800*4*(sizeof(POINT))))==NULL)
{
MessageBox(Handle, "分配内存失败 ", "错误提示 ",MB_OK);
exit (0);
}
Width=Image1-> Width;
Height=Image1-> Height;
Repaint();
l=0;
r=Image1-> Height*2-1;
for(y=0;y <Image1-> Height;y++)
{
bcount=true;
for(x=0;x <Image1-> Width;x++)
if(Image1-> Canvas-> Pixels[x][y]!=clWhite)
{
a[l].x=x+1;
a[l].y=y;
bcount=false;
break;
}
if(bcount)
a[l]=a[l-1];
++l;
bheight=true;
for(x=Image1-> Width-1;x> =0;x--)
if(Image1-> Canvas-> Pixels[x][y]!=clWhite)
{
a[r].x=x;
a[r].y=y;
bheight=false;
break;
}
if(bheight)
a[r]=a[r+1];
--r;
}
r=Image1-> Height*2-1;
for(y=0;y <Image1-> Height;y++)
{
for(x=a[y].x;x <a[r].x;x++)
if(Image1-> Canvas-> Pixels[x][y]==clWhite)
{
trgn2=CreateRectRgn(x,y,x+1,y+1);
CombineRgn(wndrgn,wndrgn,trgn2,RGN_XOR);
DeleteObject(trgn2);
}
--r;
}
trgn1=CreatePolygonRgn(a,Image1-> Height*2,ALTERNATE);
DeleteObject(trgn1);
free(a);
SetWindowRgn(Handle,wndrgn,true);
SetWindowPos(Handle,HWND_TOP,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE);
}
[解决办法]
Image1里纯白的部分才会被扣掉,你确实你Image1里的有纯白的部分吗?
另外我觉得这段代码放在OnShow里比较好
[解决办法]
我自己做的没有问题
我是这样写的,
void __fastcall TForm3::FormCreate(TObject *Sender)
{
register int x, y;
register int Left,Right;
bool FlagLeft;
HRGN tepRgn,WndRgn;
Image1-> Picture-> LoadFromFile( "logo.bmp ");
Width = Image1-> Width;
Height = Image1-> Height;
WndRgn = CreateRectRgn(0,0,Width,Height);
for(y = 0; y < Height; y++)
{
FlagLeft = false;
for(x = 0; x < Width; x++)
{ //发现了线段左边的第一个白点
if(Image1-> Canvas-> Pixels[x][y] == clWhite && FlagLeft == false)
{
Left = x;
FlagLeft = true;
} //发现了线段右边的第一个白点
else if(Image1-> Canvas-> Pixels[x][y] != clWhite && FlagLeft == true)
{
Right = x;
tepRgn = CreateRectRgn(Left,y,Right,y+1);
CombineRgn(WndRgn,WndRgn,tepRgn,RGN_XOR);
DeleteObject(tepRgn);
FlagLeft = false;
}
}
if(FlagLeft == true && x > = Width)
{
tepRgn = CreateRectRgn(Left,y,x,y+1);
CombineRgn(WndRgn,WndRgn,tepRgn,RGN_XOR);
DeleteObject(tepRgn);
}
}
SetWindowRgn(Handle,WndRgn,true);
}