【问题标题】:Bugfix for BorderWidth > 0 in combination with a scroll bar?BorderWidth > 0 与滚动条结合的错误修复?
【发布时间】:2013-01-25 17:37:12
【问题描述】:

在编写自定义控件期间,在尝试正确实现默认 BorderWidth 属性时,我似乎偶然发现了显示滚动条时绘画行为中的一个错误:滚动条之间的空间和控件的范围是没有画。

要重现该错误,请为新项目的主窗体实现以下OnCreate 处理程序:

procedure TForm1.FormCreate(Sender: TObject);
begin
  AutoScroll := True;
  BorderWidth := 20;
  SetBounds(10, 10, 200, 200);
  with TGroupBox.Create(Self) do
  begin
    SetBounds(300, 300, 50, 50);
    Parent := Self;
  end;
end;

D7 和 XE2 的结果:

似乎这在 Delphi XE2 中终于得到了修复。很可能,这个 bug 存在于 TWinControl.WMNCPaint,但是查看 Controls.pas,我找不到 D7 和 XE2 之间的实现有任何显着差异。

我想得到答案:

  • 如何为这个奇怪的东西写一个错误修复,
  • 似乎从哪个 Delphi 版本修复了此错误。

【问题讨论】:

    标签: delphi scrollbar


    【解决方案1】:

    从哪个 Delphi 版本修复?

    BorderWidth 上 QualityCentral 中的 search results 表明此错误之前未报告过。错误QC 2433(已在 D2010 中解决,更新 4)似乎相关,但从 cmets 我了解到有问题的错误在 D2007 中不存在。

    不过,这里更需要社区的验证。

    如何修复版本

    覆盖WM_NCPAINT 消息处理程序:

      private
        procedure WMNCPaint(var Message: TWMNCPaint); message WM_NCPAINT;
    
    procedure TForm1.WMNCPaint(var Message: TWMNCPaint);
    {$IF CompilerVersion < 19}
    var
      DC: HDC;
      WindowStyle: Longint;
      TotalBorderWidth: Integer;
    {$IFEND}
    begin
    {$IF CompilerVersion < 19}
      DC := GetWindowDC(Handle);
      try
        WindowStyle := GetWindowLong(Handle, GWL_STYLE);
        if WindowStyle and WS_VSCROLL <> 0 then
          TotalBorderWidth := (Width - ClientWidth - GetSystemMetrics(SM_CXVSCROLL)) div 2
        else
          TotalBorderWidth := (Width - ClientWidth) div 2;
        if WindowStyle and WS_HSCROLL <> 0 then
          FillRect(DC, Rect(0, Height - TotalBorderWidth, Width, Height), Brush.Handle);
        if WindowStyle and WS_VSCROLL <> 0 then
          FillRect(DC, Rect(Width - TotalBorderWidth, 0, Width, Height), Brush.Handle);
      finally
        ReleaseDC(Handle, DC);
      end;
    {$IFEND}
      inherited;
    end;
    

    两个绘制的矩形故意太大,调整大小时效果更好。

    【讨论】:

    • 无法使用 D2007 复制。
    • @Ser 但是你确实有来自QC 2433的错误?
    • 我不确定。当我在显示表单之前设置颜色时,就没有问题了。如果我在运行时更改它,边框不符合新颜色。如报告中所述,调整大小会导致用正确的颜色绘制边框。
    • @Ser TControlScrollBar.IsScrollBarVisible 也是如此。但我没有使用它,因为此修复不仅适用于 TScrollingWinControl 后代,还适用于每个 TWinControl 后代。
    • @kobik 是的,这样更好。完成。
    猜你喜欢
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多