各位高手请教下DBGrid用DBGrid1DrawColumnCell重画标题栏后,标题栏内容很容易就被覆盖掉,不再刷新了

木奇 2015-09-16 11:28:11
各位高手请教下DBGrid用DBGrid1DrawColumnCell重画标题栏后,打开文件对话框在标题栏上移动后,标题栏内容就被覆盖掉,不再显示了,或者程序移出屏幕一部分,标题栏内容也被覆盖掉一部分,试了下不使用DBGrid1DrawColumnCell重画不会被覆盖,请问有什么办法可以使标题内容不被覆盖掉或者覆盖后能自动刷新,谢谢?

正常显示:

被覆盖掉显示:


程序:
1) DBGrid显示内容通过ADOQuery查询表、DBGrid各列是程序动态创建的、DBGrid属性DefaultDrawing为true
2) DBGrid1DrawColumnCell重画代码:
void __fastcall TForm_CANMain::DBGrid_ListDrawColumnCell(TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
int widTitle_Draw,leftTitle_Draw,topTitle_Draw;
TRect rectAlarm_Draw;
AnsiString titleStr_Draw;
int penWidOld_Draw;
TBrushStyle bsOld_Draw;
TColor bColorOld_Draw,pColorOld_Draw,fColorOld_Draw;
TFontStyles fsOld_Draw;

//重画DBGrid标题栏
//if(Rect.Height()>16)
{
//保存画刷、画笔、字体样式
bsOld_Draw=this->DBGrid_List->Canvas->Brush->Style;
bColorOld_Draw=this->DBGrid_List->Canvas->Brush->Color;
pColorOld_Draw=this->DBGrid_List->Canvas->Pen->Color;
penWidOld_Draw=this->DBGrid_List->Canvas->Pen->Width;
fsOld_Draw=this->DBGrid_List->Canvas->Font->Style;
fColorOld_Draw=this->DBGrid_List->Canvas->Font->Color;

//根据列之位置设置标题内容
switch(Column->Index)
{
case 0:
titleStr_Draw="A";
break;
case 1:
titleStr_Draw="B";
break;
case 2:
titleStr_Draw="C";
break;
case 3:
titleStr_Draw="D";
break;
case 4:
titleStr_Draw="E";
break;
case 5:
titleStr_Draw="F";
break;
case 6:
titleStr_Draw="G";
break;
case 7:
titleStr_Draw="H";
break;
case 8:
titleStr_Draw="I";
break;
case 9:
titleStr_Draw="J1";
break;
case 10:
titleStr_Draw="J2";
break;
case 11:
titleStr_Draw="J3";
break;
case 12:
titleStr_Draw="J4";
break;
defalut:
titleStr_Draw=" ";
}//switch(Column->Index)

//设置DBGrid之画布——画刷及字体样式
this->DBGrid_List->Canvas->Brush->Style=bsClear;
this->DBGrid_List->Canvas->Font->Color=clWindowText;
this->DBGrid_List->Canvas->Font->Style=TFontStyles()<<fsBold;

//设置标题位置
widTitle_Draw=this->DBGrid_List->Canvas->TextWidth(titleStr_Draw);
leftTitle_Draw=Rect.left+Rect.Width()/2-widTitle_Draw/2;
if(Column->Index<9)
{
topTitle_Draw=12;
}
else
{
topTitle_Draw=25;

}//if(Column->Index<9)


//画各列标题
this->DBGrid_List->Canvas->TextOut(leftTitle_Draw,topTitle_Draw,titleStr_Draw);

if(Column->Index==9)
{
//设置DBGrid之画布——画刷及画笔样式
this->DBGrid_List->Canvas->Brush->Style=bsSolid;
this->DBGrid_List->Canvas->Brush->Color=clBtnFace;
this->DBGrid_List->Canvas->Pen->Width=2;
this->DBGrid_List->Canvas->Pen->Color=0xACA899;

//画矩形框并写标题内容
rectAlarm_Draw=TRect(Rect.left-1,-1,Rect.right+Rect.Width()*3+4,topTitle_Draw-3);
this->DBGrid_List->Canvas->Rectangle(rectAlarm_Draw);
widTitle_Draw=this->DBGrid_List->Canvas->TextWidth(titleStr_Draw);
this->DBGrid_List->Canvas->TextOut(Rect.left+Rect.Width()*1.3-widTitle_Draw/2,2," J 列");

}//if(Column->Index==9)

//恢复画刷、画笔、字体样式
this->DBGrid_List->Canvas->Brush->Style=bsOld_Draw;
this->DBGrid_List->Canvas->Brush->Color=bColorOld_Draw;
this->DBGrid_List->Canvas->Pen->Color=pColorOld_Draw;
this->DBGrid_List->Canvas->Pen->Width=penWidOld_Draw;
this->DBGrid_List->Canvas->Font->Style=fsOld_Draw;
this->DBGrid_List->Canvas->Font->Color=fColorOld_Draw;

}//if(Rect.Height()>40)

//this->DBGrid_List->DefaultDrawColumnCell(Rect, DataCol, Column, State);
}
...全文
254 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
缘中人 2015-09-16
  • 打赏
  • 举报
