Taskbar width, height and position
I need to get the width and height of the taskbar. Also I need the position 开发者_开发问答of the taskbar. How can I get this?
Depending on what you need that information for, you might want to look into Forms.TScreen.WorkAreaRect, because the work-area identified that way subtracts not only the TaskBar, but also any other "bar" that might limit available Desktop space.
You can simply use Screen.WorkAreaRect
from your code, because a Screen: TScreen
variable is declared in the Forms
unit and initialized by the VCL.
Well, since this question is composed on how to get the task bar coordinates I've decided to post another version of how to achieve this by using SHAppBarMessage function with ABM_GETTASKBARPOS message parameter.
I've posted two versions; one with and one without given task bar handle. Note that if you are sending ABM_GETTASKBARPOS message you should specify the hWnd member in the APPBARDATA structure as it's mentioned in the ABM_GETTASKBARPOS description. So the first version is safe for this case. The second one works though but it's formally wrong.
uses ShellAPI;
procedure TForm1.Button1Click(Sender: TObject);
var
Data: TAppBarData;
begin
Data.hWnd := FindWindow('Shell_TrayWnd', nil);
Data.cbSize := SizeOf(TAppBarData);
if Data.hWnd <> 0 then
if SHAppBarMessage(ABM_GETTASKBARPOS, Data) = 1 then
ShowMessage(
'Left: ' + IntToStr(Data.rc.Left) + 'px ; ' +
'Top: ' + IntToStr(Data.rc.Top) + 'px ; ' +
'Width: ' + IntToStr(Data.rc.Right - Data.rc.Left) + 'px ; ' +
'Height: ' + IntToStr(Data.rc.Bottom - Data.rc.Top) + 'px'
);
end;
Note that this version (where the TAppBarData.hWnd member is not specified) works though but it's wrong according to the MSDN.
procedure TForm1.Button2Click(Sender: TObject);
var
Data: TAppBarData;
begin
Data.cbSize := SizeOf(TAppBarData);
if SHAppBarMessage(ABM_GETTASKBARPOS, Data) = 1 then
ShowMessage(
'Left: ' + IntToStr(Data.rc.Left) + 'px ; ' +
'Top: ' + IntToStr(Data.rc.Top) + 'px ; ' +
'Width: ' + IntToStr(Data.rc.Right - Data.rc.Left) + 'px ; ' +
'Height: ' + IntToStr(Data.rc.Bottom - Data.rc.Top) + 'px'
);
end;
This is what I've got (made in the Win95 decade ;-) and still working):
- GetTaskBarBounds
- GetTaskBarSize
- GetTaskBarAlignment
Here is the code:
const
W95_EXPLORERCLASSNAME = 'Shell_TrayWnd';
function GetTaskBarBounds : TRect;
begin
GetWindowRect( FindWindow( W95_EXPLORERCLASSNAME, '' ), Result );
end;
function GetTaskBarSize : TPoint;
var
TaskBarBounds : TRect;
begin
TaskBarBounds:=GetTaskBarBounds;
with( TaskBarBounds ) do
Result:=Point( Right - abs( Left ), Bottom - abs( Top ) );
end;
function GetTaskBarAlignment : TAlign;
var
TaskBarBounds : TRect;
begin
Result:=alNone;
if( FindWindow( W95_EXPLORERCLASSNAME, '' ) > 0 ) then
begin
TaskBarBounds:=GetTaskBarBounds;
with( TaskBarBounds ) do
// At Left or at top of screen ?
if( Left <= 0 ) and ( Top <= 0 ) then
begin
if( Bottom >= 480 ) then
Result:=alLeft
else Result:=alTop;
end
else begin
if( Left <= 0 ) then
Result:=alBottom
else Result:=alRight;
end;
end;
end;
Here is how you get the orientation
type
TTaskBarPos = (_TOP, _BOTTOM, _LEFT, _RIGHT, _NONE);
function GetTaskBarPos: TTaskBarPos;
var
hTaskbar: HWND;
T: TRect;
scrW, scrH: integer;
begin
hTaskBar := FindWindow('Shell_TrayWnd', nil);
if hTaskbar <> 0 then
begin
GetWindowRect(hTaskBar, T);
ScrW := Screen.Width;
ScrH := Screen.Height;
if (T.Top > scrH div 2) and (T.Right >= scrW) then
Result := _BOTTOM
else if (T.Top < scrH div 2) and (T.Bottom <= scrW div 2) then
Result := _TOP
else if (T.Left < scrW div 2) and (T.Top <= 0) then
Result := _LEFT
else
if T.Left >= ScrW div 2 then
Result := _RIGHT;
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
var
TaskBarPos: TTaskBarPos;
begin
TaskBarPos := GetTaskBarPos;
case TaskBarPos of
_LEFT: ShowMessage('Left Position');
_TOP: ShowMessage('Top Position');
_RIGHT: ShowMessage('Right Position');
_BOTTOM: ShowMessage('Bottom Position');
end;
end;
I got this code from http://www.delphitips.net/2007/08/26/taskbar-position I tried this and works fine. It works even the size of the taskbar changes.
The code is put below.
procedure TForm1.Button1Click(Sender: TObject);
var
hTaskbar: HWND;
T: TRect;
ScrW, ScrH: Integer;
begin
ScrW := Screen.Width;
ScrH := Screen.Height;
hTaskBar := FindWindow('Shell_TrayWnd', nil);
GetWindowRect(hTaskBar, T);
if (T.Top > ScrH div 2) and (T.Right >= ScrW) then
ShowMessage('Bottom of the screen')
else if (T.Top < ScrH div 2) and (T.Bottom <= ScrW div 2) then
ShowMessage('Top of the screen')
else if (T.left < ScrW div 2) and (T.Top <= 0) then
ShowMessage('Left side of the screen')
else
ShowMessage('Right side of the screen');
end;
精彩评论