分享
 
 
 

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

王朝c/c++·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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

王光红

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

一. 让PageControl的TabSheet“弹出来”

弹出来的效果

用过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->LineTo(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<TImageList*>( 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“画出”斑马线:

不同的颜色

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

void __fastcall TForm::StringGridDrawCell(TObject *Sender,

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

{

TStringGrid * SG = dynamic_cast<TStringGrid *>(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 时,您必须点击它的

,下拉菜单才会打开,有点不方便。笔者继承ComboBox,制作了一个命名Autocombobox的控件,当鼠标进入Autocombobox区域,它会自动打开下拉菜单。

代码如下:

.h

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

#ifndef AutoComboBoxH

#define AutoComboBoxH

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

#include <SysUtils.hpp>

#include <Classes.hpp>

#include <Controls.hpp>

#include <StdCtrls.hpp>

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

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 <vcl.h>

#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.x<r.right && pos.y >r.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.x<r.right && pos.y >r.top && pos.y < r.bottom)){

this->DroppedDown=false;

Timer->Enabled=false;

}

msg.Result = true;

break;

}

case WM_DESTROY:

{

break;

}

}

OldComboBoxWP(msg);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有