| Integer | 符号付き32bit |
| Cardinal | 符号付き32bit |
| ShortInt | 符号付き8bit |
| SmallInt | 符号付き16bit |
| LongInt | 符号付き32bit |
| Int64 | 符号付き32bit |
| Byte | 符号無し8bit |
| Word | 符号無し16bit |
| LongWord | 符号無し32bit |
| UInt64 | 符号無し32bit |
| TDicitionary | キー-値ペア |
| TList | インデックスによってアクセス出来る順序付きリスト |
| TQueue | FIFO |
| TStack | FILO |
uses Generics.Collections; var xlist:TList<integer>; begin xlist:=TList<integer>.Craete; try xlist.Add(1); xlist.Add(2); xlist.Add(3); for i=0 to xlist.Count-1 do begin memo(StrToInt(xlist[i])) end; finally xlist.Free; end;
type TMyClass = class(TObject) public class var//静的クラスフィールドのブロック開始 Top:Integer; Bottom:Integer; var//ブロック終了 end;
keyword dbgoutOutputDebugString(PWideChar(index+' '+IntToStr(ofs)+' = '+Result));

colors:array of TColor; SetLength(colors,5); for i=0 to High(colors) do colors[i]
GetMemとの差は、Delphi2010のヘルプ(System.GetMemory)によると、var ptr:pointer; begin try ptr:=GetMemory(600*1024*1024); //600MBytes!! finally FreeMemory(ptr); end;
メモ: GetMemory は、GetMem の C++ 互換バージョンです。
動的配列の場合FillChar(m_Buffer^,total_size,$FF);
FillChar(Buffer[0],total_size,$FF);
コピー。名前は移動だけど。SysUtils.StrLCopy(Dest,Source,MaxLen)
memcpyと方向逆なのと、var渡しのが要注意。ここらへんが判りづらい。procedure Move(const Source; var Dest; Count: Integer);
出力procedure TForm1.Button1Click(Sender: TObject); var Buffer: array of Byte; src_ptr,dst_ptr:PByte; i: integer; begin SetLength(Buffer, 256); for i := 0 to Length(Buffer) - 1 do Buffer[i] := i; memo('before'); for i := 0 to 10 - 1 do begin memo(IntToStr(i) + '=' + IntToStr(Buffer[i])); end; src_ptr:=PByte(Buffer); dst_ptr:=PByte(Buffer); inc(src_ptr,4); Move(src_ptr^, dst_ptr^, 3); memo('after'); for i := 0 to 10 - 1 do begin memo(IntToStr(i) + '=' + IntToStr(Buffer[i])); end; Move(Buffer[7], Buffer[0], 3); memo('after2'); for i := 0 to 10 - 1 do begin memo(IntToStr(i) + '=' + IntToStr(Buffer[i])); end; end;
before 0=0 1=1 2=2 3=3 4=4 5=5 6=6 7=7 8=8 9=9 after 0=4 1=5 2=6 3=3 4=4 5=5 6=6 7=7 8=8 9=9 after2 0=7 1=8 2=9 3=3 4=4 5=5 6=6 7=7 8=8 9=9
FillChar Move
実行結果procedure TForm1.Button1Click(Sender: TObject); var static_arr: array [0 .. 12] of Byte; dyn_arr: array of Byte; buffer: PByte; str: string; procedure dump(ptr: PByte; len: integer); var i: integer; tmp: string; begin tmp := ''; for i := 0 to len - 1 do begin tmp := tmp + IntToHex(ptr^, 2) + ' '; inc(ptr); end; Memo1.Lines.Add(tmp) end; begin // ----------------------------------------- Memo1.Lines.Add('文字列をバイト列に変換(静的配列)'); Memo1.Lines.Add('Length(static_arr)=' + IntToStr(Length(static_arr))); str := '01234567'; Memo1.Lines.Add(str); FillChar(static_arr, sizeof(static_arr), 1); dump(@static_arr, sizeof(static_arr)); StrLCopy(PAnsiChar(@static_arr), PAnsiChar(AnsiString(str)), Length(static_arr) - 1); // ok dump(@static_arr, sizeof(static_arr)); // ----------------------------------------- Memo1.Lines.Add('文字列をバイト列に変換(動的配列)'); str := '76543210'; Memo1.Lines.Add(str); SetLength(dyn_arr, 16); Memo1.Lines.Add('Length(dyn_arr)=' + IntToStr(Length(dyn_arr))); FillChar(dyn_arr[0], Length(dyn_arr), 2); dump(PByte(dyn_arr), Length(dyn_arr)); StrLCopy(PAnsiChar(dyn_arr), PAnsiChar(AnsiString(str)), Length(dyn_arr) - 1); dump(PByte(dyn_arr), Length(dyn_arr)); // ----------------------------------------- Memo1.Lines.Add('文字列をバイト列に変換(動的メモリ)'); buffer := GetMemory(20); try str := '01234567'; Memo1.Lines.Add(str); FillChar(buffer^, 20, 3); dump(PByte(buffer), 20); StrLCopy(PAnsiChar(buffer), PAnsiChar(AnsiString(str)), 22 - 1); dump(PByte(buffer), 20); finally FreeMemory(buffer); end; end;
Memo1 文字列をバイト列に変換(静的配列) Length(static_arr)=13 01234567 01 01 01 01 01 01 01 01 01 01 01 01 01 30 31 32 33 34 35 36 37 00 01 01 01 01 文字列をバイト列に変換(動的配列) 76543210 Length(dyn_arr)=16 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 37 36 35 34 33 32 31 30 00 02 02 02 02 02 02 02 文字列をバイト列に変換(動的メモリ) 01234567 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 03 30 31 32 33 34 35 36 37 00 03 03 03 03 03 03 03 03 03 03 03
var byte_buf:array[0..15]of Byte; begin byte_buf[bytebuf_wr]:=0;//ヌル終端 str:=String(PAnsiChar(@byte_buf));
実行結果var static_arr:array [0..8] of Byte; begin StrLCopy(@static_arr,AnsiString('1234'),Length(static_arr)-1); Memo1.Lines.Add(String(PAnsiChar(@static_arr))); Memo1.Lines.Add(String(PAnsiChar(@static_arr[1]))); end;
1234 234
var buf:Pointer; ptr:PByte; begin buf:=AllocMem(16);//AllocMemは領域を0クリアする try ptr:=PByte(buf); ptr^:=$30; inc(ptr); ptr^:=$31; Memo1.Lines.Add(string(PAnsiChar(buf))); finally FreeMem(buf); end;
結果はこうprocedure TForm1.btnAnsiCharClick(Sender: TObject); var arr:array [0..2] of AnsiChar; str:String; begin arr[0]:=AnsiChar($82); arr[1]:=AnsiChar($A0); arr[2]:=AnsiChar(0); str:=arr; Memo1.Lines.Add('AnsiChar配列に「あ」$82$A0を入れた場合'); Memo1.Lines.Add(str); end; procedure TForm1.btnCharClick(Sender: TObject); var arr:array [0..2] of Char; str:String; begin arr[0]:=Char($82); arr[1]:=Char($A0); arr[2]:=Char(0); str:=arr; Memo1.Lines.Add('Char配列に「あ」$82$A0を入れた場合'); Memo1.Lines.Add(str); end;
□の部分は、見た目がそうなるという事で、□のコードに変換されたわけでは無いです。AnsiChar配列に「あ」$82$A0を入れた場合 あ Char配列に「あ」$82$A0を入れた場合 □
CompareStr,CompareTextも同様だが、戻り値がIntegerになる。uses SysUtils function SameStr(S1: string; S2: string): Boolean;//大文字小文字区別する function SameText(S1: string; S2: string): Boolean;//大文字小文字区別しない
SubstrがSに有る場合は、その位置を1〜nで返す。※1からというのが罠uses System function Pos(const Substr: string; const S: string): Integer;
Indexは1からというのが罠uses System function Copy(S:string;Index:Integer;Count:Integer):string;
uses StrUtils ReplaceStr(const AText:string;const AFromText:string;const AToText:string):string; 大文字小文字区別 ReplaceText(const AText:string;const AFromText:string;const AToText:string):string; 大文字小文字区別しない StringReplace AnsiString用
var uid:integer; implementation : : initialization uid:=0; end.
procedure test; label exit_success; begin goto exit_success; : : exit_success: : end;
function Getvalues(index: integer): string; procedure Setvalues(index: integer; const Value: string); property values[index:integer]:string read Getvalues write Setvalues;
type TSampleEvent = procedure (const Value: string) of object; type TTestEventProperty = class(TObject) private FOnSample: TSampleEvent; public property OnSampleEvent: TSampleEvent read FOnSample write FOnSample; end; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private procedure testfunc(const Value: string); { Private 宣言 } public { Public 宣言 } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.testfunc(const Value: string); begin ShowMessage(Value); end; procedure TForm1.Button1Click(Sender: TObject); var sample:TTestEventProperty; begin sample:=TTestEventProperty.Create; try sample.OnSampleEvent:=testfunc; sample.OnSampleEvent('syokun!'); finally sample.Free; end; end;
uses SysUtils; raise Exception.Create('Error!!');
uses SysUtils; FloatToStrF(Tick/1000,ffFixed,15,3);
その3uses SysUtils; FormatFloat('00',12);
f:=0.000012345678; memo(FloatToStrF(f,ffExponent,4,1)); memo(FloatToStrF(f,ffExponent,4,2)); memo(FloatToStrF(f,ffExponent,3,2)); //出力 1.235E-5 1.235E-05 1.23E-05
結果uses SysUtils; Memo1.Lines.Add(DateTimeToStr(Now));
2009/01/29 20:28:12
結果uses SysUtils; var str:string; begin DateTimeToString(str,'yymmdd_hhnnss',Now); Memo1.Lines.Add(str); end;
070127_112913 070127_112914
で、戻り値はuses DateUtils; DayOfTheWeek(dt)
とかで比較。DayMonday
uses DateUtils; DaysBetween(ANow:TDateTime,ATHen:TDateTime):integer;
| DaySpan | 端数有り |
| DaysBetween | 端数無し |
| HoursBetween | |
| MinuteBetween | |
| SecondsBetween | |
| WeeksBetween | |
| YearsBetween |
function get_file_date(fname: string): integer; var FileHnd: integer; begin { ファイルのハンドルを取得 } FileHnd := FileOpen(fname, fmOpenRead); try { ファイルのタイムスタンプを取得 } Result := FileGetDate(FileHnd); { タイムスタンプを日付型に変換 } finally FileClose(FileHnd); end; end;
この場合、Ord(ghAsaGohan)は0、Ord(ghHiruGohan)は1を返す。type TGohan = (ghAsaGohan,ghHiruGohan,ghBanGohan);
type TGohan = set of (ghAsaGohan,ghHiruGohan,ghBanGohan); if ghAsaGohan in gohan then nattou.mazemaze;
type PNS_HEAD = ^NOTE_TO_SH; NOTE_TO_SH = record lMitei2Size:Longint; lVerUpStartAddress:Longword; cYobi:array[0..31] of char; end;
var fs: TFileStream; ptr: PByte; begin GetMem(ptr, BUFFER_SIZE); try fs := TFileStream.Create(filename, fmOpenRead); try fs.Read(ptr^, BUFFER_SIZE); finally fs.Free end; finally FreeMem(ptr); end; end;
var fs:TFileStream; size:integer; chunk_size:integer; read_size:integer; fname:string; arr:TLongwordArray; i:integer; begin fname:='sample.bin'; fs:=TFileStream.Create(fname,fmOpenRead or fmShareDenyWrite); try size:=fs.Size; chunk_size:=4096; read_size:=chunk_size; while size>0 do begin if read_size>size then read_size:=size; fs.Read(arr,read_size); for i := 0 to (read_size div 4) - 1 do begin //arr[i] end; size:=size-read_size; end; finally fs.Free; end; end;
var fs:TFileStream; begin acc_size:=READ_MEMORY_SIZE; remain:=readsize; fs:=TFileStream.Create(fname,fmCreate); try while remain>0 do begin fs.Write(PChar(m_Buffer + SizeOf(SH4USBIF_HEADER)+SizeOf(SH_TO_NOTE))^,acc_size); end; finally fs.Free end; end;
procedure TForm1.btnRefClick(Sender: TObject); var fs:TFileStream; fname:string; begin if OpenDialog1.Execute then begin fname:=OpenDialog1.FileName; edFname.Text:=fname; fs:=TFileStream.Create(fname,fmOpenRead or fmShareDenyNone); try edSize.Text:=IntToStr(fs.Size); finally fs.Free; end; end; end;
procedure TfrmMain.ImageViewMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin memo('mouse down'); MouseCapture:=True; end; procedure TfrmMain.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin memo('main mouse move'); end; procedure TfrmMain.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin memo('main mouse up'); MouseCapture:=False; end;

