1
votes

I cannot detect if the scrollbars of a form are visible. Googleing the Internet shows that the code below should work. Everybody uses it:

function VertScrollBarVisible(WindowHandle: THandle): Boolean;
begin
 Result:= (GetWindowlong(WindowHandle, GWL_STYLE) AND WS_VSCROLL) <> 0  
end;

I call it like this:

procedure TFrmBaser.Button1Click(Sender: TObject);
begin
 if VertScrollBarVisible(MainForm.Handle) 
 then Caption:= 'visible';
end;

It returns False all the time, even is the scrollbars are visible. They are made visible by some MDI child forms that I drag a bit out of screen.


Delphi 7, Win XP SP3, Themes on

2
So you have this code, and you call it like that... and what happens?Mason Wheeler
Sorry. It returns False all the time.Server Overflow
NB: MDI is severely declasse, and for a lot of good reasons.Warren P

2 Answers

1
votes

This

It returns False all the time, even is the scrollbars are visible. They are made visible by some MDI child forms that I drag a bit out of screen.

shows that the form you are having problems with is a MDI parent form (FormStyle is fsMDIForm).

MDI parent forms are different from normal forms in that they create a special client window that fills the whole client area of the form, and which manages the MDI child windows / forms. The MDI client window will never be larger than the client area of its parent, so the parent form will never show scrollbars. This explains that the code in your question always returns false.

The scrollbars you see are part of the MDI client window. Modify your code to check the window style of the client window, its handle can be accessed with the ClientHandle property of the MDI parent form:

procedure TFrmBaser.Button1Click(Sender: TObject);
begin
  if VertScrollBarVisible(MainForm.ClientHandle) then
    Caption := 'visible';
end;

For more information about MDI at the Windows API level see About the Multiple Document Interface on MSDN.

1
votes

Try this:

function VertScrollBarVisible(Form : TForm) : Boolean;
  begin
    Result:=(Form.Width-Form.ClientWidth>10)
  end;

I'm not sure if it works, but it compares the "available" width of the form against the "total" width of the form (normally they are within 2-3 pixels of each other, but with a scroll bar, the available width should be significantly lower).