完善Borland C++ Builder可视化控件功能三例 (转)

完善Borland C++ Builder可视化控件功能三例 (转)[@more@]

完善Borland C++ Builder可视化控件功能三例

王光红XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

笔者在编程中积累了点滴经验,与各位交流,共同提高。

一.  PageControlTabSheet“弹出来”

弹出来的效果

ASPectratio="t"> CSDN_Dev_Image_a%20href=2003-7-71422180.png" o:title="">

用过PageControl的人知道,PageControl之ActivePage的标头较“平坦”,没有“弹出”的立体效果,笔者经过改进达到了“弹出”的效果。

在OnDrawTab事件中加入以下代码:

 

void __fastcall TsqlBuilderForm::PageControl1DrawTab(

  TCustomTabControl *Control, int TabIndex, const TRect &Rect,

  bool Active)

{

  Byte Red0,Red1,Green0,Green1,Blue0,Blue1;

 

  if(Active)

  {

 

  Red0 = 0;

  Red1 = 15;

  Green0 = 55;

  Green1 = 200;

Blue0 = 135; 

Blue1 = 240;

 

  }

 else

  {

  Red0 = 51;

  Red1 = 101;

  Green0 = 91;

  Green1 = 200;

  Blue0 = 91;

  Blue1 = 200;

  }

 

 

  int h;

  h = Rect.Bottom - Rect.Top - 2;

  Byte clr,clg,clb;

  int clTmp;

  int i = 0;

  float c=0.4; //亮的位置

  SetBkMode(Control->Canvas->Handle,TRANSPARENT);

 

  for(; i< h * c; ++i)

  {

  clTmp = Red0 + floor((double)i / double( h ) * (double)(Red1 - Red0) + 0.5) / c;

  if(clTmp < 0 ) clTmp  = 0 ;

  else if(clTmp > 255) clTmp  = 255;

  clr = Byte(clTmp);

 

  clTmp = Green0 + floor((double)i / double( h ) * (double)(Green1 - Green0) + 0.5) / c;

  if(clTmp < 0 ) clTmp  = 0 ;

  else if(clTmp > 255) clTmp  = 255;

  clg = Byte(clTmp);

 

  clTmp = Blue0 + floor((double)i / double( h ) * (double)(Blue1 - Blue0) + 0.5) / c;

  if(clTmp < 0 ) clTmp  = 0 ;

  else if(clTmp > 255) clTmp  = 255;

  clb = Byte(clTmp);

 

  Control->Canvas->Pen->Color = TColor(RGB(clr,clg,clb));

  Control->Canvas->MoveTo(Rect.Left  + 1,Rect.Top + i + 1 );

  Control->Canvas->L.NETo(Rect.Right - 2,Rect.Top + i + 1 );

  }

 

  for( ; i< h ; ++i)

  {

  clTmp = Red0 + floor((double)(h-i) / double( h ) * (double)(Red1 - Red0) + 0.5);

  if(clTmp < 0 ) clTmp  = 0 ;

  else if(clTmp > 255) clTmp  = 255;

  clr = Byte(clTmp);

 

  clTmp = Green0 + floor((double)(h-i) / double( h ) * (double)(Green1 - Green0) + 0.5);

  if(clTmp < 0 ) clTmp  = 0 ;

  else if(clTmp > 255) clTmp  = 255;

  clg = Byte(clTmp);

 

  clTmp = Blue0 + floor((double)(h-i) / double( h ) * (double)(Blue1 - Blue0) + 0.5);

  if(clTmp < 0 ) clTmp  = 0 ;

  else if(clTmp > 255) clTmp  = 255;

  clb = Byte(clTmp);

  Control->Canvas->Pen->Color = TColor(RGB(clr,clg,clb));

  Control->Canvas->MoveTo(Rect.Left  + 1,Rect.Top + i + 1 );

  Control->Canvas->LineTo(Rect.Right - 2,Rect.Top + i + 1 );

  }

  AnsiString str = PageControl1->Pages[TabIndex]->Caption;

 

  int offset=0;

  if(PageControl1->Images!=NULL && ! Active ){

  Graphics::TBitmap * bmp = new Graphics::TBitmap;

  TImageList * img =dynamic_cast( PageControl1->Images);

  img->GetBitmap(PageControl1->Pages[TabIndex]->ImageIndex,bmp);  Control->Canvas->Draw(Rect.Left+2,Rect.Top+1,bmp);

  offset= bmp->Width-10;

  delete bmp; bmp = NULL;

  }

 

 

 

Control->Canvas->TextOut( Rect.Left +offset + (Rect.Right - Rect.Left - Control->Canvas->TextWidth(str)) / 2,

  Rect.Top + (Rect.Bottom - Rect.Top - Control->Canvas->TextHeight(str)) / 2,

  str);

 

}

 

 

 

二.  StringGrid“画出”斑马线:

 

不同的颜色

CSDN_Dev_Image_2003-7-71422180.png

