以前,程序員在DOS平台下開發應用軟件時,都會利用DOS的開放性特點,通過中斷編寫出一些特殊的顯示效果,以此來給自己的程序增加一些有趣的特色。 然而,隨著面向Windows的編程和可視化的第四代開發工具的逐步使用,人們越來越被Windows的統一的編程風格所束縛,同時由於Windows直接管理中斷,想用原來的方法來實現一些特殊的顯示效果變得越來越困難了。
那麼,在Windows時代程序員如何來實現窗口的特殊效果呢?實際上,由於整個Windows是圖形顯示系統,因此對系統而言,用戶程序的每一個界面、窗口都是系統的一個畫布。程序員可以利用這一特點來實現窗口的特殊效果。
最近筆者利用C++ Builder 5.0 Enterprise成功的實現了該項功能,現介紹如下:
1、在BCB中建立一個工程文件project1.dpr,在Form1中添加如下控件:
控件名稱 屬性 值
TForm Color clBTnText
TMainMenu Name MainMneu1
TImage Name Image1
Align alClient
AutoSize True
同時在 MainMenu1控件中增加一個菜單項,其屬性為:
Name Caption
Items1 圖像從中間往左右分出
Items2 圖像從左右往中間合進
Items3 圖像從中間往上下分出
Items4 圖像從上下往中間合進
Items5 圖像從左移入
Items6 圖像從右移入
Items7 圖像從上移入
Items8 圖像從下移入
Items9 圖像從左刷屏顯示
Items10 圖像從右刷屏顯示
Items11 圖像從上刷屏顯示
Items12 圖像從下刷屏顯示
Items13 圖像從上往下流水
Items14 圖像從下往上流水
Items15 圖像圓形展出
Items16 圖像左右錯移
Items17 圖像上下錯移
同時在Unit.cpp中的private中添加如下變量:
private:
BITMAP bm;
Graphics::TBitmap *Bitmap1;
int i,j;
int WideHalf;
TRect Dest1,Source1,Dest2,Source2;
int HeightHalf;
HRGN MyRgn;
2、在Form1的OnCreate事件中添加如下代碼:
void __fastcall TForm1::FormCreate(Tobject *Sender)//進行圖像特效顯示的初始化操作// { Graphics::Tbitmap *Bitmap1 = new Graphics::Tbitmap();//定義一個圖象句柄// Bitmap1->LoadFromFile("C:\\factory.bmp");//設定所要特效顯示的圖象為factory.bmp// if(GetDeviceCaps(Form1->Canvas->Handle,BITSPIXEL)==8)//取得顯示窗口的顏色屬性// { GetObject(Bitmap1->Handle,sizeof(BITMAP),(LPSTR)&bm); if (bm.bmBitsPixel==8)//如果特效顯示的圖象顏色屬性為256色// { SelectPalette(Form1->Canvas->Handle,Bitmap1->Palette,FALSE);//從特效顯示的顏色畫板中提取配色方案// RealizePalette(Form1->Canvas->Handle); //在顯示窗口中設置配色方案// } Bitmap->HandleType=bmDDB; } }
3、在Items1的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item1Click(TObject *Sender)
{ //圖象從中間往左右分出//
Form1->Refresh( );
WideHalf=Bitmap1->Width/2+Bitmap1->Width%2;
for(i=0;i<=WideHalf; i++)
{
Dest1=Rect(WideHalf-i,0,WideHalf+i,Bitmap1->Height);
Source1=Rect(WideHalf-i,0,WideHalf+i,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);//圖像的延遲顯示//
}
}
4、在Items2的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item2Click(TObject *Sender)
{ //圖像從左右往中間合進//
Form1->Refresh( );
WideHalf=Bitmap1->Width/2+Bitmap1->Width%2;
Form1->Refresh( );
for(i=0;i<=WideHalf;i++)
{
Source1=Dest1=Rect(0,0,i,Bitmap1->Height);
Source2=Dest2=Rect(Bitmap1->Width-i,0,Bitmap->Width,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest1,Bimap1->Canvas,Source1);
Form1->Canvas->CopyRect(Dest2,Bitmap1->Canvas,Source2);
for(j=0;j<10000;j++);
}
}
5、在Items3的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item3Click(TObject *Sender)
{ //圖像從中間往上下分出//
Form1->Refresh( );
HeightHAlf=Bittmap1->Height/2+Bitmap1->Height%2;
for(i=0;i<=HeightHalf;i++)
{
Dest1=Rect(0,HeightHalf-i,Bitmap1->Width,HeightHalf);
Source1=Rect(0,0,BmpHandle->Width,i);
Dest2=Rect(0,HeightHaf,Bitmap1->Width,HeightHalf+i);
Source2=Rect(0,Bitmap1->Height-i,Bitmap1->Width,Bitmap1->height);
Form1->Canvas->CopyRect(Dest1,BmpHandle->Canvas,Source1);
Form1->Canvas->CopyRect(Dest2,BmpHandle->Canvas,Source2);
for(j=0;j<10000;j++);
}
}
6、在Items4的OnClieck事件中添加如下代碼:void__fastcall TForm1::Item4Click(TObject *Sender)
{ //圖像從上下往中間合進//
Form1->Refresh( );
HeightHalf=Bitmap1->Height/2+Bitmap1->Height%2;
for(i=0;i<=HeightHalf;i++)
{
Dest1=Rect(0,0,Bitmap1->Width,i);
Source1=Rect(0,HeightHalf-i,Bitmap1->Width,HeightHalf);
Dest2=Rect(0,Bitmap1->Height-i,
Bitmap1->Width,Bitmap1->Height);
Source2=Rect(0,HeightHalf,Bitmap1->Width,HeightHalf+i);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
Form1->Canvas->CopyRect(Dest2,Bitmap1->Canvas,Source2);
for(j=0;j<10000;j++);
}
}
7、在Items5的OnClieck事件中添加如下代碼:void__fastcall TForm1::Item5Click(TObject *Sender)
{ //圖像從左移入//
Form1->Refresh( );
for(i=Bitmap1->Width;i>=0;i--)
{
Dest1=Rect(0,0,Bitmap1->Width-i,Bitmap1->Height);
Source1=Rect(i,0,Bitmap1->Width,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
8、在Items6的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item6Click(TObject *Sender)
{ //圖像從右移入//
Form1->Refresh( );
for(i=0;i<=Bitmap1->Width;i++)
{
Dest1=Rect(Bitmap1->Width-i,0,Bitmap1->Width,
Bitmap1->Height);
Source1=Rect(0,0,i,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
9、在Items7的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item7Click(TObject *Sender)
{ //圖像從上移入//
Form1->Refresh( );
for(i=0;i<=Bitmap1->Height;i++)
{
Dest1 =Rect(0,0,Bitmap1->Width,i);
Source1 =Rect(0,Bitmap1->Height-i,Bitmap1->Width,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
10、在Items8的OnClieck事件中添加如下代碼:void__fastcall TForm1::Item8Click(TObject *Sender)
{ //圖像從下移入//
Form1->Refresh( );
for(i=0;i<=Bitmap1->Height;i++)
{
Dest1 =Rect(0,Bitmap1->Height-i,Bitmap1->Width,
Bitmap1->Height);
Source1 =Rect(0,0,Bitmap1->Width,i);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
11、在Items9的OnClieck事件中添加如下代碼:void__fastcall TForm1::Item9Click(TObject *Sender)
{ //圖像從左刷屏顯示//
Form1->Refresh( );
for(i=0;i<=Bitmap1->Width;i++)
{
Dest1 =Rect(0,0,i,Bitmap1->Height);
Source1 =Rect(0,0,i,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
12、在Items10的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item10Click(TObject *Sender)
{ //圖像從右刷屏顯示//
Form1->Refresh( );
for(i=Bitmap1->Width;i>=0;i--)
{
Dest1 =Rect(i+0,0,0+Bitmap1->Width,0+Bitmap1->Height);
Source1 =Rect(i,0,Bitmap1->Width,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
13、在Items11的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item11Click(TObject *Sender)
{ //圖像從上刷屏顯示//
Form1->Refresh( );
for(i=0;i<=Bitmap1->Height;i++)
{
Dest1 =Rect(0,0,Bitmap1->Width+0,i+0);
Source1 =Rect(0,0,Bitmap1->Width,i);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
14、在Items12的OnClieck事件中添加如下代碼:void__fastcall TForm1::Item12Click(TObject *Sender)
{ //圖像從下刷屏顯示//
Form1->Refresh( );
for(i=Bitmap1->Height;i>=0;i--)
{
Dest1 =Rect(0,i+0,Bitmap1->Width+0,Bitmap1->Height+0);
Source1 =Rect(0,i,Bitmap1->Width,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
15、在Items13的OnClieck事件中添加如下代碼:void__fastcall TForm1::Item13Click(TObject *Sender)
{ //圖像從上往下流水//
Form1->Refresh( );
for(i=Bitmap1->Height;i>=0;i--)
{
Source1 =Rect(0,i,Bitmap1->Width,i+1);
for(int j1=0;j1<=i;j1++)
{
Dest1 =Rect(0 ,j1,Bitmap1->Width+0,j+1);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
}
16、在Items14的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item14Click(TObject *Sender)
{ //圖像從下往上流水//
Form1->Refresh( );
for(i=0;i<=Bitmap1->Height;i++)
{
Source1 =Rect(0,i,Bitmap1->Width,i+1);
for(int j1=Bitmap1->Height-1;j1>=i;j1--)
{
Dest1 =Rect(0,j1,Bitmap1->Width,j1+1);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
for(j=0;j<10000;j++);
}
}
}
17、在Items15的OnClieck事件中添加如下代碼:void__fastcall TForm1::Item15Click(TObject *Sender)
{ //圖像圓形展出//
Form1->Refresh( );
int j1=sqrt(Bitmap1->Width*Bitmap1->Width+
Bitmap1->Height*Bitmap1->Height)/2+4;
for (i=1;i
{
MyRgn=CreateEllipticRgn(Bitmap1->Width/2-i,
Bitmap1->Height/2-i,
Bitmap1->Width/2+i,
Bitmap1->Height/2+i);
SelectObject(Canvas->Handle,MyRgn);
Canvas->Draw(0,0,Bitmap1);
DeleteObject(MyRgn);
for(j=0;j<10000;j++);
}
}
18、在Items16的OnClieck事件中添加如下代碼:void__fastcall TForm1::Item16Click(TObject *Sender)
{ //圖像左右錯移//
Form1->Refresh( );
HeightHalf=Bitmap1->Height/2+Bitmap1->Height%2;
Source1=Rect(0,0,Bitmap1->Width,HeightHalf);
Source2=Rect(0,HeightHalf,Bitmap1->Width,Bitmap1->Height);
for(i=0;i<=Bitmap1->Width;i++)
{
Dest1=Rect(0-Bitmap1->Width+i,0,i,0+HeightHalf);
Dest2=Rect(Bitmap1->Width-i,HeightHalf,
Bitmap1->Width+Bitmap1->Width-i,
Bitmap1->Height+0);
Form1->Canvas->MoveTo(0-Bitmap1->Width+i-1,0);
Form1->Canvas->LineTo(0-Bitmap1->Width+i-1,HeightHalf);
Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1);
Form1->Canvas->MoveTo(
Bitmap1->Width+Bitmap1->Width-i,0);
Form1->Canvas->LineTo(
Bitmap1->Width+Bitmap1->Width-i,Bitmap1->Height);
Form1->Canvas->CopyRect(Dest2,Bitmap1->Canvas,Source2);
for(j=0;j<10000;j++);
}
}
19、在Items17的OnClieck事件中添加如下代碼:
void__fastcall TForm1::Item17Click(Tobject *Sender) { //圖像上下錯移// Form1->Refresh( ); WideHalf=Bitmap1->Width/2+Bitmap1->Width%2; Source1=Rect(0,0,WideHalf,Bitmap1->Height); Source2=Rect(WideHalf,0,Bitmap1->Width,Bitmap1->Height); for(i=0;i<=Bitmap1->Height;i++) { Dest1=Rect(0,0-Bitmap1->Height+I,WideHalf,0+i); Dest2=Rect(WideHalf,Bitmap1->Height-I,Bitmap1->Width, Bitmap1->Height+Bitmap1->Top); //上面這條代碼可能有錯。當時有亂碼。:( Form1->Canvas->MoveTo(0,0-Bitmap1->Height+i-1); Form1->Canvas->LineTo(WideHalf,0-Bitmap1->Height+i-1); Form1->Canvas->CopyRect(Dest1,Bitmap1->Canvas,Source1); Form1->Canvas->MoveTo(WideHalf,Bitmap1->Height+Bitmap1->Height-i); Form1->Canvas->LineTo(Bitmap1->Width,Bitmap1->Height+Bitmap1->Height-i); Form1->Canvas->CopyRect(Dest2,Bitmap1->Canvas,Source2); for(j=0;j<10000;j++); } }
以上程序在C++ Builder5.0Enterpriseh和Win98/WinME通過。