如何避免这种不必要的行为与Delphi的TSplitter和面板? [英] how do I avoid this unwanted behaviour with Delphi's TSplitter and panels?

查看:739
本文介绍了如何避免这种不必要的行为与Delphi的TSplitter和面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包括的是一个小项目,展示了我的问题。我有一个TPageControl对齐主窗体。在两个选项卡中的每一个上,我将面板客户端对齐。在每个面板上,我有2个子面板和一个分割器。 LH面板和分割器左对齐,RH面板客户端对齐。



基本上问题是2个选项卡之间的交互。演示:




  • 运行程序

  • 水平伸展主窗体。面板3将会增长

  • 将分隔符移动到正确的位置。 Panel2将会增长,Panel3将缩小到10像素的最小宽度限制。

  • 选择标签页2.面板5是按照设计,当主窗体拉伸时,面板6增长了

  • 将主窗体宽度缩小为原始宽度。面板6收缩太多(不需要)

  • 点击标签页1.主窗体再次增加宽度(不需要)



好的,这个行为在对齐面板的规则方面可能是可以解释的,但有人可以建议改进操作吗?

单位17元; 

接口

使用
Windows,消息,SysUtils,变体,类,图形,控件,表单,
对话框,ExtCtrls,ComCtrls;

type
TForm17 = class(TForm)
PageControl1:TPageControl;
TabSheet1:TTabSheet;
TabSheet2:TTabSheet;
Panel1:TPanel;
Panel2:TPanel;
Splitter1:TSplitter;
Panel3:TPanel;
Panel4:TPanel;
Splitter2:TSplitter;
Panel5:TPanel;
Panel6:TPanel;
private
{私人声明}
public
{公开声明}
end;

var
Form17:TForm17;

执行

{$ R * .dfm}

结束。


对象Form17:TForm17
Left = 0
顶部= 0
Caption ='Form17'
ClientHeight = 254
ClientWidth = 314
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name ='Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
对象PageControl1:TPageControl
左= 0
顶部= 0
宽度= 314
高度= 254
ActivePage = TabSheet1
Align = alClient
Constraints.MinWidth = 30
TabOrder = 0
ExplicitWidth = 480
对象TabSheet1:TTabSheet
Caption ='TabSheet1'
ExplicitWidth = 281
ExplicitHeight = 165
对象Panel1:TPanel
Left = 0
顶部= 0
宽度= 306
高度= 226
Align = alClient
Caption ='Panel1'
TabOrder = 0
ExplicitWidth = 109
ExplicitHeight = 165
对象Splitter1:TSplitter
左= 151
顶部= 1
宽度= 12
Height = 224
ExplicitLeft = 145
end
对象Panel2:TPanel
左= 1
顶部= 1
宽度= 150
Height = 224
Align = alLeft
Caption ='Panel2'
Constraints.MinWidth = 10
TabOrder = 0
end
对象Panel3:TPanel
左= 163
顶部= 1
宽度= 142
高度= 224
对齐= alClient
Caption ='Panel3'
约束。 MinWidth = 10
TabOrder = 1
ExplicitLeft = 141
ExplicitWidth = 330
end
end
end
object TabSheet2:TT abSheet
Caption ='TabSheet2'
ImageIndex = 1
ExplicitWidth = 281
ExplicitHeight = 165
对象Panel4:TPanel
左= 0
顶部= 0
宽度= 306
高度= 226
对齐= alClient
Caption ='Panel4'
TabOrder = 0
ExplicitWidth = 109
ExplicitHeight = 165
对象Splitter2:TSplitter
左= 149
顶部= 1
宽度= 11
高度= 224
ExplicitLeft = 141
end
对象Panel5:TPanel
左= 1
顶部= 1
宽度= 148
高度= 224
对齐= alLeft
标题='Panel5'
Constraints.MinWidth = 10
TabOrder = 0
end
对象Panel6:TPanel
左= 160
顶部= 1
宽度= 145
高度= 224
对齐= alClient
Caption ='Panel6'
Constraints.MinWidth = 10
TabOrder = 1
ExplicitLeft = 141
ExplicitWidth = 139
ExplicitHeight = 163
end
end
end
end
end
/ pre>

解决方案

要获得预期的行为,请删除约束( MinWidth )从你的面板。这些设置目前无效,因为您的分机具有 30(默认,未存储)的MinSize



编辑(对注释的响应):您不能指望位于分割器右侧的控件的MinWidth约束来调整左侧控件的大小。这只是逻辑的,约束是您设置的控件的属性。所有您将实现的是,如果您的控件已经处于MinWidth,则表单将拒绝收缩,因此您观察到,当您切换选项卡时,窗体越来越大的不良行为。你想要什么,你必须和代码一样 - 正如Marjan在他的回答中所说的那样。应该有多种方法来实现这一点,例如,将以下内容放在Panel3的OnCanResize事件中:

  TForm1.Panel3CanResize(发件人:TObject; var NewWidth,
NewHeight:Integer; var Resize:Boolean);
begin
如果NewWidth< Splitter1.MinSize然后
Panel2.Width:= Panel2.Width - Splitter1.MinSize + NewWidth;
结束


