译: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 // ’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;
|