5

I have a form without caption, using on double click to maximize : Code looks like this:

procedure xxxxxx; 
begin
    if Form1.WindowState=wsNormal then
       begin
        Form1.WindowState:=wsMaximized;
        Form1.SetBounds(0,0,screen.Width,screen.Height-getHeightOfTaskBar);
       end
       else
       begin
         Form1.WindowState:=wsNormal;
       end;

       ShowTrayWindow;
end;
function getHeightOfTaskBar : integer;
var hTaskBar:HWND;
    rect : TRect;
begin
     hTaskbar := FindWindow('Shell_TrayWnd', Nil );
     if hTaskBar<>0 then
        GetWindowRect(hTaskBar, rect);

     Result:=rect.bottom - rect.top;
end;

This works good, except that I have to figure out where is task bar to reset SetBounds ...

What is the correct way to do this?

Thanks.

Irfan Mulic
  • 1,186
  • 3
  • 14
  • 28

2 Answers2

10

Sounds okay but like Drejc pointed out, the taskbar can appear anywhere, so too could additional docked sidebars like Google Desktop, Winamp, etc.

Instead perhaps use something like Screen.WorkAreaRect to get the client area of the screen. E.g.

with Screen.WorkAreaRect do
  Form1.SetBounds(Left, Top, Right - Left, Bottom - Top);
CodeAndCats
  • 7,508
  • 9
  • 42
  • 60
  • 3
    Good but this only works for the main monitor. To Maximize it on the current monitor you would need to do the following: with Screen.MonitorFromWindow(Form1.Handle).WorkAreaRect do Form1.SetBounds(Left, Top, Right - Left, Bottom - Top); – Name Jun 03 '13 at 13:07
0

One additional hint. The task bar can also be located on the right or the left of the screen (not only top and bottom). So you must additionally figure out where the task bar is.

I would suggest you look into the Delphi implementation of SetWidnowState. In Delphi7 it is this part of the code:

procedure TCustomForm.SetWindowState(Value: TWindowState);
const
  ShowCommands: array[TWindowState] of Integer =
    (SW_SHOWNORMAL, SW_MINIMIZE, SW_SHOWMAXIMIZED);
begin
  if FWindowState <> Value then
  begin
    FWindowState := Value;
    if not (csDesigning in ComponentState) and Showing then
      ShowWindow(Handle, ShowCommands[Value]);
  end;
end;

The ShowWindow is a Win32 library call:

function ShowWindow; external user32 name 'ShowWindow';

where user32 = 'user32.dll'; if I'm not mistaking. So dig into this library, maybe there is some info of TaskBar somewhere.

Drejc
  • 14,196
  • 16
  • 71
  • 106