DELPHI基础StringGrid控件.docx
- 文档编号:9708329
- 上传时间:2023-02-06
- 格式:DOCX
- 页数:45
- 大小:27.47KB
DELPHI基础StringGrid控件.docx
《DELPHI基础StringGrid控件.docx》由会员分享,可在线阅读,更多相关《DELPHI基础StringGrid控件.docx(45页珍藏版)》请在冰豆网上搜索。
DELPHI基础StringGrid控件
StringGrid行列的增加和删除
如何编写使StringGrid中的一列具有Check功能,和CheckBox效果一样
StringGrid组件Cells内容分行显示在Tstringgrid.ondrawcell事件中
在StringGrid怎样制作只读的列在OnSelectCell事件处理程序中
stringgrid从文本读入的问题
StringGrid组件Cells内容对齐
StringGird的行列背景色设置
怎么改变StringGrid控件某一列的背景和某一列的只读属性
StringGrid控件标题栏的对齐
在stringGrid中使用回车键模拟TAB键切换单元格的功能实现
stringgrid如何清空
让记录在StringGrid中分页显示在打印StringGrid
如何实现在stringgrid中删除鼠标点中的那一行,下一行再顶上的效果
让stringgrid点列头进行排序
正确地设置StringGrid列宽而不截断任何一个文字方法
实现StringGrid的删除,插入,排序行操作
TstringGrid的行列合并研究
Delphi中StringGrid动态绑定下拉列表
在delphi的代码编辑框中输入vk_return,然后按住ctrl键,再移鼠标到上面点击就可看到一堆东东了
shift是一个集合变量。
typeTShiftState=setof(ssShift,ssAlt,ssCtrl,ssLeft,ssRight,ssMiddle,ssDouble);
也就是说TShiftstate类型有ssShift,ssAlt,ssCtrl,ssLeft(鼠标左键),ssRight(鼠标右键),ssMiddle(鼠标中键),ssDouble(鼠标双击)
ValueMeaning
ssShiftTheShiftkeyishelddown.
ssAltTheAltkeyishelddown.
ssCtrlTheCtrlkeyishelddown.
ssLeftTheleftmousebuttonishelddown.
ssRightTherightmousebuttonishelddown.
ssMiddleThemiddlemousebuttonishelddown.
ssDoubleThemousewasdouble-clicked.
用来判断组合键中是否用shift、Ctrl、鼠标左键、右键、中键、双击事件。
一般用在keydown、keyup、keypress事件中。
OnKeyDown和OnKeyUp事件中的Key是Word型的,表示用户按的是键盘上的哪个键,OnKeyPress中的Key是Char型,表示用户输入什么字符。
1.KeyPress主要用来捕获数字(注意:
包括Shift+数字的符号)、字母(注意:
包括大小写)、小键盘等除了F1-12、SHIFT、Alt、Ctrl、Insert、Home、PgUp、Delete、End、PgDn、ScrollLock、Pause、NumLock、{菜单键}、{开始键}和方向键外的ANSI字符
KeyDown和KeyUp通常可以捕获键盘除了PrScrn所有按键(这里不讨论特殊键盘的特殊键)
2.KeyPress只能捕获单个字符
KeyDown和KeyUp可以捕获组合键。
3.KeyPress可以捕获单个字符的大小写
4.KeyDown和KeyUp对于单个字符捕获的KeyValue都是一个值,也就是不能判断单个字符的大小写。
5.KeyPress不区分小键盘和主键盘的数字字符。
KeyDown和KeyUp区分小键盘和主键盘的数字字符。
6.其中PrScrn按键KeyPress、KeyDown和KeyUp都不能捕获。
StringGrid行列的增加和删除
type
TExCell=class(TStringGrid)
public
procedureDeleteRow(ARow:
Longint);
procedureDeleteColumn(ACol:
Longint);
procedureInsertRow(ARow:
LongInt);
procedureInsertColumn(ACol:
LongInt);
end;
procedureTExCell.InsertColumn(ACol:
Integer);
begin
ColCount:
=ColCount+1;
MoveColumn(ColCount-1,ACol);
end;
procedureTExCell.InsertRow(ARow:
Integer);
begin
RowCount:
=RowCount+1;
MoveRow(RowCount-1,ARow);
end;
procedureTExCell.DeleteColumn(ACol:
Longint);
begin
MoveColumn(ACol,ColCount-1);
ColCount:
=ColCount-1;
end;
procedureTExCell.DeleteRow(ARow:
Longint);
begin
MoveRow(ARow,RowCount-1);
RowCount:
=RowCount-1;
end;
如何编写使StringGrid中的一列具有Check功能,和
CheckBox效果一样
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Classes,Graphics,
Controls,Forms,Dialogs,Grids;
type
TForm1=class(TForm)
grid:
TStringGrid;
procedureFormCreate(Sender:
TObject);
proceduregridDrawCell(Sender:
TObject;ACol,ARow:
Integer;
Rect:
TRect;State:
TGridDrawState);
proceduregridClick(Sender:
TObject);
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:
TForm1;
fcheck,fnocheck:
tbitmap;
implementation
{$R*.DFM}
procedureTForm1.FormCreate(Sender:
TObject);
var
i:
SmallInt;
bmp:
TBitmap;
begin
FCheck:
=TBitmap.Create;
FNoCheck:
=TBitmap.Create;
bmp:
=TBitmap.create;
try
bmp.handle:
=LoadBitmap(0,PChar
(OBM_CHECKBOXES));
WithFNoCheckDoBegin
width:
=bmp.widthdiv4;
height:
=bmp.heightdiv3;
canvas.copyrect(canvas.cliprect,bmp.canvas,
canvas.cliprect);
End;
WithFCheckDoBegin
width:
=bmp.widthdiv4;
height:
=bmp.heightdiv3;
canvas.copyrect(canvas.cliprect,bmp.canvas,rect(
width,0,2*width,height));
End;
finally
bmp.free
end;
end;
procedureTForm1.gridDrawCell(Sender:
TObject;ACol,
ARow:
Integer;Rect:
TRect;State:
TGridDrawState);
begin
ifnot(gdFixedinState)then
withTStringGrid(Sender).Canvasdo
begin
brush.Color:
=clWindow;
FillRect(Rect);
ifGrid.Cells[ACol,ARow]='yes'then
Draw((rect.right+rect.left-FCheck.width)div2,
(rect.bottom+rect.top-FCheck.height)div2,FCheck)
else
Draw((rect.right+rect.left-FCheck.width)div2,
(rect.bottom+rect.top-FCheck.height)div2,
FNoCheck);
end;
end;
procedureTForm1.gridClick(Sender:
TObject);
begin
ifgrid.Cells[grid.col,grid.row]='yes'then
grid.Cells[grid.col,grid.row]:
='no'
else
grid.Cells[grid.col,grid.row]:
='yes';
end;
end.
2003-11-1716:
23:
23StringGrid组件Cells内容分行显
示在Tstringgrid.ondrawcell事件中:
DrawText(StringGrid1.Canvas.Handle,pchar
(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells
[Acol,Arow]),Rect,DT_WORDBREAKorDT_LEFT);
可以实现文字换行!
2003-11-1716:
24:
04在StringGrid怎样制作只读的列
在OnSelectCell事件处理程序中,加入:
(所有的列均设成
可修改的)
ifColmod2=0then
grd.Options:
=grd.Options+[goEditing]
else
grd.Options:
=grd.Options-[goEditing];
2003-11-1716:
25:
07stringgrid从文本读入的问题
(Save/LoadaTStringGridto/fromafile?
)stringgrid从
文本读入的问题(Save/LoadaTStringGridto/froma
file?
)
//SaveaTStringGridtoafile
procedureSaveStringGrid(StringGrid:
TStringGrid;const
FileName:
TFileName);
var
f:
TextFile;
i,k:
Integer;
begin
AssignFile(f,FileName);
Rewrite(f);
withStringGriddo
begin
//WritenumberofColumns/Rows
Writeln(f,ColCount);
Writeln(f,RowCount);
//loopthroughcells
fori:
=0toColCount-1do
fork:
=0toRowCount-1do
Writeln(F,Cells[i,k]);
end;
CloseFile(F);
end;
//LoadaTStringGridfromafile
procedureLoadStringGrid(StringGrid:
TStringGrid;const
FileName:
TFileName);
var
f:
TextFile;
iTmp,i,k:
Integer;
strTemp:
String;
begin
AssignFile(f,FileName);
Reset(f);
withStringGriddo
begin
//Getnumberofcolumns
Readln(f,iTmp);
ColCount:
=iTmp;
//Getnumberofrows
Readln(f,iTmp);
RowCount:
=iTmp;
//loopthroughcells&fillinvalues
fori:
=0toColCount-1do
fork:
=0toRowCount-1do
begin
Readln(f,strTemp);
Cells[i,k]:
=strTemp;
end;
end;
CloseFile(f);
end;
//SaveStringGrid1to'c:
.txt':
procedureTForm1.Button1Click(Sender:
TObject);
begin
SaveStringGrid(StringGrid1,'c:
.txt');
end;
//LoadStringGrid1from'c:
.txt':
procedureTForm1.Button2Click(Sender:
TObject);
begin
LoadStringGrid(StringGrid1,'c:
.txt');
end;
*******************************************
打开一个已有的文本文件,并将内容放到stringgrid中,文本
行与stringgrid行一致;
在文本中遇到空格则放入下一cells.
搞定!
注意,我只写了一个空格间隔的,你自己修改一
下splitstring可以用多个空格分隔!
procedureTForm1.Button1Click(Sender:
TObject);
var
aa,bb:
tstringlist;
i:
integer;
begin
aa:
=tstringlist.Create;
bb:
=tstringlist.Create;
aa.LoadFromFile('c:
.txt');
fori:
=0toaa.Count-1do
begin
bb:
=SplitString(aa.Strings[i],'');
stringgrid1.Rows[i]:
=bb;
end;
aa.Free;
bb.Free;
end;
其中splitstring为:
functionSplitString(constsource,ch:
string):
tstringlist;
var
temp:
string;
i:
integer;
begin
result:
=tstringlist.Create;
temp:
=source;
i:
=pos(ch,source);
whilei<>0do
begin
result.Add(copy(temp,0,i-1));
delete(temp,1,i);
i:
=pos(ch,temp);
end;
result.Add(temp);
end;
StringGrid组件Cells内容对齐
在StringGrid的DrawCell事件中添加类似的代码就可以了
:
VAR
vCol,vRow:
LongInt;
begin
vCol:
=ACol;vRow:
=ARow;
WITHSenderASTStringGrid,CanvasDO
IFvCol=2THENBEGIN///对于第2列设置为右对齐
SetTextAlign(Handle,TA_RIGHT);
FillRect(Rect);
TextRect(Rect,Rect.RIGHT-2,Rect.Top+2,Cells
[vCol,vRow]);
END;
end;
2003-11-1716:
28:
41当我将StringGird的options属性
中包含goRowSelect项时每当我选中StringGrid中一行,
则选中行用深蓝色显示,我想将深蓝色改为其他颜色应
怎样该?
当我将StringGird的options属性中包含
goRowSelect项时每当我选中StringGrid中一行,则选中
行用深蓝色显示,我想将深蓝色改为其他颜色应怎样该
?
procedureTForm1.StringGrid1DrawCell(Sender:
TObject;ACol,ARow:
Integer;
Rect:
TRect;State:
TGridDrawState);
begin
WithStringGrid1do
begin
If(ARow=Krow)andnot(acol=0)then
begin
Canvas.Brush.Color:
=clYellow;//ClBlue;
Canvas.FillRect(Rect);
Canvas.font.color:
=ClBlack;
Canvas.TextOut(rect.left,rect.top,cells[acol,arow]);
end;
end;
end;
procedureTForm1.StringGrid1SelectCell(Sender:
TObject;ACol,
ARow:
Integer;varCanSelect:
Boolean);
begin
krow:
=Arow;//*
kcol:
=Acol;
end;
注意:
必须把变量KROW的值初始为1或其他不为0的值
,否则如果锁定第一行的话,第一行的颜色将被自设颜
色取代,而锁定行不会被重画。
2003-11-1716:
32:
44怎么改变StringGrid控件某一列
的背景和某一列的只读属性,StringGrid控件标题栏的对齐
.怎么改变StringGrid控件某一列的背景和某一列的只读属
性,StringGrid控件标题栏的对齐.
请参考以下代码:
在OnDrawCell事件中处理背景色。
程序如下:
//将第二列背景变为红色。
procedureTForm1.StringGrid1DrawCell(Sender:
TObject;ACol,ARow:
Integer;
Rect:
TRect;State:
TGridDrawState);
begin
ifnot((acol=1)and(arow>=stringgrid1.fixedrows))then
exit;
withstringgrid1do
begin
canvas.Brush.color:
=clRed;
canvas.FillRect(Rect);
canvas.TextOut(rect.left+2,rect.top+2,cells[acol,arow])
end;
end;
//加入如下代码,那么StringGrid的第四列就只读了.其他列
非只读
procedureTForm1.StringGrid1SelectCell(Sender:
TObject;ACol,ARow:
Integer;varCanSelect:
Boolean);
begin
withStringGrid1dobegin
ifACol=4then
Options:
=Options-[goEditing]
elseOptions:
=Options+[goEditing];
end;
procedureTForm1.StringGrid1DrawCell(Sender:
TObject;ACol,ARow:
Integer;Rect:
TRect;State:
TGridDrawState);
var
dx,dy:
byte;
begin
if(acol=4)andnot(arow=0)then
withstringgrid1do
begin
canvas.Brush.color:
=clYellow;
canvas.FillRect(Rect);
canvas.font.color:
=clblue;
dx:
=2;//调整此值,控制字在网格中显示的水平位置
dy:
=2;//调整此值,控制字在网格中显示的垂直位置
canvas.TextOut(rect.left+dx,rect.top+dy,cells[acol,
arow]);
end;
//控制标题栏的对齐
if(arow=0)then
withstringgrid1do
begin
canvas.Brush.color:
=clbtnface;
canvas.FillRect(Rect);
dx:
=12;//调整此值,控制字在网格中显示的水平位
置
dy:
=5;//调整此值,控制字在网格中显示的垂直位置
canvas.TextOut(rect.left+dx,rect.top+dy,cells
[acol,arow]);
end;
end;
2003-11-1716:
37:
15在stringGrid中使用回车键模拟
TAB键切换单元格的功能实现......
procedureTForm1.StringGrid1KeyPress(Sender:
TObject;varKey:
Char);
label
nexttab;
begin
ifkey=#13then
begin
key:
=#0;
nexttab:
if(stringgrid1.Col begin stringgrid1.Col: =stringgrid1.Col+1; end else begin ifstringgrid1.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DELPHI 基础 StringGrid 控件