delphi tips and articles 4

来源:互联网 发布:jquery1.2.min.js下载 编辑:程序博客网 时间:2024/04/29 03:16

1用回车键代替Tab键 

把Form的KeyPreview属性设为True
在Form的OnKeyDown事件处理程序中:
  if Key = VK_RETURN then
  begin
    Key := VK_TAB;
    FindNextControl(ActiveControl, True, True, False).SetFocus;
  end

但是,发现对cxgrid类的控件上述代码无法完成需要的功能,

而是如下:


    if   key   =   13   then  
      begin  
          Key   :=   0;  
          PostMessage(Handle,   WM_NEXTDLGCTL,   0,   0);  
      end;  

2TDBComboBox的高度受到字体大小的影响,要改变其高度,必须改变其字体大小。

3TDBEdit的高度受autosize控制,为false时才能自定义高度。

4动态生成一类对象(基本结构如下,可能要调整局部)

function createComponent(aowner:TComponent;AClass:TComponentClass):TComponent;

var

  instance:TComponent;

begin

  instance := TComponent(aclass.Newinstance);

  try

  instance.create(aowner);

  except

  raise;

end;

result:= instance;

end;

5variant为空,为nil的判断:

VarType(variant)

varEmpty The variant is Unassigned.
varNull The variant is Null.
varSmallint 16-bit signed integer (type Smallint in Delphi, short in C++ ).
varInteger 32-bit signed integer (type Integer in Delphi, int in C++).
varSingle Single-precision floating-point value (type Single in Delphi, float in C++).
varDouble Double-precision floating-point value (type double).
varCurrency Currency floating-point value (type Currency).
varDate Date and time value (type TDateTime).

varOleStr Reference to a dynamically allocated UNICODE string.
varDispatch Reference to an Automation object (an IDispatch interface pointer).
varError Operating system error code.
varBoolean 16-bit boolean (type WordBool).
varVariant A variant.
varUnknown Reference to an unknown object (an IInterface or IUnknown interface pointer).
varShortInt 8-bit signed integer (type ShortInt in Delphi or signed char in C++)
varByte A Byte
varWord unsigned 16-bit value (Word)

varLongWord unsigned 32-bit value (type LongWord in Delphi or unsigned long in C++)
varInt64 64-bit signed integer (Int64 in Delphi or __int64 in C++)
varStrArg COM-compatible string.
varString Reference to a dynamically allocated string (not COM compatible).
varAny A CORBA Any value.

 

6 QRLoopBand不知道从哪个版本开始支持,但是3。62以上肯定可以支持。

delphi7的默认版本不支持。

7qreport 的 usestandardprinter does not exists错误:

在Delphi7上的默认qreport版本较低,装了qreport4。03后,报错usestandardprinter does not exists:基本原因是与以前的版本有冲突。

解决:从delphi7/bin和/lib中删除qr*.*文件,然后安装qreport.exe,然后从组件安装中安装dpl文件,然后如果运行时提示缺少dcu文件或编译版本不合等, 可能需要在tools中的environment options选择lib的路径(指向qreport的安装路径下的quickrep和lib)。

8quickrep.preview之前的显示格式控制。4。03可用,其他版本不知。

quickrep.PrevInitialZoom := qrZoomToFit;
quickrep.PreviewInitialState := wsmaximized;

TQRZoomState = (qrZoomToFit,qrZoomToWidth,qrZoomOther)

PreviewInitialState  :TWindowState类型

9 qreport浏览a3的qrp时 看到的与设计期的qrp不一样,简单来说就是被压缩了。

设计期                                                                                              重新浏览时

这个问题是由quickrep.qrprinter未根据qrp文件的实际情况正确设置引起的。

QuickRep.Prepare;
quickrep.load('d:/test.qrp');
    quickrep.QRPrinter.PreviewModal;

通常如上来浏览一个qrp。

QuickRep.Prepare;根据系统的打印机设置初始化quickrep.qrprinter。所以修改系统的打印机属性可以控制qrprinter的初始属性。如默认的纸张大小,但是纸张的打印方向修改了没有用,似乎。原因不明。

通常系统的打印机的纸张大小为a4,所以出现上述问题。

解决方法有二:1手动修改系统的打印机属性。

2

QuickRep.Prepare;

    quickrep.Printer.PaperSize := a3;
quickrep.load('d:/test.qrp');

。。

加上语句如上。

