在Form上添加一個(gè)OpenPictureDialog,添加一個(gè)Image,并為其添加一個(gè)圖片。再加一個(gè)PopupMenu,并創(chuàng)建兩個(gè)菜單項(xiàng),一個(gè)是Open1,一個(gè)是Exit1,其中前者是打開圖象文件對(duì)話框,后者為退出程序。設(shè)置Image1的PopupMenu屬性為PopupMenu1。
在Form1的OnCreate事件中添加:
void __fastcall TForm1::FormCreate(TObject *Sender)
{
BmpToRgn();
}
在.h文件中的private段中添加
void __fastcall TForm1::BmpToRgn();
這個(gè)函數(shù)用來創(chuàng)建不規(guī)則窗體。
//---------------------------------------------------------------------------
void __fastcall TForm1::BmpToRgn()
{
Image1->AutoSize=true;
Form1->AutoSize=true;
Form1->BorderStyle=bsNone; //將Form的標(biāo)題欄去掉
TColor ColorKey=Image1->Canvas->Pixels[0][0]; //以這個(gè)點(diǎn)的顏色為透明基準(zhǔn)色
int x,y;
int l,r;
POINT *a;
bool lb,rb;
HRGN wndrgn,temprgn;
if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)
{
ShowMessage("申請(qǐng)內(nèi)存失敗!");
exit(0);
}
l=0;r=Image1->Height*2-1;
wndrgn=CreateRectRgn(0,0,Image1->Width,Image1->Height);
for(y=0;y<Image1->Height;y++)
{
lb=true;
for(x=0;x<Image1->Width+1;x++)
if(Image1->Canvas->Pixels[x][y]!=ColorKey)
{
a[l].x=x;
a[l].y=y;
lb=false;
break;
}
if(lb) a[l]=a[l-1];
l++;
rb=true;
for(x=Image1->Width;x>=0;x--)
if(Image1->Canvas->Pixels[x][y]!=ColorKey)
{
a[r].x=x;
a[r].y=y;
rb=false;
break;
}
if(rb) a[r]=a[r+1];
r--;
}
r=Image1->Height*2-1;
for(y=0;y<Image1->Height-1;y++)
{
for(x=a[y].x;x<=a[r].x;x++)
if(Image1->Canvas->Pixels[x][y]==ColorKey)
{
temprgn=CreateRectRgn(x,y,x+1,y+1);
CombineRgn(wndrgn,wndrgn,temprgn,RGN_XOR);
DeleteObject(temprgn);
}
r--;
}
//temprgn=CreatePolygonRgn(a,Image1->Height*2,ALTERNATE);
temprgn=CreatePolygonRgn(a,Image1->Height*2,WINDING);
CombineRgn(wndrgn,wndrgn,temprgn,RGN_AND);
DeleteObject(temprgn);
delete a;
SetWindowRgn(Handle,wndrgn,true);
}
為了使沒有標(biāo)題欄的窗體能移動(dòng),需要在Image1的OnMouseDown事件中添加以下代碼:
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Button == mbLeft)
{
ReleaseCapture();
Perform(WM_NCLBUTTONDOWN,HTCAPTION, 0);
}
}
//---------------------------------------------------------------------------
//通過右鍵菜單中的Exit1退出程序
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
//打開圖片文件,并以這個(gè)圖片創(chuàng)建不規(guī)則窗體
void __fastcall TForm1::Open1Click(TObject *Sender)
{
if(OpenPictureDialog1->Execute())
{
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
BmpToRgn();
}
}
注意圖片的背景色要相同。
posted on 2008-08-18 18:19
cpsprogramer 閱讀(1804)
評(píng)論(2) 編輯 收藏 引用 所屬分類:
VC++