C++ Builder中在TPanel上画图

由于TPanel没有Canvas属性,属于其自身维护了其绘制的功能,要在其上画图,需要重载它的WM_PAINT消息。

//.h文件
class   TForm1   :   public   TForm 
{ 
__published:	//   IDE-managed   Components 
    TPanel   *Panel1; 
    TButton   *Button1; 
private:	//   User   declarations 
    TWndMethod   FPanelWndProc; 
    void   __fastcall   PanelWndProc(TMessage&   Message); 
public:	 //   User   declarations 
    __fastcall   TForm1(TComponent*   Owner); 
    virtual   __fastcall   ~TForm1(); 


}; 


//,cpp文件
__fastcall   TForm1::TForm1(TComponent*   Owner) 
    :   TForm(Owner) 
{ 
  //   remember   the   panel 's   original   window   procedure 
  FPanelWndProc   =   Panel1-> WindowProc; 
  //   subclass   the   panel 
  Panel1-> WindowProc   =   PanelWndProc; 
} 
//--------------------------------------------------------------------------- 
__fastcall   TForm1::~TForm1() 
{ 
//   restore   the   panel 's   original   window   procedure 
  Panel1-> WindowProc   =   FPanelWndProc; 
  FPanelWndProc   =   NULL; 
} 
//--------------------------------------------------------------------------- 

void   __fastcall   TForm1::PanelWndProc(TMessage&   Message) 
{ 
    FPanelWndProc(Message); 
    //   do   your   own   painting 
  if   (Message.Msg   ==   WM_PAINT) 
  { 
//        Graphics::TBitmap *bit = new Graphics::TBitmap;
//	bit->Assign(Image1->Picture->Graphic);
//	HDC hDC = GetDC(Panel1->Handle);
//	StretchBlt(hDC,0,0,Panel1->Width,Panel1->Height,bit->Canvas->Handle,
//		0,0,bit->Width,bit->Height,SRCCOPY);
//	delete bit;


            float   Step; 
            TRect   BandRect; 
            //A   panel   doesn 't   have   a   canvas   property.   Create   one. 
            TControlCanvas   *PanelCanvas=new   TControlCanvas; 
            PanelCanvas-> Control=Panel1; 

            Step=(float)Panel1-> ClientHeight/256;               //256   bandes   de   couleur 
            for(int   i=0;i <256;i++) 
            { 
                BandRect.Left=0; 
                BandRect.Top=(int)(i*Step); 
                BandRect.Right=Panel1-> ClientWidth+1; 
                BandRect.Bottom=(int)((i+1)*Step);                 //   bandes   horizontales 
                PanelCanvas-> Brush-> Color=RGB(0,0,255-i);   //   bandes   bleues 
                PanelCanvas-> FillRect(BandRect); 
            } 
            delete   PanelCanvas; 
  } 
} 

转载于:https://www.cnblogs.com/luoshupeng/archive/2011/04/14/2016573.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值