自动滚动窗体中不会显示滚动条

时间:2017-08-23 21:06:21

标签: delphi

------------------------- ORIGINAL QUESTION -------------------- -----

向所有Delphi开发人员致以问候!在Delphi 2006非MDI应用程序中,我创建了一个不可大小,可自动滚动,可自动调整的表单。这是表单单元的摘录:

uses Grid;

TGridFrm = class(TForm)
    public
        Grid : TGrid;
        constructor Create(AOwner : TComponent; Asize : TPoint);
end;

implementation

constructor TGridFrm.Create(AOwner: TComponent; Asize : TPoint);
begin
    inherited Create(aowner);
    borderstyle := bsSingle; // users are not allowed to resize the form
    windowstate := wsNormal;
    borderwidth := 0;
    autosize := True;
    autoscroll := True;
    constraints.maxwidth := screen.width - 1;
    constraints.maxheight := screen.height - 1;
    grid := TGrid.Create(asize.x, asize.y, self);
end;

现在,TGrid是一个自定义控件,当然还有自己的画布。这是其单位的摘录:

TGrid = class (TCustomControl)
    public
        NoOfCellsX,
        NoOfCellsY,
        CellSize : integer;
        procedure SetZoom(z : integer);
        constructor Create(AWidth, AHeight : Integer; AParent : TForm = nil);
end;

implementation

constructor TGrid.Create(AWidth, AHeight : Integer; AParent : TForm = nil);
begin
    inherited Create(AParent);
    Parent := AParent;
    align := alCustom;
    left := 0;
    top := 0;
end;

procedure TGrid.SetZoom(zoom : integer);
begin
    cellsize := zoom * 10 div 100;
    width := noofcellsx * cellsize;
    height := noofcellsy * cellsize;
end;

在表单的单元中,我已经安排了(通过ApplicationEvents对象),以便每当按下数字+/-键时,使用一些缩放值调用SetZoom。所有这一切背后的想法是让我的自定义控件捕捉到窗体的左上角(带有一些预定义的边距/边框宽度),并让整个窗体在我放大或缩小自定义控件时自动调整其大小,但是永远不会超出屏幕限制。它正常工作,但只能达到滚动条必须可见的程度:它们永远不会显示出来。因为这是一个可自动滚动的形式,所以当形式中的控件(在这种情况下为Grid)变得大于约束形式并且当它变小时,它们是否应该出现?我甚至尝试通过将SetZoom移动到表单的类来进行一些重构,但无济于事。我在这里缺少什么?

-----------------后续添加的可编辑代码------------------

项目文件:

program MyApp;

uses
    Forms,
    Grid in 'Source\Grid.pas',
    GridForm in 'Source\GridForm.pas' {GridFrm},
    Main in 'Source\Main.pas' {MainFrm};

{$R *.res}

begin
    Application.Initialize;
    Application.CreateForm(TMainFrm, MainFrm);
    Application.Run;
end.

The Main.pas:

unit Main;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
    Forms, Dialogs, StdCtrls;

type
    TMainFrm = class(TForm)
        CreateNewFormButton: TButton;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure CreateNewFormButtonClick(Sender: TObject);
    end;

var
    MainFrm: TMainFrm;

implementation

{$R *.dfm}

uses
    GridForm;

procedure TMainFrm.CreateNewFormButtonClick(Sender: TObject);
var aform : TForm;
begin
    aform := TGridFrm.Create(self, point(15, 15));
    aform.show;
    tgridfrm(aform).grid.SetZoom(100);
end;

procedure TMainFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Action := caFree;
end;

end.

GridForm.pas:

unit GridForm;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, Grid, AppEvnts;

type
    TGridFrm = class(TForm)
        ApplicationEvents1: TApplicationEvents;
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
    private
        TheGrid : TGrid;
    public
        property Grid : TGrid READ TheGrid WRITE TheGrid;
        constructor Create(AOwner : TComponent; ASize : TPoint);
    end;

var
    GridFrm: TGridFrm;

implementation

{$R *.dfm}

procedure TGridFrm.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
var keystate : TKeyboardState;
begin
    if not Active then begin exit; end;
    if msg.message = WM_KEYDOWN then
    begin
        getkeyboardstate(keystate);
        case msg.wparam of

            vk_Add : begin // zoom in
                grid.setzoom(grid.zoom + 10);
                handled := True;
            end;

            vk_Subtract : begin // zoom out
                grid.setzoom(grid.zoom - 10);
                handled := True;
            end;

            // other keys down here...

        end;
    end;
end;

constructor TGridFrm.Create(AOwner : TComponent; ASize : TPoint);
begin
    inherited Create(AOwner);
    borderstyle := bsSingle;
    borderwidth := 2;
    autosize := True;
    autoscroll := True;
    constraints.maxwidth := screen.width - 1;
    constraints.maxheight := screen.height - 1;
    visible := False;
    grid := TGrid.Create(asize.x, asize.y, random(800) + 500, self);
end;

procedure TGridFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin                
    Action := caFree;
end;

end.

Grid.pas:

unit Grid;

interface

uses
    StdCtrls, SysUtils, Controls, Forms, Graphics, Dialogs;

