Delphi 没有滚动条以自动滚动的形式出现
-------------------------原始问题------------------------- 向所有Delphi开发人员致意!在Delphi2006非MDI应用程序中,我创建了一个不可缩放、可自动滚动、可自动调整大小的表单。这是表格单元的摘录:Delphi 没有滚动条以自动滚动的形式出现,delphi,Delphi,-------------------------原始问题------------------------- 向所有Delphi开发人员致意!在Delphi2006非MDI应用程序中,我创建了一个不可缩放、可自动滚动、可自动调整大小的表单。这是表格单元的摘录: uses Grid; TGridFrm = class(TForm) public Grid : TGrid; constructor Create(AOwner : TComponent; As
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。这一切背后的想法是让我的自定义控件捕捉到表单的左上角(具有一些预定义的边距/边框宽度),并让整个表单在我放大或缩小自定义控件时自动调整其大小,但永远不会超出屏幕限制。它正在工作,但仅限于滚动条必须可见的程度:它们永远不会出现。由于这是一个可自动滚动的表单,所以当表单中的控件(本例中为网格)大于受约束的表单时,它们不应该出现,而当它变小时,它们就不会出现吗?我甚至尝试了一些重构,将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.
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.
代码简介:点击主窗体的按钮创建一个可自动调整大小的窗体,从而创建一个具有随机初始大小的子网格。数字+/-键使网格变大或变小,表单也会相应地自动调整大小,但无论网格变得多大,都不会显示滚动条(我添加的标签提供了视觉反馈)。您的问题是双重的 第一个是,正如Jerry对这个问题的评论那样,
AutoSize
。“自动调整大小”的目的是调整表单大小,使内容可见。当所有内容都可见时,不可能有滚动条,因此这两个属性显然是矛盾的
因此,VCL开发人员已经采取了预防措施。以下为D2007来源:
function TScrollingWinControl.AutoScrollEnabled: Boolean;
begin
Result := not AutoSize and not (DockSite and UseDockManager);
end;
如您所见,当设置了AutoSize
时,设置AutoScroll
无效
你可以重写这个行为,这是一个虚拟的方法,如果它不干扰第二层的话
现在,您已经决定关闭“自动调整大小”,并根据工作区大小自行计算和设置表单所需的大小,现在可以解决第二个问题:网格控件的对齐 下面是垂直滚动条想要查看是否需要调整时的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;
如您所见,如果控件没有alTop
、alBottom
或alNone
对齐,则控件不会对自动垂直滚动条产生影响。您有alCustom
这也是为什么重写自动调整大小行为没有帮助的原因,AutoSize
依赖于控件具有“左”、“右”、“上”、“下”或“无”对齐控件
考虑到VCL内部的工作方式,您必须重新设计控件。并非所有的内部依赖方面都可以被记录,因此您必须使用源代码进行这种增强的开发。AutoSize和AutoScroll?我想他们会互相争斗。它们都会受到完全相同的影响:控件放置在其边界之外。发布可编译代码,以便我们可以查看问题是否存在。@SertacAkyuz我编写了重现问题的代码。我该怎么把它贴在这里?把它作为我问题的答案贴出来可以吗?还是这违反了规则?抱歉,我是新来的。请在问题本身中包含一个最小的示例,请参阅。感谢您更新代码,但它仍然不是MCVE。据我所知,您的问题似乎与这个自定义网格控件无关,而是与它的容器形式有关。这同样适用于任何控制,对吗?这就是MCVE的目的,消除与问题无关的一切,特别是“最小”部分。在创建MCVE的过程中,您很可能会自己发现问题,因为您不会有大量不相关的代码需要通读。