• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

寻找windows的任务栏  _Delphi

delphi 搞代码 7年前 (2018-08-09) 208次浏览 已收录 0个评论

译:DBoy

    有些时候也许你想知道当前windows的任务栏在窗口的什么位置,这当然也可以用Delphi来实现。Windows API 函数允许您得到有关任务栏(也可称为应用程序栏)的信息。访问下面的微软MSDN地址可以了解到更多有关于这个函数调用的信息:       http://msdn.microsoft.com/library/psdk/shellcc/shell/Functions/SHAppBarMessage.htm 。这篇文档主要集中在为该函数传递ABM_GETTASKBARPOS消息上。
    你可以按照下面的方法自己创建一个应用程序,或从以下的Borland代码中心网址下载到程序的原代码:http://codecentral.borland.com/codecentral/ccweb.exe/download?id=15681 。
    首先,也许你很想知道这个API究竟是在哪个单元声明的。在MSDN中它被声明在shellapi.h里,而在Delphi中它被声明在shellapi.pas里。因此你要把shellapi添加到你的uses列表中。然后请看下面的函数示例:
// FindTaskBar 返回任务栏的位置,写到ARect中。
function FindTaskBar(var ARect: TRect): Integer;
var
AppData: TAppBarData;
begin

http://www.gaodaima.com/65712.html寻找windows的任务栏  _Delphi

  // ’Shell_TrayWnd’ 是任务栏窗口的名子
  AppData.Hwnd := FindWindow(’Shell_TrayWnd’, nil);
  if AppData.Hwnd = 0 then
    RaiseLastWin32Error;
  AppData.cbSize := SizeOf(TAppBarData);
  //当有错误发生时, SHAppBarMessage 会返回False (0)
  if SHAppBarMessage(ABM_GETTASKBARPOS, AppData) = 0 then
    raise Exception.Create(’SHAppBarMessage returned false when trying ’ +
      ’to find the Task Bar’’s position’);
  // 否则的话,我们就成功了,把结果写到Result中。
  Result := AppData.uEdge;
  ARect := AppData.rc;
end;

    当你把以上代码加到你的应用程序时,你就可以使用该函数了。添加一个TLabel和TButton到你的Form中,在Button的click事件中加入以下的代码。
var
  Rect: TRect;
  DestLeft: Integer;
  DestTop: Integer;
begin
  DestLeft := Left;
  DestTop := Top;
  case FindTaskBar(Rect) of
    ABE_BOTTOM:
    begin
      DestLeft := Trunc((Screen.Width – Width) / 2.0);
      DestTop := Rect.Top – Height;
    end;
    ABE_LEFT:
    begin
      DestTop := Trunc((Screen.Height – Height) / 2.0);
      DestLeft := Rect.Right;
    end;
    ABE_RIGHT:
    begin
      DestTop := Trunc((Screen.Height – Height) / 2.0);
      DestLeft := Rect.Left – Width;
    end;
    ABE_TOP:
    begin
      DestLeft := Trunc((Screen.Width – Width) / 2.0);
      DestTop := Rect.Bottom;
    end;
  end;
  Label1.Caption := Format(’Found at Top: %d Left: %d Bottom: %d Right: %d)’,
    [Rect.Top, Rect.Left, Rect.Bottom, Rect.Right]);
  // Move us to the task bar
  while (Left <> DestLeft) or (Top <> DestTop) do
  begin
    if Left < DestLeft then
      Left := Left + 1
    else if Left <> DestLeft then
      Left := Left – 1;

    if Top < DestTop then
      Top := Top + 1
    else if Top <> DestTop then
      Top := Top – 1;

    Application.ProcessMessages;
  end;
end;

欢迎大家阅读《寻找windows的任务栏  _Delphi,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:寻找windows的任务栏  _Delphi
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址