tags:

views:

142

answers:

3

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

A: 

What makes the scrollbars visible?

MB34
They are made visible by some MDI child forms that I drag a bit out of screen.
Altar
@Altar: Then check the handle of the MDI client window. See the `ClientHandle` property of `TForm`.
mghie
@mghie - it works!Maybe you can post a separate answer so I can accept it.
Altar
Please don't ask questions in an answer. If you can't comment yet, because your reputation is too low, then find a few questions you can answer, don't comment as an answer. Bad form.
Warren P
+1  A: 

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).

HeartWare
This is a very unexpected idea but it might work!
Altar
This should only be used in worst case, because it only works "in most cases". For instance, it is very possible to increase the thickness of the borders of windows, so that this will return true even in the absence of scrollbars.
Andreas Rejbrand
I agree with you Andreas, but you know, when nothing else works, you use what you have :) :)
Altar
+1 for HeartWare and +1 for mghie (his comment is the solution)
Altar
+1  A: 

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.

mghie