如何根据数据库表中各字段的宽度动态修改tstringgrid中各列的宽度? 有时候读出来的值超出列宽度,又不能手工拉大宽度,有没有办法解决。
解决方案 »
- 请教一个ioHandle的write 大stream问题
- 请各路大侠帮忙installshield express for delphi7 哪里下载
- 关于Listview---用listview显示房态图
- 文件传输,从客户端传输到服务器端,并在服务器段显示文件传输进度--->>>>???
- 如何实现右键单击选择TreeView的节点?
- 再问:一个程序结束,如何告诉另一个程序?
- 为什么出现延迟?万分紧急!
- 恩,好象有不少高手在找问题答呢!那我问一个问题!
- delphi的源代码怎么还没有VB多?delphi到底行不行了
- help!!! 为什么我的D6在编译时出现错误:“Fatal Error QuickRpt.pas(23):File not found:'Dsgnintf.dcu'
- 一个上传QQ聊天记录的软件
- 多表组合分类统计问题~~~
begin
Color := clInfobk;
DefaultRowHeight := 22;
RowCount := 2;
ColCount := 7;
ColWidths[0] := 20;
ColWidths[1] := 80;
ColWidths[2] := 60;
ColWidths[3] :=120;
ColWidths[4] := 50;
ColWidths[5] := 80;
ColWidths[6] :=400;
Cells[0,0] := '';
Cells[1,0] := 'QQ号码';
Cells[2,0] := '去留状态';
Cells[3,0] := 'QQ昵称';
Cells[4,0] := 'QQ性别';
Cells[5,0] := '最近发言时间';
Cells[6,0] := '信息更新情况';
FixedColor := clMoneyGreen;
FixedRows := 1;
FixedCols := 1;
Font.Color := clWindowText;//clNavy;
end;这是从我一个程序里复制的一段
有时候读出来的值超出列宽度,又不能手工拉大宽度,有没有办法解决。
---------------------------------------
自动换行显示。请参考:
http://community.csdn.net/Expert/topic/5236/5236146.xml?temp=.4189875
老冯,可不可以写代码实现鼠标停留时自动显示出来,这样的代码会不会少一些?等你的回复..
Q: 1、可不可以写代码实现鼠标停留时自动显示出来
--------------------------------------------
A: 这个我写了一个“点击StringGrid单元格时用Hint显示单元格内容”的代码,附后。
Q: 2、lihuasoft兄,我是指自动调整宽度
-------------------------------------------
A: 这个,我们一起等老冯写好后,共同学习吧。
{ 代码功能:单击StringGrid单元格时,在 Hint 里显示此格Text }
//Button1 : 往StringGrid里添加测试文本procedure TForm1.Button1Click(Sender: TObject);
begin
with StringGrid1 do
begin
Cells[1,1] := '号称礼仪(义)之邦的某个古国和她的民族正在面临一场礼仪(义)危机';
Cells[1,2] := '中华人民共和国山东省';
Cells[2,1] := '日本国静冈县';
Cells[2,2] := '以上是测试数据';
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ShowHint := True;
StringGrid1.Options := [goFixedHorzLine,goFixedVertLine,goVertLine,goHorzLine,goRangeSelect,goEditing,goAlwaysShowEditor];
end;procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
HWND : integer;
begin
HWND := FindWindowEx(StringGrid1.Handle,0,Pchar('TInplaceEdit'),nil);
FindControl(HWND).Hint := TInPlaceEdit(FindControl(HWND)).Text;
end;
begin
with StringGrid1 do
begin
Cells[1,1] := '号称礼仪(义)之邦的某个古国和她的民族正在面临一场礼仪(义)危机';
Cells[1,2] := '中华人民共和国山东省';
Cells[2,1] := '日本国静冈县';
Cells[2,2] := '测试数据';
end;
end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
iTemp : Integer;
begin
with StringGrid1 do
if Length(Trim( Cells[ACol,ARow] )) > 1 {为了应付某格空值} then
ColWidths[ACol] := Font.Size * Length(Trim( Cells[ACol,ARow] ));
end;
======
procedure TFormDemo.StringGridDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
WCell: Integer;
WCol: Integer;
SCell: string;
begin
SCell := StringGrid.Cells[ACol, ARow];
WCol := StringGrid.ColWidths[ACol];
WCell := StringGrid.Canvas.TextWidth(SCell);
if WCell > WCol then
StringGrid.ColWidths[ACol] := WCell;
//下面的语句是重画,使之水平和垂直都居中(你也可以不重画, 保持原有对齐状态)
StringGrid.Canvas.FillRect(Rect);
DrawText(StringGrid.Canvas.Handle, PChar(SCell), Length(SCell), Rect, DT_Center or DT_VCenter or DT_SingleLine);
end;
====
自动换行procedure TFormDemo.StringGridDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
HCell: Integer;
HRow: Integer;
SCell: string;
begin
SCell := StringGrid.Cells[ACol, ARow];
HRow := StringGrid.RowHeights[ARow];
StringGrid.Canvas.FillRect(Rect);
HCell := DrawText(StringGrid.Canvas.Handle, PChar(SCell), Length(SCell), Rect, DT_Center or DT_VCenter or DT_WORDBREAK );
if HCell > HRow then
StringGrid.RowHeights[ARow] := HCell;
end;
function AvgCharWidth(FHandle:Hwnd): Word;
var
Metrics: TTextMetric;
begin
GetTextMetrics(FHandle, Metrics);
Result := Metrics.tmAveCharWidth;
end;
//计算列宽代码
procedure TForm1.BitBtn1Click(Sender: TObject);
var
maxFieldValue_arr:array of integer;
I:integer;
StringGird_FontWidth,SelectFieldValue_Len:integer;
begin
StringGird_FontWidth:=AvgCharWidth(StringGrid1.Canvas.Handle);
with table1 do
begin
open;
setlength(maxFieldValue_arr,Fields.Count);
self.StringGrid1.ColCount:=Fields.Count;
self.StringGrid1.RowCount:=recordcount;
while (not eof) do
begin
For I:=0 to Fields.Count-1 do
begin
SelectFieldValue_Len:=StringGird_FontWidth*Length(Fields[I].AsString);
if SelectFieldValue_Len>maxFieldValue_arr[I] then
begin
maxFieldValue_arr[I]:=SelectFieldValue_Len*2;
StringGrid1.ColWidths[I]:=SelectFieldValue_Len*2;
end;
if not Fields[I].IsNull then
StringGrid1.Cells[I,recNo]:=Fields[I].AsString;
end;
next;
end;
close;
end;
end;
if SelectFieldValue_Len>maxFieldValue_arr[I] then
begin
maxFieldValue_arr[I]:=SelectFieldValue_Len*2+2;
StringGrid1.ColWidths[I]:=SelectFieldValue_Len*2+2;
end;