{ --▽---------------------------▼-- タスクバー位置取得関数 2000/11/07 TaskBarRect/TaskBarPosition/TaskBarHeight/TaskBarWidthを実装 2004/12/30 FW_TaskBarRect/FW_TaskTrayRectを実装 参考 タスクバーの設定を取得する http://homepage1.nifty.com/MADIA/delphi/Win32API/GetTask.htm //--▲---------------------------△-- } unit TaskbarUnit; interface uses SysUtils, Windows, Types, Forms; type TTaskBarPosition = (tpLeft, tpTop, tpRight, tpBottom); function TaskBarRect: TRect; function TaskBarPosition: TTaskBarPosition; function TaskBarHeight: Integer; function TaskBarWidth: Integer; function FW_TaskBarRect: TRect; function FW_TaskTrayRect: TRect; implementation uses ShellAPI; (*------------------------------- //タスクバーの位置をRectで返す関数 戻り値: 1152x864の環境 ↑ Top:-2=0-2 Bottom:33=× Left:-2=0-2 Right:1154=1152 + 2 ← Top:-2=0-2 Bottom:866=864+2 Left:-2=0-2 Right:71=× ↓ Top:831=× Bottom:866=864+2 Left:-2=0-2 Right:1154=1152 + 2 → Top:-2=0-2 Bottom:866=864+2 Left:1081=×Right:1154=1152 + 2 備考: 『タスクバーを自動で隠すOn/Off』や 『常に手前に表示On/Off』のプロパティは影響しません 履歴: 2000/11/07 -------------------------------*) function TaskBarRect: TRect; var apbData : TAPPBARDATA; begin apbData.cbSize := SizeOf(TAPPBARDATA); SHAppBarMessage(ABM_GETTASKBARPOS, apbData); Result := apbData.rc; end; (*------------------------------- //タスクバーの位置を上下左右で返す関数 戻り値: TTaskBarPosition = (tpLeft, tpTop, tpRight, tpBottom) 処理: 1.もしタスクバーTopの値が0以下ならtpTop 2.もしタスクバーBottomの値がScreen.Height以上ならtpBottom 3.として決定する 備考: raise Exception.Create(ErrCaption) のコードは実際には動作しないはず。 念のために入れてみた} 履歴: 2000/11/07 -------------------------------*) function TaskBarPosition: TTaskBarPosition; var i: Integer; TaskRect: TRect; DecisionFlag: Boolean; const ErrCaption: String = 'タスクバーの方向が決められませんでした'; begin TaskRect := TaskBarRect; Result := tpLeft; DecisionFlag := False; {決定フラグ} for i := Ord(Low(TTaskBarPosition)) to Ord(High(TTaskBarPosition)) do begin case i of ord(tpLeft): if not (TaskRect.Left <= 0) then begin Result := tpRight; if DecisionFlag then raise Exception.Create(ErrCaption) else DecisionFlag := true; end; ord(tpTop): if not (TaskRect.Top <= 0) then begin Result := tpBottom; if DecisionFlag then raise Exception.Create(ErrCaption) else DecisionFlag := true; end; ord(tpRight): if not (Screen.Width <= TaskRect.Right) then begin Result := tpLeft; if DecisionFlag then raise Exception.Create(ErrCaption) else DecisionFlag := true; end; ord(tpBottom): if not (Screen.Height<= TaskRect.Bottom) then begin Result := tpTop; if DecisionFlag then raise Exception.Create(ErrCaption) else DecisionFlag := true; end; end; //case end; //for if DecisionFlag = false then raise Exception.Create(ErrCaption); end; (*--▽---------------------------▼--BCBコードです //タスクバーの位置を上下左右で返す関数 TTaskBarPosition TaskBarPosition() { TRect TaskRect = TaskBarRect(); if ( (TaskRect.left<=0)&&(TaskRect.top<=0) ){ if (Screen->Width<=TaskRect.right) { return tpTop; } else { return tpLeft; } } else if ( (Screen->Width<=TaskRect.right)&&(Screen->Height<=TaskRect.bottom)) { if (TaskRect.left<=0) { return tpBottom; } else { return tpRight; } } return tpLeft; } //--▲---------------------------△--*) (*------------------------------- //タスクバーの高さを求める 戻り値: 高さ 備考: デスクトップの隅に埋め込まれているでいる分 (±2ピクセル分)を修正している 履歴: 2000/11/07 -------------------------------*) function TaskBarHeight: Integer; begin Result := 0; case TaskBarPosition of tpTop, tpBottom: Result := TaskBarRect.Bottom - TaskBarRect.Top -2; tpLeft, tpRight: Result := TaskBarRect.Bottom - TaskBarRect.Top -4; end; end; (*------------------------------- //タスクバーの幅を求める 戻り値: 幅 備考: デスクトップの隅に埋め込まれているでいる分 (±2ピクセル分)を修正している 履歴: 2000/11/07 -------------------------------*) function TaskBarWidth: Integer; begin Result := 0; case TaskBarPosition of tpLeft, tpRight: Result := TaskBarRect.Right - TaskBarRect.Left -2; tpTop, tpBottom: Result := TaskBarRect.Right - TaskBarRect.Left -4; end; end; {------------------------------- // タスクバー/タスクトレイの位置を返す関数 FW_TaskBarRect FW_TaskTrayRect 機能: FindWindowsExを使って タスクバー/タスクトレイの位置を取得する関数 備考: FW_TaskBarRectはTaskBarRectと互換のはず 履歴: 2004/12/30 //--▼----------------------▽--} //WindowsAPIのClientToScreen function Windows_ClientToScreen_Rect(h: HWND; r: TRect): TRect; var p1, p2: TPoint; begin //スクリーン座標を変換 p1 := Point(r.Left, r.Top); Windows.ClientToScreen(h, p1); p2 := Point(r.Right, r.Bottom); Windows.ClientToScreen(h, p2); Result := Rect(p1.x, p1.y, p2.x, p2.y); end; //Rect補正+4関数 function RectPlus4(r: TRect): TRect; begin Result := Rect(r.Left-4, r.Top -4, r.Right+4, r.Bottom+4) end; //タスクバーの位置を求める関数 //FindWindowEx版 function FW_TaskBarRect: TRect; var hwndTaskBar: HWND; begin //タスクバーのウインドウハンドルを取得 hwndTaskBar := FindWindowEx(0, 0, 'Shell_TrayWnd', nil); //タスクバーのサイズが取得できる Windows.GetClientRect(hwndTaskBar, Result); Result := Windows_ClientToScreen_Rect(hwndTaskBar, Result); Result := RectPlus4(Result); end; function FW_TaskTrayRect: TRect; var hwndTaskBar, hwndTaskTray: HWND; begin hwndTaskBar := FindWindowEx(0, 0, 'Shell_TrayWnd', nil); //タスクトレイのウインドウハンドルを取得 hwndTaskTray := FindWindowEx(hwndTaskBar, 0, 'TrayNotifyWnd', nil); Windows.GetClientRect(hwndTaskTray, Result); Result := Windows_ClientToScreen_Rect(hwndTaskTray, Result); Result := RectPlus4(Result); end; //--△----------------------▲-- end.