天天看点

BCB中使TPanel透明

/*
           
从一个论坛发现的代码, 用代码是TPanel透明, 而不是重新写一个组件.
           
*/
           
// Unit1.h



//---------------------------------------------------------------------------



#ifndef Unit1H

#define Unit1H

//---------------------------------------------------------------------------

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

#include <Forms.hpp>

#include <ComCtrls.hpp>

#include <ExtCtrls.hpp>

#include <Buttons.hpp>

//---------------------------------------------------------------------------

class TForm1 : public TForm

{

__published:        // IDE-managed Components

    TButton *Button2;

    TButton *Button3;

    TImage *Image2;

    TPanel *Panel1;

    TLabel *Label1;

    TImage *Image1;

    TButton *Button1;

    TEdit *Edit1;

    TCheckBox *CheckBox1;

    void __fastcall Button2Click(TObject *Sender);

    void __fastcall Button3Click(TObject *Sender);

private:        // User declarations

public:                // User declarations

    __fastcall TForm1(TComponent* Owner);

    void __fastcall NewPanelWndProc(Messages::TMessage &Message);

};

//---------------------------------------------------------------------------

extern PACKAGE TForm1 *Form1;

//---------------------------------------------------------------------------

#endif



//---------------------------------------------------------------------------

//unit1.cpp



#include <vcl.h>

#pragma hdrstop



#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

TWndMethod PanelWndProc;

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

    : TForm(Owner)

{

    Button2->Caption = "設為透明";

    Button3->Caption = "設為不透明";

    Button2->Enabled = TRUE;

    Button3->Enabled = FALSE; 

}

//---------------------------------------------------------------------------

void DrawParentImage(TControl* Control, TCanvas* DestCanvas)

{

    int iSaveIndex;

    TPoint Position;



    if (!Control->Parent)

    return;



    iSaveIndex = SaveDC(DestCanvas->Handle);

    GetViewportOrgEx( DestCanvas->Handle, &Position);

    SetViewportOrgEx( DestCanvas->Handle, Position.x - Control->Left,

        Position.y - Control->Top, NULL);

    IntersectClipRect(DestCanvas->Handle, 0, 0, Control->Parent->ClientWidth,

        Control->Parent->ClientHeight);

    Control->Parent->Perform(WM_ERASEBKGND, (int)DestCanvas->Handle, 0);

    Control->Parent->Perform(WM_PAINT, (int)DestCanvas->Handle, 0);

    RestoreDC(DestCanvas->Handle, iSaveIndex);

}



void SetPanelTransparent(TPanel* Panel, BOOL bTransparent)

{

    Graphics::TBitmap* MemoryBitmap = new Graphics::TBitmap;

    try

    {

    MemoryBitmap->Width = Panel->Width;

    MemoryBitmap->Height = Panel->Height;

    

    if (bTransparent) // 設為透明

    {

        // 把Panel背後的圖Copy到Memory Bitmap上.

        DrawParentImage(Panel, MemoryBitmap->Canvas);

    }

    else // 設為不透明

    {

        // 填滿Panel原始底色

        MemoryBitmap->Canvas->Brush->Color = Panel->Color;

        MemoryBitmap->Canvas->FillRect(Panel->ClientRect);

    }



    // 把Memory Bitmap 畫到Panel上.

    BitBlt( GetDC(Panel->Handle), 1, 1, Panel->Width - 2, Panel->Height - 2,

        MemoryBitmap->Canvas->Handle, 0, 0, SRCCOPY);



    // 避免TGraphicControl類的元件被清掉

    for (int i = 0; i < Panel->ControlCount; i++)

    {

        if (Panel->Controls[i]->Visible)

        Panel->Controls[i]->Refresh();

    }

    }

    __finally

    {

    delete MemoryBitmap;

    }

}



void __fastcall TForm1::NewPanelWndProc(Messages::TMessage &Message)

{

    PanelWndProc(Message);

    if (Message.Msg == WM_PAINT) // 攔截Panel的WM_PAINT, 並把背景畫上.

    SetPanelTransparent(Form1->Panel1, TRUE);

}



void __fastcall TForm1::Button2Click(TObject *Sender)

{

    // 設為透明

    // Subclass Panel1以處理WM_PAINT

    PanelWndProc = Panel1->WindowProc;

    Panel1->WindowProc = NewPanelWndProc;

    Panel1->Refresh();



    Button2->Enabled = FALSE;

    Button3->Enabled = TRUE;

}



void __fastcall TForm1::Button3Click(TObject *Sender)

{

    // 設為不透明

    // 還原Panel1原始的訊息迴圈

    Panel1->WindowProc = PanelWndProc;

    Panel1->Refresh();

    

    Button2->Enabled = TRUE;

    Button3->Enabled = FALSE; 

}



           

继续阅读