回复 "刘那" 的问题: 你的例子挺好玩的, 我模拟做了一下
2008-05-14 15:00
253 查看
问题来源: http://www.cnblogs.com/del/archive/2008/05/14/1089344.html#1196271
本例效果图:
代码文件:
窗体设计:
本例效果图:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Grids; type TForm1 = class(TForm) LabeledEdit1: TLabeledEdit; LabeledEdit2: TLabeledEdit; LabeledEdit3: TLabeledEdit; Button1: TButton; procedure FormCreate(Sender: TObject); procedure LabeledEdit2KeyPress(Sender: TObject; var Key: Char); procedure LabeledEdit3KeyPress(Sender: TObject; var Key: Char); procedure LabeledEdit1Change(Sender: TObject); procedure Button1Click(Sender: TObject); end; {自定义的类, 用于显示字符列表} TMyClass = class(TStringGrid) private FCharList: string; procedure SetCharList(const str: string); public constructor Create(AOwner: TComponent); override; procedure SetChar(ind: Integer; char: Char); property CharList: string read FCharList write SetCharList; procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override; end; var Form1: TForm1; implementation {$R *.dfm} var MyTable: TMyClass; procedure TForm1.FormCreate(Sender: TObject); begin LabeledEdit1.EditLabel.Caption := '字符列表'; LabeledEdit2.EditLabel.Caption := '插入位置(数字)'; LabeledEdit3.EditLabel.Caption := '要插入的字符(字母)'; MyTable := TMyClass.Create(Self); MyTable.Parent := Self; MyTable.Left := 8; MyTable.Top := 12; end; procedure TForm1.LabeledEdit2KeyPress(Sender: TObject; var Key: Char); begin if not (Key in ['0'..'9']) then Key := #0; end; procedure TForm1.LabeledEdit3KeyPress(Sender: TObject; var Key: Char); begin if not (Key in ['A'..'Z', 'a'..'z']) then Key := #0 else TLabeledEdit(Sender).SelectAll; end; procedure TForm1.LabeledEdit1Change(Sender: TObject); begin MyTable.CharList := LabeledEdit1.Text; end; procedure TForm1.Button1Click(Sender: TObject); var i: Integer; c: Char; begin if LabeledEdit3.Text = '' then Exit; i := StrToIntDef(LabeledEdit2.Text, 0); c := LabeledEdit3.Text[1]; MyTable.SetChar(i, c); LabeledEdit1.Text := MyTable.FCharList; end; { 下面是 TMyClass 的代码 } //建立时调整 StringGrid 的外观 constructor TMyClass.Create(AOwner: TComponent); begin inherited; FixedCols := 0; FixedRows := 0; RowCount := 1; ColCount := 3; Font.Size := 10; Font.Color := clRed; Font.Style := [fsBold]; DefaultColWidth := DefaultRowHeight; Height := DefaultRowHeight + 4; Width := (DefaultColWidth + 1) * ColCount + 3; end; //赋予字符列表 procedure TMyClass.SetCharList(const str: string); var i: Integer; begin if str = '' then Rows[0][0] := '' else begin FCharList := str; ColCount := Length(str); for i := 0 to ColCount - 1 do Rows[0][i] := str[i+1]; Width := (DefaultColWidth + 1) * ColCount + 3; end; end; //让字符居中 procedure TMyClass.DrawCell(ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState); var str: string; begin inherited; Canvas.FillRect(ARect); str := Cells[ACol,ARow]; Canvas.TextRect(ARect, str, [tfSingleLine, tfCenter, tfVerticalCenter]); end; //插入字符 procedure TMyClass.SetChar(ind: Integer; char: Char); var s1,s2: string; i: Integer; begin if ind > Length(FCharList) then i := Length(FCharList) else i := ind; s1 := Copy(FCharList, 0, i); s2 := Copy(FCharList, i+1, Length(FCharList)); FCharList := s1 + char + s2; SetCharList(FCharList); end; end.
窗体设计:
object Form1: TForm1 Left = 384 Top = 206 Caption = 'Form1' ClientHeight = 202 ClientWidth = 255 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False Position = poDesigned OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object LabeledEdit1: TLabeledEdit Left = 8 Top = 75 Width = 227 Height = 21 EditLabel.Width = 61 EditLabel.Height = 13 EditLabel.Caption = 'LabeledEdit1' TabOrder = 0 OnChange = LabeledEdit1Change end object LabeledEdit2: TLabeledEdit Left = 8 Top = 123 Width = 121 Height = 21 EditLabel.Width = 61 EditLabel.Height = 13 EditLabel.Caption = 'LabeledEdit2' TabOrder = 1 OnKeyPress = LabeledEdit2KeyPress end object LabeledEdit3: TLabeledEdit Left = 8 Top = 167 Width = 121 Height = 21 EditLabel.Width = 61 EditLabel.Height = 13 EditLabel.Caption = 'LabeledEdit3' TabOrder = 2 OnKeyPress = LabeledEdit3KeyPress end object Button1: TButton Left = 160 Top = 165 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 3 OnClick = Button1Click end end
相关文章推荐
- 一个定时器相关的简单例子 - 回复 "Timer008" 的问题
- Linux下找不到 _finddata_t,坐等结贴(Linux区也发了贴,解决问题了去那边回复一下,同样给分)
- 简单n! (本来只是一个·简单的地推,只是大数问题有点纠结·,本人用数组简单模拟了一下)
- Dll 使用 PChar 参数的小例子 - 回复 "linximf" 的问题
- 分割图片的例子 - 回复 "小熊宝" 的问题
- Dll 使用 PChar 参数的小例子 - 回复 "linximf" 的问题
- 时间比较问题 举一个简单例子说明:比如一个论坛对当天发表的贴子用new图片标记一下
- 一个字符串到数组的例子 - 回复"成红"的问题, 对其他朋友参考价值不大
- 用 TStringList 模拟 "将字符串转换为变量的功能" - 回复 "flq_00000" 和 "外来天客" 的问题
- 模拟点击网页中的按钮 - 回复 "starcraft_ly" 的问题
- 关于 OnMouseWheelDown、OnMouseWheelUp 等事件的简单例子 - 回复 "巅枫" 的问题
- 用 TStringList 存取结构的例子 - 回复 Test1234 的问题
- 简单的 "双缓冲" 绘图的例子 - 回复 "TookiQ" 的问题
- 记录一下Appium中使用swipe方法时候出现的问题(Appium1.0.0.3版本)
- SUSE ssh登录慢解决办法,ssh登录失败,但是strace一下就好了的分析查询 第一次ssh,路由的问题
- IE8浏览器访问百度首页,输入关键字,点击“百度一下”,搜索内容出现在一个自动打开的新窗口的问题的一个原因
- 编程学习笔记11--模拟问题
- 好玩的东西,测试一下
- (来自百度知道)c/c++ VC++ MFC 关于在Static 控件里面作图的问题 详见问题补充,希望能留下QQ,交流一下
- 【JSP开发】session模拟购物车小例子