这样的效果如何实现:高分求救(200)

  • 主题发起人 主题发起人 lzd123
  • 开始时间 开始时间
L

lzd123

Unregistered / Unconfirmed
GUEST, unregistred user!
在DELPHI中 使用类似stringGRID的控件,实现: 1、奇偶行可以自行设置背景颜色 2、文字可以自动换行,中英文皆可 3、每一行的文本可以是多个字段拼合起来的文字,可以对指定的文字进行着色,如:发布文字[red]红[/red][yellow]黄[/yellow][blue]兰[/blue] [green]发布时间:2009-01-01[/green]注意,三个条件同时满足,如能解决,定重谢!
 
DrawGrid可以满足一切要求
 
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids;Const WM_MYMESSAGE = WM_USER+1;type TForm1 = class(TForm) DrawGrid1: TDrawGrid; procedure DrawGrid1GetEditText(Sender: TObject; ACol, ARow: Integer; var Value: String); procedure DrawGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); procedure DrawGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String); procedure DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); private { Private declarations } Function GetCellText(ACol,ARow:Integer):String; Protected public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DrawGrid1GetEditText(Sender: TObject; ACol, ARow: Integer; var Value: String);begin//获得当前单元格内容end;procedure TForm1.DrawGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);begin//CanSelect用于控制是否可以选中当前单元格end;procedure TForm1.DrawGrid1SetEditText(Sender: TObject; ACol, ARow: Integer; const Value: String);begin//保存当前单元格内容end;procedure TForm1.DrawGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);Const CHARARR:ARRAY [1..2] OF String=('你','好'); COLARR:ARRAY [1..2] OF TColor=(clRed,clBlue);Var X,Y,I:Integer; iPos:Integer; Flags:integer; TmpRect:TRect; S,Head:String;begin //绘制内容 S := GetCellText(ACol,ARow); //汇总默认值 X:=Rect.Left+2; Y:=Rect.Top+2; Flags:=DT_WORDBREAK ; TmpRect:=Rect; OffsetRect(TmpRect,2,2); TmpRect.Bottom:=TmpRect.Bottom-2; TmpRect.Right:=TmpRect.Right-2; if (ARow Mod 2) = 0 then TDrawGrid(Sender).Canvas.Brush.Color:=clblue else TDrawGrid(Sender).Canvas.Brush.Color:=clYellow; TDrawGrid(Sender).Canvas.FillRect(TmpRect); DrawText(TDrawGrid(Sender).Canvas.Handle, PChar(S), Length(S), TmpRect, Flags); //处理特殊颜色 For I:=Low(CHARARR) to High(CHARARR) do Begin iPos := Pos( CHARARR,S); Y:=Rect.Top+2; if iPos>0 then begin Head:=Copy(S,1,iPos-1); X:=Rect.Left+2+ TStringGrid(Sender).Canvas.TextWidth(Head); TDrawGrid(Sender).Canvas.Font.Color:=COLARR; TDrawGrid(Sender).Canvas.TextOut(X,Y,CHARARR); end; End;end;function TForm1.GetCellText(ACol, ARow: Integer): String;begin result:=inttostr(ACol)+',好'+#13#10+Inttostr(ARow); //测试案例end;end.
 
在StringGrid1的OnDrawCell事件里写代码自已画吧,能满你一切要求,若数据量大记得要把DefaultDrawing设为false可加快速度(这也是完全自画了).主要就是Canvas.Brush属性和Canvas.Font属性设背景颜色及字体颜色再加上Canvas.FillRect写背景,Canvas.TextOut写字,再根据TGridDrawState判断一下状态,给点例字,你自已琢磨吧,不难的,关键是算好Rect的范围(若有必要,可自已再定义一个Rect变量,这样画的时候不破坏原来的Rect)procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);var S: string; L, T: integer;begin with TStringGrid(Sender) do begin if (ARow>0) and (ACol>0) then begin //奇数行:兰色,偶数行:红色 if odd(ARow) then Canvas.Brush.Color:= clBlue else Canvas.Brush.Color:= clRed; end; Canvas.FillRect(Rect); S:= Format('(%d,%d)',[ACol, ARow]); L:= Rect.Left + ((Rect.Right - Rect.Left - Canvas.TextWidth(S)) div 2); T:= Rect.Top + ((Rect.Bottom - Rect.Top - Canvas.TextHeight(S)) div 2); if L < Rect.Left then L:= Rect.Left; Canvas.TextOut(L, T, S); end;end;
 
后退
顶部