delphi一句话帮助

来源:百度文库 编辑:神马文学网 时间:2024/10/04 22:31:28
原文:delphi一句话帮助
1.  如果想你的程序能够正确处理异常情况的话,请引用SysUtils.pas单元,否则即使程序使用了try...except...也不能正确捕获异常。
2.  定义常量字符串的一种方式
resourcestring
aa=‘aaaa‘;
raise Exception.CreateRes(@aa);
3. 字符串常量数组的初始化
const constarray:array [0..2] of string=(‘first’,’second’,’third’);
4. 结构体初始化
type Tstructinit=record
A1:integer;
A2:array [0..2] of integer;
End;
Const m_structinit:Tstructinit=(A1:0;A2:(0,1,2));
5. 多维数组的长度
var array2:array of array of integer;
setlength(array2,2,2);
6.  使用Create和New开辟的空间都存在于堆中,不能自动释放,建议使用FreeAndNil释放, 参数以及局部变量存在于栈中,自动释放。
7. SizeOf不适合于对象,返回的总是4;对于固定类型可以正确返回.
8. Create(nil)需要手工释放,Creat(self)会随着拥有者的释放而释放.
9.  动态改变已定义常量的值
procedure ChangeConst(const Const;var Value;Size:Integer);
begin
Move((@Value)^,(@Constant)^,Size);
End;
10.   进行删除操作的时候循环使用DownTo,会避免错误.
11.   汉字的Ascii码>128,可以用它来判别是否为汉字
12.   dll编写中,需要使用Sharemem单元来引用BORLANDMM.DLL内存管理.
13.   PostMessage只将消息放到消息队列中,需要排队等待处理。
SendMessage绕过消息队列直接发送到窗口过程,等到消息处理返回值才返回.
14.   鼠标移入移出消息:CM_MOUSEENTER,CM_MOUSELEAVE
15.
1.       产生随机密码(应该比较有用):office" />
function CreatePass:String;
const
MAX_LEN=10;
var
i: integer;
s: string;
begin
Randomize;
s:=‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘+‘abcdefghijklmnopqrstuvwxyz‘+ ‘0123456789‘;
Result := ‘;
for i := 0 to MAX_LEN-1 do
begin
Result := Result + s[Random(Length(s)-1)+1];
end;
end;
2.  十进制数字转换成罗马数字
function DecToRoman(iDecimal: longint): string;
const
aRomans: array[1..13] of string = (‘I‘, ‘IV‘, ‘V‘, ‘IX‘, ‘X‘, ‘XL‘, ‘L‘, ‘XC‘,
‘C‘, ‘CD‘, ‘D‘, ‘CM‘, ‘M‘);
aArabics: array[1..13] of integer = (1, 4, 5, 9, 10, 40, 50, 90, 100, 400,
500, 900, 1000);
var
i: integer;
begin
result := ‘;
for i := 13 downto 1 do
while (iDecimal >= aArabics[i]) do
begin
iDecimal := iDecimal - aArabics[i];
result := result + aRomans[i];
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(DecToRoman(5));
end;
3.       格式化整数显示
使用FormatFloat函数可以解决你很多问题。例如把1200000格式化为1,200,000输出
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
s:string;
begin
i := 1200000;
s := FormatFloat(‘#,0‘, i);
showmessage(s);
end;
4.       判断串口是否收到了数据可以使用ClearCommError函数,TcomStat结构体中cbInQue,cbOutQue可以帮助实现判断。
5.       RGB颜色转换为TColor类
function RGBToColor(R,G,B:Byte): TColor;
begin
Result:=B Shl 16 Or
G Shl 8 Or
R;
end;
6.       把TColor转换为RGB值
procedure TForm1.Button1Click(Sender: TObject);
var
Color: TColor;
R, G, B: Integer;
begin
Color := clBlack;
R := Color and $FF;
G := (Color and $FF00) shr 8;
B := (Color and $FF0000) shr 16;
showmessage(inttostr(R));
showmessage(inttostr(G));
showmessage(inttostr(B));
end;
7.       浏览计算机对话框
uses ShlObj;
function BrowseForComputer(const winhandle : THANDLE; const title : string) : string;
var
BrowseInfo: TBrowseInfo;
IDRoot: PItemIDList;
Path: array[0..MAX_PATH] of Char;
begin
SHGetSpecialFolderLocation(winHandle, CSIDL_NETWORK, IDRoot);
ZeroMemory(@BrowseInfo, SizeOf(TBrowseInfo));
ZeroMemory(@path, MAX_PATH);
BrowseInfo.hwndOwner := winhandle;
BrowseInfo.pidlRoot := IDRoot;
BrowseInfo.lpszTitle := PChar(title);
BrowseInfo.pszDisplayName := @path;
BrowseInfo.ulFlags:=BIF_BROWSEFORCOMPUTER;
SHBrowseForFolder(BrowseInfo);
end;