type
    TGrid = class (TCustomControl)
        Lbl1, Lbl2,
        GridSizeInfoLbl,
        FormSizeInfoLbl,
        WarningLbl : TLabel;

        public
            NoOfCellsX,
            NoOfCellsY,
            SquareSize, // in 1/1000ths of centimeter
            CellSize, // in pixels
            Zoom : integer;
            procedure SetZoom(z : integer);
            constructor Create(x, y, asquaresize : integer; AParent : TForm = nil);
    end;

implementation

uses
    GridForm;

constructor TGrid.Create(x, y, asquaresize : integer; AParent : TForm = nil);
begin
    inherited Create(AParent);
    parent := AParent;
    color := clTeal;
    align := alCustom;
    left := 0;
    top := 0;
    noofcellsx := x;
    noofcellsy := y;
    squaresize := asquaresize;
    Lbl1 := TLabel.Create(self);
    Lbl2 := TLabel.Create(self);
    GridSizeInfoLbl := TLabel.Create(self);
    FormSizeInfoLbl := TLabel.Create(self);
    WarningLbl := TLabel.Create(self);
    with Lbl1 do
    begin
        parent := self;
        caption := 'Size of grid: ';
        width := 55;
        height := 18;
        left := 2;
        top := 1;
    end;
    with Lbl2 do
    begin
        parent := self;
        caption := 'Size of form: ';
        width := 75;
        height := 18;
        left := 2;
        top := 19;
    end;
    with GridSizeInfoLbl do
    begin
        parent := self;
        width := 100;
        height := 18;
        left := 65;
        top := 1;
    end;
    with FormSizeInfoLbl do
    begin
        parent := self;
        width := 100;
        height := 18;
        left := 65;
        top := 19;
    end;
    with WarningLbl do
    begin
        parent := self;
        width := 150;
        height := 18;
        left := 2;
        top := 39;
    end;
end;

procedure TGrid.SetZoom(z : integer);
begin
    zoom := z;
    cellsize := (screen.pixelsperinch * squaresize * zoom) div (1000 * 254);
    width := noofcellsx * cellsize;
    height := noofcellsy * cellsize;

    GridSizeInfoLbl.caption := inttostr(Width) +
        'x' + inttostr(Height) +
        ' (zoom: ' + inttostr(zoom) +
        ', cellsize zoomed: ' + inttostr(cellsize) +
        ', squaresize: ' + inttostr(squaresize) +
        'mm, squares: ' + inttostr(noofcellsx) + 'x' + inttostr(noofcellsy) + ')';
    with tgridfrm(parent) do
    begin
        left := (screen.Width - width) div 2;
        top := (screen.Height - height) div 2;
        FormSizeInfoLbl.caption := inttostr(Width) + 'x' + inttostr(Height) +
        ' (clientarea: ' + inttostr(clientwidth) + 'x' + inttostr(clientheight) + ')';
        if self.width > clientwidth then
            if self.Height > clientheight then
                warninglbl.caption := 'Both scrollbars should appear!'
            else
                warninglbl.caption := 'Horizontal scrollbar should appear!'
        else if self.Height > clientheight then
            warninglbl.caption := 'Vertical scrollbar should appear!'
        else
            warninglbl.caption := 'No scrollbars needed';
    end;
end;

end.

代码简介:单击主窗体的按钮可创建一个可自动调整的窗体,该窗体又创建一个随机初始大小的子网格。数字+/-键使网格更大或更小,并且表格相应地自动调整,但没有滚动条显示,无论网格变得多大(我添加的标签提供视觉反馈)。 / p>

1 个答案:

答案 0 :(得分:1)

你的问题是双重的。

首先,正如杰瑞评论的那样,AutoSize。自动调整大小的目的是调整表单大小以使内容可见。当所有内容都可见时,就没有滚动条,所以很明显这两个属性是矛盾的。

因此VCL开发人员已经采取了他们的预防措施。以下是D2007来源:

function TScrollingWinControl.AutoScrollEnabled: Boolean;
begin
  Result := not AutoSize and not (DockSite and UseDockManager);
end;

如您所见,设置AutoScroll时设置AutoSize无效。

你可以覆盖这种行为,这是一种虚拟方法,如果它不会干扰第二次折叠。


现在您决定关闭自动调整大小并根据工作区大小自行计算和设置所需的表单大小,以解决您的第二个问题:网格控件的对齐。

当垂直滚动条想要查看是否需要调整时,下面是D2007代码:

  procedure ProcessVert(Control: TControl);
  begin
    if Control.Visible then
      case Control.Align of
        alTop, alNone:
          if (Control.Align = alTop) or (Control.Anchors * [akTop, akBottom] = [akTop]) then
            NewRange := Max(NewRange, Position + Control.Top + Control.Height);
        alBottom: Inc(AlignMargin, Control.Height);
      end;
  end;

正如您所看到的,如果控件没有alTopalBottomalNone对齐,则控件不会对自动垂直滚动条产生影响。你的alCustom

这也是为什么覆盖自动调整行为无法提供帮助的原因AutoSize取决于具有" left"," right"," top&#的控件34;,"底部"或"无"对齐控件。


考虑到VCL内部的工作原理,您必须重新设计控件。并非所有内部依赖方面都可以记录,因此您必须使用进行此类增强开发。