StringGrid的每一行都是一样的颜色,Cell多了容易看错位,我们可以在OnDrawCell事件中把相邻的Row之间“画出”不同的色彩,让人一目了然,不再会看花了眼。

 

void __fastcall TForm::StringGridDrawCell(Tobject *Sender,

  int ACol, int ARow, TRect &Rect, TGridDrawState State)

{

TStringGrid * SG = dynamic_cast(Sender);

 

 if(SG){

 try

  {

  if(ARow>=SG->FixedRows && ACol>=SG->FixedCols)

  {

  if(ARow%2 == 0)

  {

  SG->Canvas->Brush->Color=(TColor) 0x00E6FADC;

  }

  else

  {

  SG->Canvas->Brush->Color=(TColor)0x00FBEFD5;

  }

  }

  else{

  SG->Canvas->Font->Color = clBlack;

  SG->Canvas->Brush->Color=(TColor)0x00DDDDDD;

  }

 

  SG->Canvas->FillRect(Rect);

  DrawText(SG->Canvas->Handle, SG->Cells[ACol][ARow].c_str(), -1, (RECT*)&Rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);

  }

  catch(Exception &exception)

  {

   Application->ShowException(&exception);

  return ;

  }

  }

 

}

 

三.  ComboBox自动拉开菜单

使用ComboBox 时,您必须点击它的CSDN_Dev_Image_2003-7-71422184.png,下拉菜单才会打开,有点不方便。笔者继承ComboBox,制作了一个命名Autocombobox的控件,当鼠标进入Autocombobox区域,它会自动打开下拉菜单。

代码如下:

 

.h

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

 

#ifndef AutoComboBoxH

#define AutoComboBoxH

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

#include

#include

#include

#include

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

class PACKAGE TAutoComboBox : public TComboBox

{

private:

  TTimer *Timer;

  RECT r;

  void __fastcall NewComboBoxWP(TMessage &Msg);

  void __fastcall TimerTimer(TObject *Sender);

  void __fastcall (__closure *OldComboBoxWP)(TMessage &Message);

 

protected:

public:

  __fastcall TAutoComboBox(TComponent* Owner);

 

  __fastcall ~TAutoComboBox(void);

__published:

};

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

#endif

 

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

 

 

 

.cpp

#include

 

#pragma hdrstop

 

#include "AutoComboBox.h"

#pragma package(smart_init)

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

// ValidCtrCheck is used to assure that the components created do not have

// any pure virtual functions.

//

 

static inline void ValidCtrCheck(TAutoComboBox *)

{

  new TAutoComboBox(NULL);

}

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

__fastcall TAutoComboBox::TAutoComboBox(TComponent* Owner)

  : TComboBox(Owner)

{

  Timer = new TTimer(this);

  Timer->Enabled=false;

  Timer->Interval=200;

  Timer->OnTimer=TimerTimer;

  OldComboBoxWP = this->WindowProc;

  this->WindowProc = NewComboBoxWP;

}

 

__fastcall TAutoComboBox::~TAutoComboBox(void)

{

delete Timer;

}

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

namespace Autocombobox

{

  void __fastcall PACKAGE Register()

  {

  TComponentClass classes[1] = {__classid(TAutoComboBox)};

  RegisterComponents("WghSoft", classes, 0);

  }

}

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

 

void __fastcall TAutoComboBox::TimerTimer(TObject *Sender)

{

  POINT pos;

  GetCursorPos(&pos);

  if(!(pos.x>r.left && pos.xr.top && pos.y < r.bottom)){

  this->DroppedDown=false;

  Timer->Enabled=false;

  }

}

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

 

void __fastcall TAutoComboBox::NewComboBoxWP(TMessage& msg)

{

  switch (msg.Msg)

  {

  case CM_MOUSEENTER:

  {

  this->DroppedDown=true;

  msg.Result = true;

  break;

  }

  case CM_MOUSELEAVE:

  {

  POINT pos;

  Timer->Enabled=true;

  GetCursorPos(&pos);

  this->Perform(CB_GETDROPPEDCONTROLRECT,0,LPARAM(&r));

  if(!(pos.x>r.left && pos.xr.top && pos.y < r.bottom)){

  this->DroppedDown=false;

  Timer->Enabled=false;

  }

  msg.Result = true;

  break;

  }

  case WM_DESTROY:

  {

 

   break;

  }

  }

  OldComboBoxWP(msg);

}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-958785/,如需转载,请注明出处,否则将追究法律责任。

user_pic_default.png
请登录后发表评论 登录
全部评论
<%=items[i].createtime%>

<%=items[i].content%>

<%if(items[i].items.items.length) { %>
<%for(var j=0;j
<%=items[i].items.items[j].createtime%> 回复

<%=items[i].items.items[j].username%>   回复   <%=items[i].items.items[j].tousername%><%=items[i].items.items[j].content%>

<%}%> <%if(items[i].items.total > 5) { %>
还有<%=items[i].items.total-5%>条评论 ) data-count=1 data-flag=true>点击查看
<%}%>
<%}%>
<%}%>

转载于:http://blog.itpub.net/10748419/viewspace-958785/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值