用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧编程其他

C++BUILDER中一些实现界面效果的技巧。。不规则窗体等等。。

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 19:40:03

以下源码是快速创建不规则窗体的,还可以移动。
file://------------------------------------------------------------------------
file://Unit1.h

#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


file://unit1.cpp
file://---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#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://---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{

BmpToRgn();
}
file://---------------------------------------------------------------------------

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[0][0];
int x,y;
int l,r;
POINT *a;
bool lb,rb;
HRGN WndRgn,TempRgn;

if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL)
{
  ShowMessage("申请内存失败!");
  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--;
}

file://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);
}

void __fastcall TForm1::Open1Click(TObject *Sender)
{
  if(OpenPictureDialog1->Execute())
  {
      Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
      BmpToRgn();
  }
}
file://---------------------------------------------------------------------------

对啦,图片的背景色要相同。
图片在IMAGE1的属性里加,不是在运行期通过LoadFromFile()加的。。

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);
    }
}

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;

  ::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;

  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);


  delete rctFrame;
  delete rctClient;
  delete lpTL,lpBR;

}


Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:30,734.38000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号