注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

BCB-DG's Blog

...

 
 
 

日志

 
 

透明TPanel  

2014-04-03 11:00:26|  分类: Delphi |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
//轉
// 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:
    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:
public:
    __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* bmp = new Graphics::TBitmap;
    HDC dc = GetDC(Panel->Handle);
    try
    {
    bmp->Width = Panel->Width;
    bmp->Height = Panel->Height;
    if (bTransparent) // 設為透明
    {
        // 把Panel背後的圖Copy到Memory Bitmap上.
        DrawParentImage(Panel, bmp->Canvas);
    }
    else // 設為不透明
    {
        // 填滿Panel原始底色
        bmp->Canvas->Brush->Color = Panel->Color;
        bmp->Canvas->FillRect(Panel->ClientRect);
    }

    // 把Memory Bitmap 畫到Panel上.
    BitBlt(dc, 1, 1, Panel->Width - 2, Panel->Height - 2, bmp->Canvas->Handle, 1, 1, SRCCOPY);
    // 避免TGraphicControl類的元件被清掉
    for (int i = 0; i < Panel->ControlCount; i++)
    {
        if (Panel->Controls[i]->Visible)
        Panel->Controls[i]->Refresh();
    }
    }
    __finally
    {
    ReleaseDC(Panel->Handle, dc);
    delete bmp;
    }
}

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;
}
  评论这张
 
阅读(705)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017