面板显示在网格滚动条下

时间:2016-11-03 19:28:28

标签: delphi

我在我的项目中使用了Windows 10主题,我已经注意到:位于网格边缘的面板,它们显示在网格滚动条下, 喜欢这张图片:

enter image description here

我没有改变VCL的任何行为,也没有改变网格或滚动行为。

pas文件:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls,
  Datasnap.DBClient, Vcl.Grids, Vcl.DBGrids, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    DbGrid: TDBGrid;
    Panel2: TPanel;
    ClientDataSet: TClientDataSet;
    DataSource1: TDataSource;
    ButtonAdd: TButton;
    ShowPanel: TButton;
    ClientDataSetname: TStringField;
    ClientDataSetaddress: TStringField;
    procedure ButtonAddClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ShowPanelClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonAddClick(Sender: TObject);
begin
  ClientDataset.Append;
  ClientDataSetname.AsString := 'Test name';
  ClientDataSetaddress.AsString := 'Test address';
  ClientDataset.Insert;
end;

procedure TForm1.ShowPanelClick(Sender: TObject);
begin
  if Panel2.Visible then
     Panel2.Visible := False
  else
    Panel2.Visible := True;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClientDataset.CreateDataSet;
end;

end.

dfm文件:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 201
  ClientWidth = 555
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 460
    Top = 0
    Width = 95
    Height = 201
    Align = alRight
    TabOrder = 0
    object ButtonAdd: TButton
      Left = 10
      Top = 16
      Width = 75
      Height = 25
      Caption = 'ButtonAdd'
      TabOrder = 0
      OnClick = ButtonAddClick
    end
    object ShowPanel: TButton
      Left = 10
      Top = 47
      Width = 75
      Height = 25
      Caption = 'ShowPanel'
      TabOrder = 1
      OnClick = ShowPanelClick
    end
  end
  object DbGrid: TDBGrid
    Left = 0
    Top = 0
    Width = 460
    Height = 201
    Align = alClient
    DataSource = DataSource1
    TabOrder = 1
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'Tahoma'
    TitleFont.Style = []
    Columns = <
      item
        Expanded = False
        FieldName = 'name'
        Visible = True
      end
      item
        Expanded = False
        FieldName = 'address'
        Visible = True
      end>
  end
  object Panel2: TPanel
    Left = 0
    Top = 160
    Width = 185
    Height = 41
    Caption = 'panel2'
    TabOrder = 2
    Visible = False
  end
  object ClientDataSet: TClientDataSet
    Aggregates = <>
    Params = <>
    Left = 216
    Top = 104
    object ClientDataSetname: TStringField
      FieldName = 'name'
      Size = 50
    end
    object ClientDataSetaddress: TStringField
      FieldName = 'address'
      Size = 50
    end
  end
  object DataSource1: TDataSource
    DataSet = ClientDataSet
    Left = 152
    Top = 88
  end
end

第二次点击ShowPanel后会发生错误。

3 个答案:

答案 0 :(得分:0)

您可以将DBgrid定义为面板的父级。

procedure TForm1.FormShow(Sender: TObject);
begin
  panel1.Parent := dbgrid1;
  panel1.align := alBottom;
end;

答案 1 :(得分:0)

我找到了一种解决这个问题的方法,只需将网格seBorder更改为false,我使用Notepad ++在所有项目中找到“:TDBGrid”¹并替换为“:TDBGrid StyleElements = [seFont ,seClient]“¹。我认为这不是解决此问题的更好方法,因为将seBorder更改为false会使滚动条样式看起来像Windows版本的滚动条。

¹尝试时忽略引号。

答案 2 :(得分:-1)

实际上,唯一的方法是使用DBgrid的父级。 这个例子适合我:

procedure TForm1.adjustPanelTo(const aPanel: TPanel; aWcontrol: TWinControl);
begin
  if aPanel = nil then Exit;
  if aWcontrol = nil then Exit;
  if aWcontrol.Parent = nil then Exit;

  aPanel.Parent := aWcontrol.Parent;
  aPanel.Anchors := [akLeft, akTop];
  aPanel.Left := aWcontrol.Left + 1;
  aPanel.Top := aWcontrol.Top + aWcontrol.ClientHeight - aPanel.Height;
  aPanel.BringToFront;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  adjustPanelTo(Panel1, DBGrid1);
end;