zoukankan      html  css  js  c++  java
  • 构造自己的StringGrid

    构造自己的StringGrid

    代码
    {*******************************************************}
    { }
    { Module Name: unitStringGrid.pas }
    { Author Ming.z }
    { Creation Date 2010-12-29 }
    { }
    {*******************************************************}
    unit unitStringGrid;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, Grids;

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

    TSGTitleEnum
    = (sgtID,sgtName,sgtAge,sgtSex,sgtAddr);
    TSGTitleSubRange
    = sgtID..sgtAddr;
    TSGRecord
    = record
    name:
    string;
    Word;
    end;

    const //5 col N row
    SGTitle:
    array [TSGTitleEnum] of TSGRecord
    //SGTitle: array [TSGTitleSubRange] of TSGRecord //same as above
    = ((name:'ID' ;40),
    (name:
    'Name' ;70),
    (name:
    'Age' ;45),
    (name:
    'Sex' ;45),
    (name:
    'Address' ;80));
    var
    Form1: TForm1;

    implementation
    {$R *.dfm}

    procedure TForm1.InitStringGridData;
    var
    i,j: Integer;
    begin
    with StringGrid1 do
    begin
    for i := 0 to ColCount - 1 do
    for j := 1 to RowCount - 1 do
    begin
    case i of
    Ord(sgtID):
    Cells[i,j] :
    = IntToStr(j);
    else
    Cells[i,j] :
    = Format('%d%d',[i,j]);
    end;
    end;
    end;
    end;

    procedure TForm1.InitStringGridParam;
    var
    i: Integer;
    lSG: TSGTitleSubRange;
    begin
    //for lSG in [Low(lSG)..High(lSG)] do //same as below
    for lSG in [Low(TSGTitleSubRange)..High(TSGTitleSubRange)] do
    begin
    i :
    = Ord(lSG);
    StringGrid1.ColWidths[i] :
    = SGTitle[lSG].width;
    StringGrid1.Cells[i,
    0] := SGTitle[lSG].name;
    end;
    InitStringGridData;
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    InitStringGridParam;
    end;

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
    begin
    with TStringGrid(sender).Canvas do
    begin
    if ARow = 0 then //Draw Title
    begin
    brush.color:
    =clTeal;
    FillRect(rect);
    Font.Style:
    =[fsBold];
    Font.Size:
    =10;
    Font.Color:
    =clYellow; //Grid font color
    OffsetRect(rect,
    1,1);
    DrawText(handle,PChar(TStringGrid(sender).cells[ACol,ARow])
    ,length(TStringGrid(sender).cells[ACol,ARow]),rect,DT_CENTER);
    end
    else
    case ACol of
    Ord(sgtID):
    begin
    //brush.color:=clBtnFace; //default
    brush.color:
    =clSkyBlue;
    FillRect(rect);
    Font.Style:
    =[fsBold];
    Font.Size:
    =9;
    Font.Color:
    =clRed; //Grid font color
    OffsetRect(rect,
    1,1);
    DrawText(handle,PChar(TStringGrid(sender).cells[ACol,ARow])
    ,length(TStringGrid(sender).cells[ACol,ARow]),rect,DT_CENTER);
    end;
    Ord(sgtName):
    begin
    brush.color:
    =clBtnFace; //default
    //brush.color:=clSkyBlue;
    FillRect(rect);
    Font.Style:
    =[fsBold];
    Font.Size:
    =9;
    Font.Color:
    =clRed; //Grid font color
    OffsetRect(rect,
    1,1);
    DrawText(handle,PChar(TStringGrid(sender).cells[ACol,ARow])
    ,length(TStringGrid(sender).cells[ACol,ARow]),rect,DT_CENTER);
    end;
    else //default
    begin
    brush.color:
    =clBtnFace; //default
    //brush.color:=clSkyBlue;
    FillRect(rect);
    Font.Style:
    =[fsBold];
    Font.Size:
    =9;
    Font.Color:
    =clBlue; //Grid font color
    OffsetRect(rect,
    1,1);
    DrawText(handle,PChar(TStringGrid(sender).cells[ACol,ARow])
    ,length(TStringGrid(sender).cells[ACol,ARow]),rect,DT_CENTER);
    end
    end;
    end;
    end;

    end.

  • 相关阅读:
    .Net/C#: 实现支持断点续传多线程下载
    软件设计过程中常用的几种图(二)
    走马灯 代码appendChild实现的无缝滚动
    调用web service时的SOAPHEADER验证
    Excel VBA 学习总结 数据验证与正则表达式
    C#者重建C++之路 运行机制的差异
    Excel VBA 学习总结 代码优化之道
    Excel VBA 学习总结 文件系统
    软件设计:“度”、“裁剪”与“变通”
    Excel VBA 学习总结 开发模式
  • 原文地址:https://www.cnblogs.com/Jekhn/p/1920348.html
Copyright © 2011-2022 走看看