譯:DBoy
有些時候也許你想知道當(dāng)前windows的任務(wù)欄在窗口的什么位置,這當(dāng)然也可以用Delphi來實現(xiàn)。Windows API 函數(shù)允許您得到有關(guān)任務(wù)欄(也可稱為應(yīng)用程序欄)的信息。訪問下面的微軟MSDN地址可以了解到更多有關(guān)于這個函數(shù)調(diào)用的信息: http://msdn.microsoft.com/library/psdk/shellcc/shell/Functions/SHAppBarMessage.htm 。這篇文檔主要集中在為該函數(shù)傳遞ABM_GETTASKBARPOS消息上。 你可以按照下面的方法自己創(chuàng)建一個應(yīng)用程序,或從以下的Borland代碼中心網(wǎng)址下載到程序的原代碼:http://codecentral.borland.com/codecentral/ccweb.exe/download?id=15681 。 首先,也許你很想知道這個API究竟是在哪個單元聲明的。在MSDN中它被聲明在shellapi.h里,而在Delphi中它被聲明在shellapi.pas里。因此你要把shellapi添加到你的uses列表中。然后請看下面的函數(shù)示例: // FindTaskBar 返回任務(wù)欄的位置,寫到ARect中。 function FindTaskBar(var ARect: TRect): Integer; var AppData: TAppBarData; begin // ’Shell_TrayWnd’ 是任務(wù)欄窗口的名子 AppData.Hwnd := FindWindow(’Shell_TrayWnd’, nil); if AppData.Hwnd = 0 then RaiseLastWin32Error; AppData.cbSize := SizeOf(TAppBarData); //當(dāng)有錯誤發(fā)生時, 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’); // 否則的話,我們就成功了,把結(jié)果寫到Result中。 Result := AppData.uEdge; ARect := AppData.rc; end;
當(dāng)你把以上代碼加到你的應(yīng)用程序時,你就可以使用該函數(shù)了。添加一個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;
|