计算机二级辅导:DelphiAPI函数详解
来源:优易学  2011-6-7 11:57:58   【优易学:中国教育考试门户网】   资料下载   IT书店
  控件与消息函数
  1、语法:AnyPopup: BOOL;
  单元:windows.pas(该单元DELPHI会自行在USES里加上,下同)
  作用:判断屏幕上是否存在任何弹出式窗口
  返回值:BOOL,如存在弹出式菜单,则返回TRUE
  注解:对该函数来说,弹出式菜单包含所有可见的包容顶级窗口,无论弹出式还是重叠窗口
  示例:
  procedure TForm1.Button1Click(Sender: TObject);
  begin
  if (AnyPopup) then
  Label1.Caption:=‘Pop-ups found: TRUE’
  else
  Label1.Caption:=‘Pop-ups found: FALSE’;
  end;
  2、语法:EnableWindow(hWnd: HWND; bEnable: BOOL): BOOL;单元:windows.pas
  作用:指定的窗口里允许或禁止所有鼠标及键盘输入
  返回值:BOOL,如果返回真,WINDOWS已经禁止,否则返回假
  示例:
  procedure TForm1.Button1Click(Sender: TObject);
  begin
  if (IsWindowEnabled(Edit1.Handle)) then
  begin
  EnableWindow(Edit1.Handle,FALSE);
  Button1.Caption:=‘Enable Window’;
  Edit1.Text:=‘This window is disabled’;
  end
  else
  begin
  EnableWindow(Edit1.Handle,TRUE);
  Button1.Caption:=‘Disable Window’;
  Edit1.Text:=‘This window is enabled’;
  end;
  end;
  3、语法:FlashWindow(hWnd: HWND; bInvert: BOOL): BOOL;
  单元:windows.pas
  作用:闪烁显示指定窗口。这意味着窗口的标题和说明文字会发生变化,似乎从活动切换到非活动状态、或反向切换。通常对不活动的窗口应用这个函数,引起用户的注意
  返回值:BOOL,如窗口在调用前处于活动状态,则返回TRUE
  注解:该函数通常与一个计数器组合使用,生成连续的闪烁效果。
  在windows nt及windowsfor workgroup中,bInvert参数会被忽略。
  但在windows 95中不会忽略
  示例:
  procedure TForm1.Timer1Timer(Sender: TObject);
  begin
  FlashWindow(Form1.Handle, TRUE);
  FlashWindow(Application.handle, TRUE);
  end;
  4、语法:SetWindowText(hWnd: HWND;lpString: PChar): BOOL;
  单元:windows.pas
  作用:设置窗口的标题文字或控件的内容
  返回值:设置成功返回TRUE,否则返回FALSE
  示例:
  procedure TForm1.Button1Click(Sender: TObject);
  var
  TheText: PChar;
  TextLen: Integer;
  begin
  TextLen:=GetWindowTextLength(Form1.Handle);
  GetMem(TheText,TextLen);
  GetWindowText(Form1.Handle,TheText,TextLen+1);
  Edit1.Text:=string(TheText);
  FreeMem(TheText);
  end;
  procedure TForm1.Button2Click(Sender: TObject);
  begin
  SetWindowText(Form1.Handle, PChar(Edit1.Text));
  end;
  5、语法:IsWindow(hWnd: HWND): BOOL;
  单元:windows.pas
  作用:判断一个窗口句柄是否有效
  返回值:有效返回TRUE,否则返回FALSE
  示例:
  procedure TForm1.Button1Click(Sender: TObject);
  begin
  if (IsWindow(Button1.Handle)) then
  Button1.Caption:=‘TRUE’
  else
  Button1.Caption:=‘FALSE’;
  end;
  怎么样,还过瘾吧?今天是第一次,就介绍些较容易接受的函数,否则朋友们肯定会喊吃不消。不知道朋友们对这样的编排形式能够接受吗?还有,我会按照API函数的分类(控件与消息函数/硬件与系统函数/菜单函数/文本和字体函数/打印函数等等)分别介绍,但我不会介绍全部的API函数,否则大有骗稿费之嫌疑,而且本人的水平也难做到每个语句都有示例,只介绍平常用得上的,本人经常使用的函数,有时也会介绍一下比较隐秘但却非常有用的API函数。
  附TIPS(DELPHI技巧)一个:
  如果有这样一个目录:
  c:\\windows\\media\\temp\\abc\\sound\\chime.wav
  我希望它能缩短成:
  c:\\windows\\..\\sound\\chime.wav
  如何写程序呢?
  回答:
  用下面的过程试试:
  function shortenfilename(s : string) : string;
  var drive,curdrive : string[2];
  dir,curdir : string[80];
  name : string[20];
  ext : string[5];
  i : byte;
  begin
  for i:=1 to length(s) do s[i]:=upcase(s[i]);
  s:=fexpand(s);
  fsplit(s,dir,name,ext);
  drive:=copy(dir,1,2);
  dir:=copy(dir,4,length(dir)-3);
  getdir(0,curdir);
  curdrive:=copy(curdir,1,2);
  curdir:=copy(curdir,4,length(curdir)-3)+‘\\’;
  if drive=curdrive then begin
  if copy(dir,1,length(curdir))=curdir then begin
  i:=length(curdir);
  if length(dir)<>i then dir:=dir+‘\\’;
  shortenfilename:=copy(dir,i+1,length(dir)-i-1)+name+ext;
  end else shortenfilename:=copy(s,3,length(s)-2);
  end else shortenfilename:=s;
  end;

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训