Delphi实现StringGrid的行列选择

2006-12-18 18:05

Delphi实现StringGrid的行列选择

//----------------------------------------------------------------------------//
//基础数据StringGrid的鼠标移动事件
//----------------------------------------------------------------------------//
procedure TfrmCKSJHD.sg_JCSJMouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
with Sender as TStringGrid do
begin
CurrentCol := GetColByCX(Sender as TStringGrid, X);
CurrentRow := GetRowByCY(Sender as TStringGrid, Y);
//当鼠标在标题行或序号列时,鼠标指针变成手形
if (CurrentCol = 0) or (CurrentRow = 0) then
begin
Cursor := crHandPoint;
end
//其他情况鼠标指针为默认
else Cursor := crDefault;
end;
end;

//----------------------------------------------------------------------------//
//基础数据StringGrid的行列选择
//----------------------------------------------------------------------------//
procedure TfrmCKSJHD.sg_JCSJMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
CurrentRect: TGridRect;
begin
with Sender as TStringGrid do
begin
//全选
if (CurrentCol = 0) and (CurrentRow = 0) then
begin
CurrentRect.Left := 1;
CurrentRect.Top := 1;
//CurrentRect.Right := ColCount;
CurrentRect.Right := ColCount - 1;
CurrentRect.Bottom := RowCount;
Selection := CurrentRect;
end
//选择一列
else if CurrentRow = 0 then
begin
CurrentRect.Left := CurrentCol;
CurrentRect.Top := 1;
CurrentRect.Right := CurrentCol;
CurrentRect.Bottom := RowCount;
Selection := CurrentRect;
end
//选择一行
else if CurrentCol = 0 then
begin
CurrentRect.Left := 1;
CurrentRect.Top := CurrentRow;
//CurrentRect.Right := ColCount;
CurrentRect.Right := ColCount - 1;
CurrentRect.Bottom := CurrentRow;
Selection := CurrentRect;
end;
end;
end;

     功能虽然实现了,但当选择一行或全选时,再滚动鼠标滚轮会出现“grid index out of range”错误,目前还不知道怎么解决。(已修正)
另外MouseDown事件不能改为Click事件,因为固定行和列不响应Click事件。

posted @ 2013-02-17 12:16  hanks  阅读(1129)  评论(0编辑  收藏  举报