Included is a small project demonstrating my problem. I have a TPageControl aligned to the main form. On each of two tabsheets I have panels client aligned. On each of those panels I have 2 subpanels and a splitter. The LH panel and splitter is aligned left, the RH panel client-aligned.

Basically the problem is interaction between the 2 tabs. To demonstrate:

  • run the program
  • stretch the main form horizontally. Panel 3 will grow
  • move the splitter as far to the right as it will go. Panel2 will grow, Panel3 will shrink to it's 10 pixel min width constraint.
  • select tabsheet 2. Panel 5 is as designed, panel 6 grew when the main form was stretched
  • reduce the main form width to it's original width. Panel 6 shrinks too much (undesirable)
  • click on tabsheet 1. Main form increases in width again (undesirable)

OK, the behaviour is probably explainable in terms of the rules of aligned panels, but can anybody suggest improvements to the operation?

unit Unit17;

interface

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

type
  TForm17 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Panel1: TPanel;
    Panel2: TPanel;
    Splitter1: TSplitter;
    Panel3: TPanel;
    Panel4: TPanel;
    Splitter2: TSplitter;
    Panel5: TPanel;
    Panel6: TPanel;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form17: TForm17;

implementation

{$R *.dfm}

end.


object Form17: TForm17
  Left = 0
  Top = 0
  Caption = 'Form17'
  ClientHeight = 254
  ClientWidth = 314
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object PageControl1: TPageControl
    Left = 0
    Top = 0
    Width = 314
    Height = 254
    ActivePage = TabSheet1
    Align = alClient
    Constraints.MinWidth = 30
    TabOrder = 0
    ExplicitWidth = 480
    object TabSheet1: TTabSheet
      Caption = 'TabSheet1'
      ExplicitWidth = 281
      ExplicitHeight = 165
      object Panel1: TPanel
        Left = 0
        Top = 0
        Width = 306
        Height = 226
        Align = alClient
        Caption = 'Panel1'
        TabOrder = 0
        ExplicitWidth = 109
        ExplicitHeight = 165
        object Splitter1: TSplitter
          Left = 151
          Top = 1
          Width = 12
          Height = 224
          ExplicitLeft = 145
        end
        object Panel2: TPanel
          Left = 1
          Top = 1
          Width = 150
          Height = 224
          Align = alLeft
          Caption = 'Panel2'
          Constraints.MinWidth = 10
          TabOrder = 0
        end
        object Panel3: TPanel
          Left = 163
          Top = 1
          Width = 142
          Height = 224
          Align = alClient
          Caption = 'Panel3'
          Constraints.MinWidth = 10
          TabOrder = 1
          ExplicitLeft = 141
          ExplicitWidth = 330
        end
      end
    end
    object TabSheet2: TTabSheet
      Caption = 'TabSheet2'
      ImageIndex = 1
      ExplicitWidth = 281
      ExplicitHeight = 165
      object Panel4: TPanel
        Left = 0
        Top = 0
        Width = 306
        Height = 226
        Align = alClient
        Caption = 'Panel4'
        TabOrder = 0
        ExplicitWidth = 109
        ExplicitHeight = 165
        object Splitter2: TSplitter
          Left = 149
          Top = 1
          Width = 11
          Height = 224
          ExplicitLeft = 141
        end
        object Panel5: TPanel
          Left = 1
          Top = 1
          Width = 148
          Height = 224
          Align = alLeft
          Caption = 'Panel5'
          Constraints.MinWidth = 10
          TabOrder = 0
        end
        object Panel6: TPanel
          Left = 160
          Top = 1
          Width = 145
          Height = 224
          Align = alClient
          Caption = 'Panel6'
          Constraints.MinWidth = 10
          TabOrder = 1
          ExplicitLeft = 141
          ExplicitWidth = 139
          ExplicitHeight = 163
        end
      end
    end
  end
end 

解决方案

To get expected behavior remove the constraints (MinWidth) from your panels. These settings are currently ineffective anyway, since your splitters have a MinSize of '30' (the default, not stored).

edit (response to the comment): You cannot expect the 'MinWidth' constraint of a control that's at the right side of a splitter to adjust the size of the left-side control. That's only logical, the constraint is a property for the control that you set. All you'll achieve is that the form will deny shrinking if your control is already at its 'MinWidth', hence the undesirable behavior that you observe that the form is getting larger when you switch tabs. What you desire, you have to do with code - as Marjan told in his answer. There should be more than one way to achieve this, for instance, put the below to the Panel3's 'OnCanResize' event:

procedure TForm1.Panel3CanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  if NewWidth < Splitter1.MinSize then
    Panel2.Width := Panel2.Width - Splitter1.MinSize + NewWidth;
end;

这篇关于如何避免这种不必要的行为与Delphi的TSplitter和面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