DBGrid的控制(200分)

  • 主题发起人 主题发起人 spear
  • 开始时间 开始时间
S

spear

Unregistered / Unconfirmed
GUEST, unregistred user!
1 怎样将DBGrid的MultiSelect的热键改为F11
2 怎样在按F11选中记录时立即改变颜色,如红色(不用缺省的)
3 怎样改变焦点所在的cell的颜色
 
1. onkeydown事件中判断:
if key = VK_F11 then
if dgMultiSelect in dbgrid.options then
dbgrid.options := dbgrid.options - [dgMultiSelect]
else
dbgrid.options := dbgrid.options + [dgMultiSelect];

2, 3. OnDrawColumnCell中改变底色.
OnDrawCell中的state参数指定了当前cell的状态, 分别为
gdSelected The cell is currently selected.
gdFocused The cell has input focus.
gdFixed The cell is in the fixed region of the grid.
根据state画不同颜色的背景即可.
 
1,2 ???

3.下面两种方法可以使焦点所在cell有一个红色边框:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
(Sender as TDbGrid).Canvas.Font.Color := clRed ;
// (Sender as TDbGrid).Canvas.pen.Color := clRed ;
(Sender as TDbGrid).Canvas.brush.Color := clRed ;
if state=[gdFocused] then
(Sender as TDbGrid).DefaultDrawDataCell(Rect, Field ,State);
end;

或者:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
(Sender as TDbGrid).Canvas.Font.Color := clRed ;
// (Sender as TDbGrid).Canvas.pen.Color := clRed ;
(Sender as TDbGrid).Canvas.brush.Color := clRed ;
if state=[gdFocused] then
(Sender as TDbGrid).DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;
 
我还以为是要用F11代替鼠标健选择纪录呢. :-P
 
问题是不知怎样改变默认的兰色背景,Another_Eyes,cakk 有什么办法?
 
分太多了. :)
 
代替鼠标也可以: 一样在OnKeyDown里判断:
if key = VK_F11 then
dbgrid.selectedrows.currentrowselected := not dbgrid.selectedrows.currentrowselected;
 
>>改变默认的兰色背景,
蓝色背景其实就是select, 判断当时的state里面是否有gsSelect(大概这么写的),
如果有,就自己用别的颜色fillrect. 实在懒的写代码了. :-P
 
我早就试过fillrect的方法,不行:(

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin

(Sender as TDbGrid).Canvas.Font.Color := clRed ;
(Sender as TDbGrid).Canvas.pen.Color := clRed ;
(Sender as TDbGrid).Canvas.brush.Color := clRed ;
if state=[gdFocused] then
begin
(Sender as TDbGrid).Canvas.FillRect(rect);
(Sender as TDbGrid).DefaultDrawDataCell(Rect, Field ,State);
end;
end;
 
The OnDrawDataCell event is obsolete, and only included for backward compatibility.
用OnDrawColumnCell试试
 
if state=[gdFocused] then
改成
if gdselected in state then

不是跟你说了是select的吗? 不是focused!!
 
>> if state=[gdFocused] then
改成
if gdselected in state then

Cakk果然厉害! 试了一下,可以了.

不过 if gdFocused in state then 也同样可以,所以刚才关键是判断方法错误,
而不是 "select"和"focused"

正确方法:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
Field: TField; State: TGridDrawState);
begin
(Sender as TDbGrid).Canvas.Font.Color := clyellow ;
(Sender as TDbGrid).Canvas.brush.Color := clRed ;
if gdFocused in state then
begin
(Sender as TDbGrid).Canvas.FillRect(rect);
(Sender as TDbGrid).DefaultDrawDataCell(Rect, Field ,State);
end;
end;


BTW: Anothereyes>>用OnDrawColumnCell试试.
效果一样!

谢谢!

 
正常情况下,select和foucs是一样的,因为当前被select的肯定有foucs,
但是如果用程序来设置select或foucs,就会出现不一致的情况,毕竟
select和foucs并不一样.
 
if gdFocused in state 时肯定可能用FillRect,
但 gdSelected in state 时不行,我看过源代码,找不着这一块。

不知Another_eyes的办法对F11可行如否,但可能不会起作用
当时我在做时,发现只要离开一下本记录就可以了,不知
为何没有立即刷新,但用refresh/update/invalidate都不行.



 
>>if gdFocused in state 时肯定可能用FillRect,
但 gdSelected in state 时不行,我看过源代码,找不着这一块。

if gdSelected in state 也是可以的呀?


 
TStringGrid中有一个highlightcell,它主要是判断是否被选择
或是否有焦点来改变填充颜色,虽然它的焦点颜色怎样改变,我是
说在DefaultDrawing=true时.
 
我是指多重选择的时候,即gdMultiSelect in State时.
Another_eyes说的F11只能选中一行,离开当前选中的
记录时就不再被选中,过去我曾用过TList来存放书签,
但太复杂,现在想重新换过,请各位帮忙。
 
怎么不让DBGrid1中某不列接收双击。
表中某字段中值为true或为false,
我让它在DBgrid中显示为一个叉或
对号图形,但双击出现原字段值,我只
希望出现图形.每双击一次进行一次not
 
后退
顶部