↓実際はこう。VisibleをTrueにしないと見えない。TLineSeries.Pointer.Style:=TSeriesPointerStyle(cmbPointerStyle.ItemIndex); TLineSeries.Pointer.Visible:=chkPointerVisible.Checked;
Series1.Pointer.Style:=psCircle; Series1.Pointer.Visible:=True;
ShellExecute(Application.Handle,'open',PChar(cmd),PChar(option),nil,SW_NORMAL);
function TForm1.shell_exec(cmd:string;option:string):DWORD; var sei:SHELLEXECUTEINFO; begin ZeroMemory(@sei, sizeof(SHELLEXECUTEINFO)); //構造体のサイズ sei.cbSize := sizeof(SHELLEXECUTEINFO); //起動側のウインドウハンドル sei.Wnd := Handle; //起動後の表示状態 sei.nShow := SW_SHOWNORMAL; //このパラメータが重要で、セットしないとSHELLEXECUTEINFO構造体のhProcessメンバがセットされない。 sei.fMask := SEE_MASK_NOCLOSEPROCESS; //起動プログラム sei.lpFile := PChar(cmd); sei.lpParameters := PChar(option); //プロセス起動 if not ShellExecuteEx(@sei) then//shell32.lib必須 exit; //エラー? if sei.hInstApp <= 32 then exit; //終了を待つ WaitForSingleObject( sei.hProcess, INFINITE ) ; //戻り値を取得 GetExitCodeProcess(sei.hProcess, Result); end;
参考type TForm1 = class(TForm) private { Private 宣言 } procedure WMDropFiles(var Msg: TWMDropFiles); Message WM_DropFiles; end; implementation uses ShellApi; {$R *.dfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin DragAcceptFiles(Handle,True); end; procedure TForm1.WMDropFiles(var Msg: TWMDropFiles); var num_files:integer; i:integer; FileName: Array[0..MAX_PATH] of Char; begin num_files := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0); for i := 0 to num_files - 1 do begin DragQueryFile(Msg.Drop, i, FileName, SizeOf(FileName)); Memo1.Lines.Add(String(FileName)); end; DragFinish(Msg.Drop); end;
procedure TForm1.btnComm1CreateClick(Sender: TObject); begin comm1:=TComm.Create(self); comm1.BaudRate:=9600; comm1.ByteSize:=cbs8; comm1.ParityBits:=cpbNone; comm1.StopBits:=csb1; comm1.FlowControls:=[]; comm1.Port:=1; comm1.OnCommReceive:=Comm1CommReceive; comm1.Open; end; procedure TForm1.Comm1CommReceive(Sender: TObject; Size: Word); begin end;
Application.ShowMainForm:=False;
GetEnvironmentVariable('OS')
if not DirectoryExists('xyz') then MkDir('xyz');
procedure WMNCHITTEST(var Msg: TWMNCHITTEST); message WM_NCHITTEST; procedure WMNCLButtonDBLCLK(var msg :TWMNCHitMessage); message WM_NCLBUTTONDBLCLK; procedure TFormHusen.WMNCHITTEST(var Msg: TWMNCHITTEST); var Pt:TPoint; begin //マウス座標を取得 GetCursorPos(Pt); //フォーム上の座標に変換 Pt := ScreenToClient(Pt); if GetAsyncKeyState(VK_LBUTTON) < 0 then //ウィンドウズにタイトル バーで発生することを示すHTCAPTIONを返す Msg.Result := HTCAPTION else Msg.Result := HTCLIENT; end; //WMNCHITTESTで小細工しているので、ダブルクリックをFormのイベントで検出出来ない procedure TFormHusen.WMNCLButtonDBLCLK(var msg: TWMNCHitMessage); begin end;
unit IkCustomList; interface uses Classes; type TIkDoubleList = class(TList) Protected procedure Notify(Ptr: Pointer; Action: TListNotification); override; function GetItem(Index: Integer): Double; procedure SetItem(Index: Integer; const Value: Double); public procedure Clear; override; function Add(Value: Double): Integer; property Items[Index: Integer]: Double read GetItem write SetItem; default; end; function CompareDouble(item1,item2:Pointer):Integer; implementation //Sort用比較関数 function CompareDouble(item1, item2: Pointer): Integer; begin if PDouble(item1)^ < PDouble(item2)^ then Result:=-1 else if PDouble(item1)^ > PDouble(item2)^ then Result:=1 else Result:=0
end; { TIkDoubleList } function TIkDoubleList.Add(Value: Double): Integer; var p:PDouble; begin New(p); p^:=Value; Result:=Inherited Add(p) end; //この方法だと、Extractの戻り値が使えなくなる。が、実害はなさそう。 procedure TIkDoubleList.Notify(Ptr: Pointer; Action: TListNotification); begin if Action = lnDeleted then begin Dispose(Ptr); end; inherited Notify(Ptr, Action); end; procedure TIkDoubleList.Clear; var i:integer; begin for i := 0 to Count - 1 do Delete(0);//ClearはTListでは要素の解放をしない。ちなみにDestroyではClear呼出しが存在する。 inherited Clear; end; function TIkDoubleList.GetItem(Index: Integer): Double; begin Result:= PDouble(inherited Items[index])^; //Items[index]をアクセスすると、TList.Getが呼ばれる //TList.Getはポインタを返す。 //ポインタを逆参照して値を返す。 end; procedure TIkDoubleList.SetItem(Index: Integer; const Value: Double); begin PDouble(inherited Items[index])^:=Value; //Items[index]をアクセスすると、TList.Getが呼ばれる //TList.Getはポインタを返す。 //ポインタを逆参照して値を入れる。 end; end.
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,IkCustomList; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var dlist:TIkDoubleList; i:integer; begin dlist:=TIkDoubleList.Create; try for i := 0 to 100 do dlist.Add(Random()*100); dlist.Sort(@CompareDouble); for i := 0 to 100 do Memo1.Lines.Add( FloatToStr(dlist.Items[i])); finally dlist.Free; end; end; end.
参考:FTPでlistがDirectoryListingに入らない。uses IdAllFTPListParsers
引用:glyFX Embarcadero Special Edition Icon Set for Embarcadero CustomersC:\Program Files\Common Files\CodeGear Shared\Images\glyFX C:\Program Files\Common Files\Borland Shared\Images\glyFX
if Key = Char(VK_RETURN) then
uses FileCtrl; var path:string; begin if SelectDirectory('Caption','',path) then //真ん中は初期パス exec_proc(path); end;
var rc:TGridRect; begin rc:=StringGrid1.Selection;
color:TColor; r,g,b:Byte; color:=RGB(255,128,64); r = GetRValue(color); g = GetGValue(color); b = GetBValue(color); color := RGB(r,g,b);
if MessageDlg('確認して下さい',mtConfirmation,[mbRetry,mbAbort],0)<>mrRetry then Exit; end;
テスト対象の記述(簡単なクラス)uses GUITestRunner GUITestRunner.RunRegisteredTests;
参考:フォームの重なり順序を変更する方法は?Application.MainFormOnTaskbar := True;
type IxAddNewFunc = procedure (caption:String) of Object; type IxTest = class(TObject) protected FAddNew:IxNewFunc; public property AddNew:IxAddNewTabFunc read FAddNew write FAddNew; end; ---- procedure add_new(caption: string); ---- var tst:IxTest; begin tst:=IxTest.Create; tst.AddNew := add_new;
var strlist:TStringList; begin strlist:=TStringList.Create; try strlist.CommaText:='a=1,b=2,c=3'; memo('---- Strings[] ----'); memo(strlist.Strings[0]); memo(strlist.Strings[1]); memo(strlist.Strings[2]); memo('---- Values[] ----'); memo(strlist.Values['a']); memo(strlist.Values['b']); memo(strlist.Values['c']); memo(strlist.Values['d']);//エラーにはならない finally strlist.Free; end; end;
procedure TfrmSettings.Edit2KeyPress(Sender: TObject; var Key: Char); begin if Ord(Key) = VK_RETURN then begin memo('hit return'); end; end;
| Delphi | C言語 | |
| {$DEFINE XYZ} | #define XYZ | |
| {$I xyz.inc} | #include "xyz.inc" | |
| {$IFDEF XYZ} {$ENDIF} | #ifdef XYZ #endif | |
| {$IF exp} {$ELSE} {$IFEND} | #if exp #else #endif |