从样式Dialog / ShowMessage边框中排除VCL样式

时间:2015-03-14 07:14:02

标签: delphi vcl delphi-xe7 vcl-styles

有没有办法排除VCL样式的样式化系统对话框的边框。

特别是通过调用MessageDlg或ShowMessage显示的对话框。

我读了一些关于“德尔福之路”的文章(这是一个很棒的网站顺便说一句),但找不到答案。

这是我想要实现的目标:

现在(带有样式边框的碳风格):

now

目标(标准窗口边框的碳样式):

enter image description here

我仍然想要设置样式控件,但没有样式边框。

从父表单StyleElements中删除seBorder不起作用。

enter image description here

谢谢!

2 个答案:

答案 0 :(得分:4)

MessageDlg()ShowMessage()是Delphi VCL函数。他们动态创建Delphi TForm并显示它,因此您没有机会对其进行自定义。但是,您可以使用CreateMessageDialog()创建相同的TForm,然后根据需要修改其样式元素,然后显示它。例如:

function DoMessageDlgPosHelp(MessageDialog: TForm; X, Y: Integer): Integer;
begin
  with MessageDialog do
    try
      if X >= 0 then Left := X;
      if Y >= 0 then Top := Y;
      if (Y < 0) and (X < 0) then Position := poScreenCenter;
      Result := ShowModal;
    finally
      Free;
    end;
end;

procedure ShowStyledMessage(const Msg: string; const StyleElements: TStyleElements);
var
  Form: TForm;
begin
  Form := CreateMessageDialog(Msg, mtCustom, [mbOK]);
  Form.StyleElements := StyleElements;
  DoMessageDlgPosHelp(Form, -1, -1);
end;

这样称呼:

ShowStyledMessage('Some text', [seFont, seClient]);

对话框如下所示:

screenshot

答案 1 :(得分:3)

要设置没有边框的样式,您必须从表单的seBorder属性中删除StyleElements

  StyleElements := [seFont, seClient];

From form designer

但是你必须为每个表单设置该属性。如果我理解正确,您希望显示带有Windows边框的消息对话框。在这种情况下,为调用StyleElements的表单设置ShowMessage属性对对话框没有影响,因为这是完全新的形式。

你需要做的就是以某种方式设置StyleElements属性,以便Delphi创建的对话框形式超出你的范围。为此,您必须创建自己的表单StyleHook并替换为所有表单注册的TFormStyleHook

只需在项目中添加以下单元,所有表单都将具有Windows边框,而无需为每个表单明确设置。

unit WinBorder;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  Vcl.Themes,
  Vcl.Controls,
  Vcl.Forms;

type
  TWinBorderFormStyleHook = class(TFormStyleHook)
  protected
    procedure WndProc(var Message: TMessage); override;
  public
    constructor Create(AControl: TWinControl); override;
  end;

implementation

constructor TWinBorderFormStyleHook.Create(AControl: TWinControl);
begin
  inherited;
  OverridePaintNC := false;
end;

procedure TWinBorderFormStyleHook.WndProc(var Message: TMessage);
begin
  inherited;
  if Message.Msg = CM_VISIBLECHANGED then
    begin
      if (Control is TCustomForm) and (seBorder in TCustomForm(Control).StyleElements) then
        TCustomForm(Control).StyleElements := [seFont, seClient];
    end;
end;

initialization

  TCustomStyleEngine.UnRegisterStyleHook(TCustomForm, TFormStyleHook);
  TCustomStyleEngine.UnRegisterStyleHook(TForm, TFormStyleHook);
  TCustomStyleEngine.RegisterStyleHook(TCustomForm, TWinBorderFormStyleHook);
  TCustomStyleEngine.RegisterStyleHook(TForm, TWinBorderFormStyleHook);

finalization

  TCustomStyleEngine.UnRegisterStyleHook(TCustomForm, TWinBorderFormStyleHook);
  TCustomStyleEngine.UnRegisterStyleHook(TForm, TWinBorderFormStyleHook);
  TCustomStyleEngine.RegisterStyleHook(TCustomForm, TFormStyleHook);
  TCustomStyleEngine.RegisterStyleHook(TForm, TFormStyleHook);

end.
相关问题