Correct way to maximize form in delphi (without caption)
Asked Answered
A

3

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.

Aircool answered 19/12, 2008 at 6:7 Comment(0)
L
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);
Lindblad answered 19/12, 2008 at 7:14 Comment(1)
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);Sliwa
A
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.

Absorptance answered 19/12, 2008 at 7:0 Comment(0)
C
-1

Please use this one line of Code to maximize a form:

postmessage(handle,WM_SYSCOMMAND,SC_MAXIMIZE,0);
Consociate answered 6/1 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.