回复
要扑捉窗体的重绘消息进行重绘这个grid cell应该就可以
木奇 2015-09-16
  • 打赏
  • 举报
回复
引用 11 楼 ksrsoft 的回复:
试试重载

--h
void __fastcall WndProc(Messages::TMessage &msg);

--cpp
void __fastcall TForm3::WndProc(Messages::TMessage &msg)
{
	if (msg.Msg == WM_ERASEBKGND)
	{
		this->DBGrid_List->Invalidate();
	}
	TForm::WndProc(msg);
}
WM_ERASEBKGND消息改成WM_PAINT消息闪烁明显小了,虽然不知道什么原因, 谢谢ksrsoft!!!
木奇 2015-09-16
  • 打赏
  • 举报
回复
引用 12 楼 flying0302 的回复:
[quote=引用 11 楼 ksrsoft 的回复:] 试试重载

--h
void __fastcall WndProc(Messages::TMessage &msg);

--cpp
void __fastcall TForm3::WndProc(Messages::TMessage &msg)
{
	if (msg.Msg == WM_ERASEBKGND)
	{
		this->DBGrid_List->Invalidate();
	}
	TForm::WndProc(msg);
}
加上是会刷新了,但不进行任何操作明显能看到DBGrid标题栏在闪烁[/quote] 可以了,闪烁是我程序多加了界面刷新的代码, 去掉后,只是拖动对话框时会闪烁,应该是重绘产生的吧,非常感谢!
木奇 2015-09-16
  • 打赏
  • 举报
回复
引用 11 楼 ksrsoft 的回复:
试试重载

--h
void __fastcall WndProc(Messages::TMessage &msg);

--cpp
void __fastcall TForm3::WndProc(Messages::TMessage &msg)
{
	if (msg.Msg == WM_ERASEBKGND)
	{
		this->DBGrid_List->Invalidate();
	}
	TForm::WndProc(msg);
}
加上是会刷新了,但不进行任何操作明显能看到DBGrid标题栏在闪烁
缘中人 2015-09-16
  • 打赏
  • 举报
回复
试试重载

--h
void __fastcall WndProc(Messages::TMessage &msg);

--cpp
void __fastcall TForm3::WndProc(Messages::TMessage &msg)
{
	if (msg.Msg == WM_ERASEBKGND)
	{
		this->DBGrid_List->Invalidate();
	}
	TForm::WndProc(msg);
}
木奇 2015-09-16
  • 打赏
  • 举报
回复
引用 9 楼 ksrsoft 的回复:
自绘太麻烦,省点事,有现成的拿来用。
第三方软件还要装,而且代码已经基本写完了,DBGrid很多相关的都是代码动态创建的或动态设置的, 换第三方软件太麻烦了,最好有个什么事件触发到让我能调用DBGrid的DrawColumnCell重画事件,再刷新一次就好了
缘中人 2015-09-16
  • 打赏
  • 举报
回复
自绘太麻烦,省点事,有现成的拿来用。
缘中人 2015-09-16
  • 打赏
  • 举报
回复
DbGrid本身没有此功能。我倒是觉得这个用第三方很方便,拿来使用即可。
木奇 2015-09-16
  • 打赏
  • 举报
回复
引用 6 楼 ksrsoft 的回复:
gridEh第三方组件有此功能。 multititile=true; titlelines=2 DBGridEh.Columns[0].Title.Caption := '办公用品|代码 '; DBGridEh.Columns[1].Title.Caption := '办公用品|名称 '; http://blog.csdn.net/xiongmao000738/article/details/6774587
有没有不用装第三方软件就能实现的方法啊?
缘中人 2015-09-16
  • 打赏
  • 举报
回复
gridEh第三方组件有此功能。 multititile=true; titlelines=2 DBGridEh.Columns[0].Title.Caption := '办公用品|代码 '; DBGridEh.Columns[1].Title.Caption := '办公用品|名称 '; http://blog.csdn.net/xiongmao000738/article/details/6774587
木奇 2015-09-16
  • 打赏
  • 举报
回复
引用 3 楼 ksrsoft 的回复:
你为啥要重绘,直接给列标题复制不可以吗
最后四列需要分成两栏,J列下面有J1、J2、J3、J4四项,所以要重绘,没办法直接设置Title->Caption
缘中人 2015-09-16
  • 打赏
  • 举报
回复
这样也可以啊
	this->ADOQuery1->Open();
	char tname[13][3] = {"A","B","C","D","E","F","G","H","I","J1","J2","J3","J4"	};
	for (int i = 0; i < DBGrid_List->Columns->Count; i++)
	{
		DBGrid_List->Columns->Items[i]->Title->Caption = tname[i];
	}
缘中人 2015-09-16
  • 打赏
  • 举报
回复
你为啥要重绘,直接给列标题复制不可以吗
木奇 2015-09-16
  • 打赏
  • 举报
回复
引用 1 楼 ksrsoft 的回复:
要扑捉窗体的重绘消息进行重绘这个grid cell应该就可以
请教如何扑捉窗体的重绘消息啊? DBGrid的DrawColumnCell事件好像只在ADOQuery->Open()后才被触发啊,谢谢?

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