StringGrid

如何在StringGrid当前行第一列也像DBGrid样标出箭头呢?
---------------------------------------------------------------

你可以从TStringGrid派生一个子类,在子类中当发生进入单元格事件时,在该行第0列自己显示一个箭头。
---------------------------------------------------------------

const
  MY_DRAWCELL = WM_USER + 10;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure MyDrawCell(var Msg: TMessage); message MY_DRAWCELL;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if ARow = TStringGrid(Sender).Row then
    PostMessage(Handle, MY_DRAWCELL, Rect.Top, 0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.DefaultRowHeight := 17;
  StringGrid1.ColWidths[0] := 11;
end;

procedure TForm1.MyDrawCell(var Msg: TMessage);
const
  {$J+}vWParam: DWORD = 0;{$J-}
begin
  with StringGrid1.Canvas, Msg do
  begin
    ///Begin 清除上一次的三角
    Pen.Color := clBtnFace;
    Brush.Color := clBtnFace;
    Polygon([
      Point(3, vWParam + 3),
      Point(8, vWParam + 8),
      Point(3, vWParam + 13)
    ]);
    ///End 清除上一次的三角

    Pen.Color := clBlack;
    Brush.Color := clBlack;
    Polygon([
      Point(3, WParam + 3),
      Point(8, WParam + 8),
      Point(3, WParam + 13)
    ]);
    vWParam := WParam;
  end;
end;

 


StringGrid方法补充
2007-07-23 17:33:21
删除选定行
Procedure DeleteRow(AGrid : TStringGrid);
var i, cr : integer;
begin
  If assigned(AGrid) then
  begin
    cr := AGrid.Selection.Top;
    for i := cr + 1 to AGrid.RowCount - 1 do
      AGrid.Rows[i-1].Assign(AGrid.Rows[i]);
    AGrid.RowCount := AGrid.RowCount - 1;
  end;
end;


2003-11-28 10:01:00     查看评语???    

 2003-11-28 10:56:22    保存StringGrid到html文件

procedure SaveToHtml(StringGrid:TStringGrid;const FileName : string;const Title : string);
var
  Txt : TextFile;
  i,ii: integer;
  Value:string;
  BgColor:TColor;
  function GetColor(Color: TColor): String;
  var s: String;
  begin
    if Color = clNone then
      s := '000000'
    else
      s := IntToHex(ColorToRGB(Color), 6);
    Result := Copy(s, 5, 2) + Copy(s, 3, 2) + Copy(s, 1, 2);
  end;
begin
  BgColor := clWhite;
  AssignFile(Txt,FileName);
  Rewrite(Txt);
  WriteLn(Txt,'<Title>' + Title + '</Title>');
  WriteLn(Txt,'<TABLE WIDTH=100% border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">');

  for i := 0 to StringGrid.RowCount - 1 do
  begin
    WriteLn(Txt,'<TR>');
    for ii := 0 to StringGrid.ColCount - 1 do
    begin
      Value := StringGrid.Cells[ii,i];
      if Value = '' then Value := '&nbsp;';
      if (ii < StringGrid.FixedCols) or (i < StringGrid.FixedRows) then
        BgColor := StringGrid.FixedColor
      else
        BgColor := StringGrid.Color;
      WriteLn(Txt,'<TD BGCOLOR="#' + GetColor(BgColor) + '"><font color="#' +
        GetColor(StringGrid.Font.Color) + '">' + Value + '</font></TD>')
    end;
    WriteLn(Txt,'</TR>');
  end;
  WriteLn(Txt,'</TABLE>');
  CloseFile(Txt);
end;

使用示例:
SaveToHtml(StringGrid1,'c:/1.html','标题');

 
 2003-11-28 13:51:20    高速排序函数(在StringGrid里加上5000行试试就知道它的效率了)
procedure Quicksort(Grid:TStringGrid; var List:array of integer;
    min, max,sortcol,datatype: Integer);
{List is a list of rownumbers in the grid being sorted}
var
  med_value : integer;
  hi, lo, i : Integer;

  function compare(val1,val2:string):integer;
  var
    int1,int2:integer;
    float1,float2:extended;
    errcode:integer;
  begin
    case datatype of
      0: result:=ANSIComparetext(val1,val2);
      1: begin
           int1:=strtointdef(val1,0);
           int2:=strtointdef(val2,0);
           if int1>int2 then result:=1
           else if int1<int2 then result:=-1
           else result:=0;
         end;

      2: begin
           val(val1,float1,errcode);
           if errcode<>0 then float1:=0;
           val(val2,float2,errcode);
           if errcode<>0 then float2:=0;
           if float1>float2 then result:=1
           else if float1<float2 then result:=-1
           else result:=0;
         end;
       else result:=0;
    end;
 end;

begin
  {If the list has <= 1 element, it's sorted}
  if (min >= max) then Exit;
  {Pick a dividing item randomly}
  i := min + Trunc(Random(max - min + 1));
  med_value := List[i];
  List[i] := List[min]; { Swap it to the front so we can find it easily}
  {Move the items smaller than this into the left
   half of the list. Move the others into the right}
  lo := min;
  hi := max;
  while (True) do
  begin
    // Look down from hi for a value < med_value.
    while compare(Grid.cells[sortcol,List[hi]]
                         ,grid.cells[sortcol,med_value])>=0 do
    (*ANSIComparetext(Grid.cells[sortcol,List[hi]]
                         ,grid.cells[sortcol,med_value])>=0 do*)
    begin
        hi := hi - 1;
        if (hi <= lo) then Break;
    end;
    if (hi <= lo) then
    begin {We're done separating the items}
      List[lo] := med_value;
      Break;
    end;

    // Swap the lo and hi values.
    List[lo] := List[hi];
    inc(lo); {Look up from lo for a value >= med_value}
    while Compare(grid.cells[sortcol,List[lo]],
             grid.cells[sortcol,med_value])<0 do
    begin
        inc(lo);
        if (lo >= hi) then break;
    end;
    if (lo >= hi) then
    begin  {We're done separating the items}
      lo := hi;
      List[hi] := med_value;
      break;
    end;
    List[hi] := List[lo];
  end;
  {Sort the two sublists}
  Quicksort(Grid,List, min, lo - 1,sortcol,datatype);
  Quicksort(Grid,List, lo + 1, max,sortcol,datatype);
end;

//datatype 0:按字符排序  1:按整型排序  2:按浮点型排序
procedure Sortgrid(Grid : TStringGrid; sortcol,datatype:integer);
var
   i : integer;
   tempgrid:tstringGrid;
   list:array of integer;
begin
  screen.cursor:=crhourglass;
  tempgrid:=TStringgrid.create(nil);
  with tempgrid do
  begin
    rowcount:=grid.rowcount;
    colcount:=grid.colcount;
    fixedrows:=grid.fixedrows;
  end;
  with Grid do
  begin
    setlength(list,rowcount-fixedrows);
    for i:= fixedrows to rowcount-1 do
    begin
      list[i-fixedrows]:=i;
      tempgrid.rows[i].assign(grid.rows[i]);
    end;
    quicksort(Grid, list,0,rowcount-fixedrows-1,sortcol,datatype);
    for i:=0 to rowcount-fixedrows-1 do
    begin
      rows[i+fixedrows].assign(tempgrid.rows[list[i]])
    end;
    row:=fixedrows;
  end;
  tempgrid.free;
  setlength(list,0);
  screen.cursor:=crdefault;
end;

使用方法:
procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  c:integer;
  w:integer;
  Grid:TStringGrid;
begin
  Grid := Sender as TStringGrid;
  with Grid do
  if y<=rowheights[0] then
  begin
    c:=0;
    w:=colwidths[0];
    while (c<colcount) and (w<=x) do
    begin
      inc(c);
      w:=w+colwidths[c]+gridlinewidth;
    end;
    sortgrid(Grid,c,0);
 end;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值