C++BUILDER中创建不规则窗体
<P>以下源码是快速创建不规则窗体的,还可以移动。file://------------------------------------------------------------------------
<a>file://Unit1.h</A></P>
<P>#ifndef Unit1H
#define Unit1H
file://------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
#include <Menus.hpp>
#include <Dialogs.hpp>
#include <ExtDlgs.hpp>
file://---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TImage *Image1;
TPopupMenu *PopupMenu1;
TMenuItem *Exit1;
TOpenPictureDialog *OpenPictureDialog1;
TMenuItem *Open1;
void __fastcall Exit1Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
void __fastcall Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall Open1Click(TObject *Sender);
private: // User declarations
void __fastcall BmpToRgn();
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
file://---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
file://---------------------------------------------------------------------------
#endif</P>
<P>
<a>file://unit1.cpp</A>
file://---------------------------------------------------------------------------</P>
<P>#include <vcl.h>
#pragma hdrstop</P>
<P>#include "Unit1.h"
file://---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
file://---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
file://---------------------------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender)
{
Close();
}
file://---------------------------------------------------------------------------</P>
<P>void __fastcall TForm1::FormCreate(TObject *Sender)
{</P>
<P>BmpToRgn();
}
file://---------------------------------------------------------------------------</P>
<P>void __fastcall TForm1::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Button == mbLeft)
{
ReleaseCapture();
Perform(WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
}
file://---------------------------------------------------------------------------
void __fastcall TForm1::BmpToRgn()
{
Image1->AutoSize=true;
Form1->AutoSize=true;
Form1->BorderStyle=bsNone;
TColor ColorKey=Image1->Canvas->Pixels;
int x,y;
int l,r;
POINT *a;
bool lb,rb;
HRGN WndRgn,TempRgn;</P>
<P>if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)
{
ShowMessage("申请内存失败!");
exit(0);
}</P>
<P>l=0;r=Image1->Height*2-1;
WndRgn=CreateRectRgn(0,0,Image1->Width,Image1->Height);</P>
<P>for(y=0;y<Image1->Height;y++)
{
lb=true;
for(x=0;x<Image1->Width+1;x++)
if(Image1->Canvas->Pixels!=ColorKey)
{
a.x=x;
a.y=y;
lb=false;
break;
}</P>
<P> if(lb) a=a;
l++;
rb=true;</P>
<P> for(x=Image1->Width;x>=0;x--)
if(Image1->Canvas->Pixels!=ColorKey)
{
a.x=x;
a.y=y;
rb=false;
break;
}
if(rb) a=a;
r--;
}</P>
<P>r=Image1->Height*2-1;
for(y=0;y<Image1->Height-1;y++)
{
for(x=a.x;x<=a.x;x++)
if(Image1->Canvas->Pixels==ColorKey)
{
TempRgn=CreateRectRgn(x,y,x+1,y+1);
CombineRgn(WndRgn,WndRgn,TempRgn,RGN_XOR);
DeleteObject(TempRgn);
}
r--;
}</P>
<P><a>Height*2,ALTERNATE">file://TempRgn=CreatePolygonRgn(a,Image1->Height*2,ALTERNATE</A>);
TempRgn=CreatePolygonRgn(a,Image1->Height*2,WINDING);
CombineRgn(WndRgn,WndRgn,TempRgn,RGN_AND);
DeleteObject(TempRgn);
delete a;
SetWindowRgn(Handle,WndRgn,true);
}</P>
<P>void __fastcall TForm1::Open1Click(TObject *Sender)
{
if(OpenPictureDialog1->Execute())
{
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
BmpToRgn();
}
}
file://---------------------------------------------------------------------------</P>
<P>对啦,图片的背景色要相同。
图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。</P>
<P>file://--------------------(一)为窗体背景增加渐变色(蓝)---------------
void __fastcall TForm1::FormPaint(省略)
{
AnsiString ww;int bb;
ww=AnsiString(Form1->Width/256);
TRect MyRect;
MyRect.left=0;
for(bb=0;bb<255;bb++)
{
Form1->Canvas->Brush->Color=(TColor)RGB(0,0,bb);
MyRect.top=bb*(ww.ToInt());
MyRect.right=Form1->Width;
MyRect.bottom=(bb+1)*(ww.ToInt());
Form1->Canvas->FillRect(MyRect);
}
} </P>
<P>file://---------------透明窗体--------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TRect *rctClient,*rctFrame;
HRGN hClient,hFrame;
POINT *lpTL,*lpBR;
rctFrame=new TRect;
rctClient=new TRect;
lpTL=new POINT;
lpBR=new POINT;
GetWindowRect(Form1->Handle,rctFrame);
::GetClientRect(Form1->Handle,rctClient);
lpTL->x=rctFrame->Left;lpTL->y=rctFrame->Top;
lpBR->x=rctFrame->Right;lpBR->y=rctFrame->Bottom;</P>
<P> ::ScreenToClient(Form1->Handle,lpTL);
::ScreenToClient(Form1->Handle,lpBR);
rctFrame->Left=lpTL->x;rctFrame->Top=lpTL->y;
rctFrame->Right=lpBR->x;rctFrame->Bottom=lpBR->y;
rctClient->Left=abs(rctFrame->Left);
rctClient->Top=abs(rctFrame->Top);
rctClient->Right=rctClient->Right+
abs(rctFrame->Left);
rctClient->Bottom=rctClient->Bottom+
abs(rctFrame->Top);
rctFrame->Right=rctFrame->Right+
abs(rctFrame->Left);
rctFrame->Bottom=rctFrame->Bottom+
abs(rctFrame->top);
rctFrame->Top=0;rctFrame->Left=0;</P>
<P> hClient=CreateRectRgn(rctClient->Left,rctClient->Top,
rctClient->Right,rctClient->Bottom);
hFrame=CreateRectRgn(rctFrame->left,rctFrame->Top,
rctFrame->Right,rctFrame->Bottom);
CombineRgn(hFrame,hClient,hFrame,RGN_XOR);
SetWindowRgn(Form1->Handle,hFrame,true);</P>
<P>
delete rctFrame;
delete rctClient;
delete lpTL,lpBR;</P>
<P>} </P>
页:
[1]