如果test.qrp是a4的,不论其打印方向,quickrep.load()之后,qrprinter的属性会根据实际情况改变(即a4,polandscape或poPortrait)。

但是如果test.qrp是a3,poLandscape的话,qrprinter的打印方向不会变到poLandscape。原因不明。

 10 http://bbs.fi123.com/showthread.php?p=13871

Q :--Delphi中怎么将实数取整?

Re:

  floor 和 ceil 是 math unit 里的函数,使用前要先 Uses Math。

  trunc 和 round 是 system unit 里的函数,缺省就可以用。

   floor 直接往小的取,比如 floor(-123.55)=-124,floor(123.55)=123

   trunc 直接切下整数,比如 trunc(-123.55)=-123, floor(123.55)=123

   ceil 直接往大的取,比如 ceil(-123.55)=-123, ceil(123.55)=124

   round 计算四舍五入,比如 round(-123.55)=-124,round(123.55)=124

Q :--Delphi中如何得到程序的路径?

Re:

  使用如下的语句即可:

   ExtractFilePath(Application.ExeName)
   ExtractFileDir(Application.Exename)

  它们分别用来获取应用程序的路径名,但前者其结尾字符总是“/”;而后者不返回“/”;除非是在根目录下,程序举例如下:

   procedure TForm1.Button1Click(Sender: TObject);   
   begin
    ShowMessage(ExtractFileDir(Application.Exename));    // 如: c:/temp
    ShowMessage(ExtractFilePath(Application.Exename));   // 如: c:/temp/      
   end; 

Q :--Delphi里去掉字符串中空格的函数是什么?

Re:

  Trim()去掉字符串左右两边的空格和控制符,它的使用语法如下:
   function Trim(const S: string): string;
  TrimLeft()用于去除字符串左边的空格,它的使用方法如下:
   function TrimLeft(const S: string): string;
  TrimRight()用于去除字符串右边的空格,它的使用方法如下:
   function TrimRight(const S: string): string;
  如果要去除字符串中间的空格就需要自己写程序来实现了,下面提供了一种方法,其主要思路是利用Pos()函数得到空格所在整个字符串中的位置,然后以此位置为界把整个字符串分别存到两个变量中,然后Trim掉有空格的那一部分,在把两个字符串合并,然后重复上述过程,直到Pos()函数返回0也就是没有找到空格为止。

11 在formcreate中退出form

用close是不行的。

PostMessage(Handle, WM_CLOSE, 0, 0);

12 可以用表名.*来选择表中的所有字段

select a.* b.field1 from a ,b

13 application.messagebox要用到的unit:controls,windows

14 当新插入一条记录时,如果希望某些字段有默认值,则在dataset的onnewrecord事件中

dataset.SetFields(['1','2','3']);

15 delphi中的输入对话框

procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
s:= InputBox('请输入姓名','姓名:','');//参数分别为标题,提示,默认值
ShowMessage(s);
end;


 

16 Varint类型到String的转换

string := vartoStr(variant);

还有很多类似的转换toBCD,toDateTime等。


17 delphi中的类常量

方法如下:

tconst = class
public
  function  getconstant : string;
  property constant : string read getconstant;
end;

  function  getconstant : string;
begin
  result := 'fred';
end;

18 用数组作为函数的返回值

定义一个类型
type
TMyArray = array[0..n] of integer

function MyFunction : TMyArray;

19 带标题的messagedlg

QDialogs.MessageDlg('标题','内容',mtconfirmation, [mbyes, mbno], 0)才可以用。

 20 设置双击时间

// Set example:
procedure TForm1.Button1Click(Sender: TObject);
begin 
  // will reset after system start
  
SetDoubleClickTime(1500);
end;

// Get example:
procedure TForm1.Button2Click(Sender: TObject);
begin
  
ShowMessage(IntToStr(GetDoubleClickTime));
end;

21 在delphi把字符串分割成一维数组

type userarray=array of string;  

  function tform1.split(s:string;dot:char):userarray;
    var
     str:userarray;
     i,j:integer;
    begin
       i:=1;
       j:=0;
       SetLength(str, 255);
       while Pos(dot, s) > 0 do
       begin
        str[j]:=copy(s,i,pos(dot,s)-i);
        i:=pos(dot,s)+1;
        s[i-1] := chr(ord(dot)+1);
        j:=j+1;
       end;
       str[j]:=copy(s,i,strlen(pchar(s))-i+1);
      result:=str;
    end;

原创粉丝点击