1、在stringgrid中,如果单元格列数超出控件宽度范围就会出现横向滚动条,但是如果我把滑块拉到最右边,
然后移动光标条查看上一行或下一行(属性值为行选择,edit跟随),滑块自动回到最左边,而不能保持原来的位
置,这个处理起来有点麻烦。2、用一个edit跟随,滚动滚动条后,想编辑后面的列,edit不能永远跟随,也就是不能编辑滚动条后边的列,这怎么处理?
然后移动光标条查看上一行或下一行(属性值为行选择,edit跟随),滑块自动回到最左边,而不能保持原来的位
置,这个处理起来有点麻烦。2、用一个edit跟随,滚动滚动条后,想编辑后面的列,edit不能永远跟随,也就是不能编辑滚动条后边的列,这怎么处理?
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, ExtCtrls, ImgList, AppEvnts;type
TForm1 = class(TForm)
Button2: TButton;
Panel1: TPanel;
strgrid: TStringGrid;
ComboBox1: TEdit;
ApplicationEvents1: TApplicationEvents;
procedure FormCreate(Sender: TObject);
procedure ComboBox11Exit(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure strgridKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure ComboBox1Exit(Sender: TObject);
procedure strgridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure strgridSelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
sACol,sARow : Integer;
Flag : Boolean;implementationuses Unit2;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
combobox1.visible := false;
strgrid.Enabled := false;
end;procedure TForm1.ComboBox11Exit(Sender: TObject);
begin
with sender as TCombobox do
begin
hide;
if text <> '' then
with strgrid do
cells[ col, row ] := text;
end;
end;procedure TForm1.Button2Click(Sender: TObject);
var
i : Integer;
begin
sACol := 1;
sARow := 1;
strgrid.Enabled := True;
strgrid.ColCount := 6;
strgrid.RowCount := 15;
for i := 0 to strgrid.RowCount - 1 do
strgrid.Rows[i].Clear;
strgrid.Cells[0,0] := '项目\样品';
strgrid.Cells[1,0] := 'a';
strgrid.Cells[2,0] := 'b';
strgrid.Cells[3,0] := 'c';
strgrid.Cells[4,0] := 'd';
strgrid.Cells[5,0] := 'e';
strgrid.Cells[0,1] := 'fe';
strgrid.Cells[0,2] := 'zn';
strgrid.Cells[0,3] := 'al';
strgrid.Cells[0,4] := 'na';
end;procedure TForm1.FormShow(Sender: TObject);
begin
ComboBox1.Color := clHighlight;
ComboBox1.Font.Color := clHighlightText ;
Button2Click(sender);
ComboBox1.Color := clWindow;
ComboBox1.Font.Color := clblue ;
end;procedure TForm1.strgridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
k : word;
arow,acol : Integer;
begin
k:=0;
strgrid.MouseToCell(x,y,acol,arow);
//如果不是标题行
if arow <> 0 then
strgrid.Row := arow;
//如果不是标题列
if acol <> 0 then
strgrid.Col := acol;
sarow := arow;
sacol := acol;
//如果不是选择标题fixedrows ,并且不超过有效行数(目前是4行)
if (sacol = 0) or (sarow > 4) then
begin
combobox1.Visible := False;
Exit;
end;
strgridKeyDown(strgrid,k,[]);
end;procedure TForm1.strgridKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
r : trect;
begin
if (key <> VK_RIGHT) and
(key<> VK_DOWN) and
(key<> VK_UP) and
(key <> VK_LEFT) and
(key <> 0) then Exit; With strgrid Do
begin
//如果不是选择标题fixedrows
If (sarow >= FixedRows) Then
Begin
r := CellRect(sacol,sarow ); //移动滚动条
if r.Right = 0 then
strgrid.ScrollBy(-80,0); With combobox1 do
begin
setbounds( r.Left + strgrid.Left ,r.Top + strgrid.Top, r.right-r.left, height );
combobox1.Text := strgrid.Cells[sACol,sARow];
Show;
BringTofront;
//使得ComboBox称为输入的焦点
SetFocus;
end;
End;
end;
end;procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
{ ADOTab.Post;
}
with sender as TEdit do
begin
strgrid.cells[sACol, sARow ] := text;
end;
end;
在Delphi中没有可以直接编辑的风格控件吗?
晕。
代码如下:
//自己写的一段
procedure TForm1.DrawSelect(sg:TObject;arow,acol:Integer);
var
i,j : Integer;
r,rect : TRect;
begin
iSelectRow := arow;
sarow := arow;
sacol := acol;
try
With sg as TStringGrid do
begin
//Row := arow;
For i := 1 to RowCount - 1 do
begin
if i = arow then
begin
Canvas.Font.Color := clHighlightText;
Canvas.Brush.Color := clHighlight;
end
else
begin
Canvas.Font.Color := clBlack;
Canvas.Brush.Color := clWhite;
end; For j := 1 to ColCount - 1 do
begin
rect := CellRect(j,i);
Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, cells[j, i]);
Canvas.FrameRect(Rect);
Application.ProcessMessages ;
end; R := CellRect(acol,arow );
With combobox1 do
begin
setbounds( r.Left + strgrid.Left + 1 ,
r.Top + strgrid.Top + 1, r.right-r.left,
r.Bottom - r.Top );
combobox1.Text := strgrid.Cells[sACol,sARow];
combobox1.Visible := True;
BringTofront;
//使得ComboBox称为输入的焦点
SetFocus;
end;
end;
end;
except
end;
Application.ProcessMessages ;
end;//重新画stringgrid内容
procedure TForm1.strgridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
var
CellString : String;
begin
//设行列标题居中显示
if (ARow < strgrid.FixedRows) or (ACol < strgrid.FixedCols) then
begin
CellString := strgrid.Cells[ACol, ARow];
strgrid.Canvas.TextRect(rect, rect.left + (rect.right - rect.left - strgrid.Canvas.TextWidth(CellString)) div 2,
rect.top + (rect.bottom - rect.top - strgrid.Canvas.TextHeight(CellString)) div 2, CellString);
end; Try
if iSelectRow < 1 then
Exit;
With Sender as TStringGrid do
begin
if (arow = 0) or (acol = 0) then
Exit;
if iSelectRow = arow then
begin
Canvas.Font.Color := clHighlightText;
Canvas.Brush.Color := clHighlight;
end
else
begin
Canvas.Font.Color := clBlack;
Canvas.Brush.Color := clWhite;
end; Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, cells[acol, arow]);
Canvas.FrameRect(Rect);
end;
Except
End;
Application.ProcessMessages ;
end;procedure TForm1.strgridSelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
CanSelect := True;
DrawSelect(Sender,arow,ACol);
end;//鼠标点击stringgrid单元格时触发
procedure TForm1.strgridMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Try
strgrid.MouseToCell(x,y,sacol,sarow);
if (sacol = 0) or (sarow=0) then
Exit;
strgrid.Row := sarow;
strgrid.Col := sacol;
DrawSelect(strgrid,sarow,sacol);
repaint;
Except
End;
end;//stringgrid中控制编辑框edit的跟随
procedure TForm1.strgridKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
r : TRect;
begin
Try
if (Key <> VK_DOWN) and (Key <> VK_UP) and
(Key <> VK_LEFT) and (Key <> VK_RIGHT) then Exit;
//键盘方向键下移,向下换行
ComboBox1Exit(ComboBox1);
if key= VK_DOWN then
begin
if strgrid.Row < strgrid.Rowcount - 1 then
strgrid.Row := strgrid.Row+1
else
strgrid.Row:=1;
sARow := strgrid.Row;
end;
//键盘方向键上移,向上换行
if key = VK_UP then
begin
if strgrid.Row > 1 then
strgrid.Row := strgrid.Row - 1
else
strgrid.Row := strgrid.Rowcount - 1;
sARow := strgrid.Row;
end;
//键盘方向键左移,向左换行
if key = VK_LEFT then
begin
if strgrid.Col > 1 then
strgrid.Col := strgrid.Col - 1
else
strgrid.Col := strgrid.ColCount - 1;
sACol := strgrid.Col;
sARow := strgrid.Row;
end;
//键盘方向键右移,向右换行
if key = VK_RIGHT then
begin
if strgrid.Col < strgrid.ColCount - 1 then
strgrid.Col := strgrid.Col + 1
else
strgrid.Col := 1;
sACol := strgrid.Col;
sARow := strgrid.Row;
end;
//DrawSelect(strgrid,sarow,sACol);
rePaint;
r := strgrid.CellRect(sACol,sARow);
strgridMouseDown(strgrid,mbLeft,[],r.Left + 2,r.Top + 2);
Except
End;
end;procedure TForm1.FormPaint(Sender: TObject);
begin
if iSelectRow < 1 then
Exit;
DrawSelect(strgrid,strgrid.Row,strgrid.Col );
end;
希望大虾能继续,效果能更好哦!我是尽力了.....惭愧哦