在 TCornerButton 的画布上绘图时位置错误

发布于 2024-12-09 15:09:16 字数 930 浏览 0 评论 0原文

我创建了一个继承自 TCornerButton 的新组件来添加下拉菜单选项。

第一个问题...

为什么我需要重写“AfterPaint”方法而不是文档中描述的“Paint”方法。覆盖“Paint”也不会导致任何绘图。

第二个问题...

当我使用“AfterPaint”方法绘制一个向下的小箭头时,其向右偏移约 5 个像素,直到按钮获得焦点。仅将鼠标悬停在按钮上似乎并不能解决问题(正如您期望发生的重新绘制一样)。我已经将其与代码中的“ScalePoint”和没有“ScalePoint”联系起来,但每次都是错误的。 它还在设计时显示在错误的位置。一旦按钮在运行时获得焦点,箭头就会移动到预期位置。当它失去焦点时,它会移回到错误的位置。 供参考。 “宽度”没有改变 - 我已经对此进行了测试。

procedure TLFButton.AfterPaint;
var
  down_arrow: TPolygon;
  x, y: Extended;
begin
  inherited;
  if FDropDownButton then
  begin
    Canvas.Fill := TText(FindStyleResource('text')).Fill;
    x := Width - 12;
    y := (Height / 2) - 3;
    SetLength(down_arrow, 3);
    down_arrow[0] := ScalePoint(PointF(x, y), Scale.X, Scale.Y);
    down_arrow[1] := ScalePoint(PointF(x+8,y), Scale.X, Scale.Y);
    down_arrow[2] := ScalePoint(PointF(x+4,y+6), Scale.X, Scale.Y);
    Canvas.FillPolygon(down_arrow, 255);
  end;
end;

I have created a new component that inherits from TCornerButton to add a drop down menu option.

First question...

Why do I need to override the "AfterPaint" method rather then the "Paint" method as described in the documentation. Overriding the "Paint" did nor result in any drawing.

Second question...

When I do use the "AfterPaint" method to draw a little down pointing arrow its offset right by about 5 pixels until the button gets focus. Just hovering over the button does not appear to fix the problem (as you would expect a repaint to occur). I have tied this with and without the "ScalePoint" in the code and every time its wrong.
It also shows in the wrong position at design time. As soon as the button is gets focus at runtime the arrow moves to the expected position. When it looses focus it moves back to the wrong position.
FYI. The "Width" is not changing - I have tested for that.

procedure TLFButton.AfterPaint;
var
  down_arrow: TPolygon;
  x, y: Extended;
begin
  inherited;
  if FDropDownButton then
  begin
    Canvas.Fill := TText(FindStyleResource('text')).Fill;
    x := Width - 12;
    y := (Height / 2) - 3;
    SetLength(down_arrow, 3);
    down_arrow[0] := ScalePoint(PointF(x, y), Scale.X, Scale.Y);
    down_arrow[1] := ScalePoint(PointF(x+8,y), Scale.X, Scale.Y);
    down_arrow[2] := ScalePoint(PointF(x+4,y+6), Scale.X, Scale.Y);
    Canvas.FillPolygon(down_arrow, 255);
  end;
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2024-12-16 15:09:16

另一种方法是使用 StyleLookup。
通过使用 StyleLookup,设计人员可以在不更改代码的情况下更改向下箭头。

constructor TLFButton.Create(AOwner: TComponent);
begin
  inherited;
  FDropDownButton := true;
end;

procedure TLFButton.SetDropDownButton(const Value: boolean);
var
  obj: TFmxObject;
begin
  FDropDownButton := Value;
  obj := FindStyleResource('dropdownbutton');
  if obj is TControl then
    TControl(obj).visible := Value;
end;

将下面的代码保存在 LFButton.Style 文本文件中并将其加载到样本中。
TPath = 向下的小箭头。

object _1: TLayout
  Align = alClient
  Position.Point = '(0,33)'
  Width = 842.000000000000000000
  Height = 715.000000000000000000
  object TLayout
    StyleName = 'LFButtonStyle'
    Position.Point = '(375,345)'
    Width = 91.000000000000000000
    Height = 24.000000000000000000
    DesignVisible = False
    object TRectangle
      StyleName = 'background'
      Align = alContents
      Width = 91.000000000000000000
      Height = 24.000000000000000000
      HitTest = False
      Fill.Kind = bkGradient
      Fill.Gradient.Points = <
        item
          Color = claWhite
        end
        item
          Color = xFFFEFEFE
          Offset = 0.472727268934249900
        end
        item
          Color = xFFDDDDDD
          Offset = 0.512727260589599600
        end
        item
          Color = xFFDBDBDB
          Offset = 1.000000000000000000
        end>
      Stroke.Color = xFF989898
      XRadius = 3.000000000000000000
      YRadius = 3.000000000000000000
      object TGradientAnimation
        Duration = 0.200000002980232200
        Trigger = 'IsMouseOver=true;IsPressed=false'
        StartValue.Points = <
          item
            Color = claWhite
          end
          item
            Color = xFFFEFEFE
            Offset = 0.472727268934249900
          end
          item
            Color = xFFDDDDDD
            Offset = 0.512727260589599600
          end
          item
            Color = xFFDBDBDB
            Offset = 1.000000000000000000
          end>
        StopValue.Points = <
          item
            Color = xFFA5D9FF
          end
          item
            Color = xFFA5D9FF
            Offset = 0.472727268934249900
          end
          item
            Color = xFFA5D9FF
            Offset = 0.512727260589599600
          end
          item
            Color = xFFA5D9FF
            Offset = 1.000000000000000000
          end>
        PropertyName = 'Fill.Gradient'
      end
      object TGradientAnimation
        Duration = 0.200000002980232200
        Trigger = 'IsMouseOver=false;IsPressed=false'
        StartValue.Points = <
          item
            Color = xFFA5D9FF
          end
          item
            Color = xFFA5D9FF
            Offset = 0.472727268934249900
          end
          item
            Color = xFFA5D9FF
            Offset = 0.512727260589599600
          end
          item
            Color = xFFA5D9FF
            Offset = 1.000000000000000000
          end>
        StopValue.Points = <
          item
            Color = claWhite
          end
          item
            Color = xFFFEFEFE
            Offset = 0.472727268934249900
          end
          item
            Color = xFFDDDDDD
            Offset = 0.512727260589599600
          end
          item
            Color = xFFDBDBDB
            Offset = 1.000000000000000000
          end>
        PropertyName = 'Fill.Gradient'
      end
      object TColorAnimation
        Duration = 0.200000002980232200
        Inverse = True
        Trigger = 'IsMouseOver=false;IsPressed=true'
        StartValue = xFF34BFFE
        StopValue = xFFA5D9FF
        PropertyName = 'Fill.Color'
      end
      object TColorAnimation
        Duration = 0.200000002980232200
        Trigger = 'IsMouseOver=true;IsPressed=true'
        StartValue = xFF34BFFE
        StopValue = xFFA5D9FF
        PropertyName = 'Fill.Color'
      end
    end
    object TText
      StyleName = 'text'
      Align = alClient
      Position.Point = '(5,3)'
      Locked = True
      Width = 66.000000000000000000
      Height = 18.000000000000000000
      Padding.Rect = '(5,3,5,3)'
      HitTest = False
      Text = 'button'
    end
    object TGlowEffect
      Trigger = 'IsFocused=true'
      Enabled = False
      Softness = 0.200000002980232200
      GlowColor = x82005ACC
      Opacity = 0.899999976158142100
    end
    object TLayout
      Align = alRight
      Position.Point = '(76,0)'
      Locked = True
      Width = 15.000000000000000000
      Height = 24.000000000000000000
      object TPath
        StyleName = 'dropdownbutton'
        Align = alCenter
        Position.Point = '(3,9)'
        Width = 8.000000000000000000
        Height = 5.000000000000000000
        HitTest = False
        Fill.Color = claBlack
        Stroke.Kind = bkNone
        Data.Path = {
          04000000000000000000000000000000010000000000803F0000000001000000
          0000003F0000803F030000000000000000000000}
      end
    end
  end
end

A different approach is to use a StyleLookup.
By using a StyleLookup, a designer is able to change the down pointing arrow without changing the code.

constructor TLFButton.Create(AOwner: TComponent);
begin
  inherited;
  FDropDownButton := true;
end;

procedure TLFButton.SetDropDownButton(const Value: boolean);
var
  obj: TFmxObject;
begin
  FDropDownButton := Value;
  obj := FindStyleResource('dropdownbutton');
  if obj is TControl then
    TControl(obj).visible := Value;
end;

Save code below in a LFButton.Style text file and load it in a StyleBook.
TPath = the little down pointing arrow.

object _1: TLayout
  Align = alClient
  Position.Point = '(0,33)'
  Width = 842.000000000000000000
  Height = 715.000000000000000000
  object TLayout
    StyleName = 'LFButtonStyle'
    Position.Point = '(375,345)'
    Width = 91.000000000000000000
    Height = 24.000000000000000000
    DesignVisible = False
    object TRectangle
      StyleName = 'background'
      Align = alContents
      Width = 91.000000000000000000
      Height = 24.000000000000000000
      HitTest = False
      Fill.Kind = bkGradient
      Fill.Gradient.Points = <
        item
          Color = claWhite
        end
        item
          Color = xFFFEFEFE
          Offset = 0.472727268934249900
        end
        item
          Color = xFFDDDDDD
          Offset = 0.512727260589599600
        end
        item
          Color = xFFDBDBDB
          Offset = 1.000000000000000000
        end>
      Stroke.Color = xFF989898
      XRadius = 3.000000000000000000
      YRadius = 3.000000000000000000
      object TGradientAnimation
        Duration = 0.200000002980232200
        Trigger = 'IsMouseOver=true;IsPressed=false'
        StartValue.Points = <
          item
            Color = claWhite
          end
          item
            Color = xFFFEFEFE
            Offset = 0.472727268934249900
          end
          item
            Color = xFFDDDDDD
            Offset = 0.512727260589599600
          end
          item
            Color = xFFDBDBDB
            Offset = 1.000000000000000000
          end>
        StopValue.Points = <
          item
            Color = xFFA5D9FF
          end
          item
            Color = xFFA5D9FF
            Offset = 0.472727268934249900
          end
          item
            Color = xFFA5D9FF
            Offset = 0.512727260589599600
          end
          item
            Color = xFFA5D9FF
            Offset = 1.000000000000000000
          end>
        PropertyName = 'Fill.Gradient'
      end
      object TGradientAnimation
        Duration = 0.200000002980232200
        Trigger = 'IsMouseOver=false;IsPressed=false'
        StartValue.Points = <
          item
            Color = xFFA5D9FF
          end
          item
            Color = xFFA5D9FF
            Offset = 0.472727268934249900
          end
          item
            Color = xFFA5D9FF
            Offset = 0.512727260589599600
          end
          item
            Color = xFFA5D9FF
            Offset = 1.000000000000000000
          end>
        StopValue.Points = <
          item
            Color = claWhite
          end
          item
            Color = xFFFEFEFE
            Offset = 0.472727268934249900
          end
          item
            Color = xFFDDDDDD
            Offset = 0.512727260589599600
          end
          item
            Color = xFFDBDBDB
            Offset = 1.000000000000000000
          end>
        PropertyName = 'Fill.Gradient'
      end
      object TColorAnimation
        Duration = 0.200000002980232200
        Inverse = True
        Trigger = 'IsMouseOver=false;IsPressed=true'
        StartValue = xFF34BFFE
        StopValue = xFFA5D9FF
        PropertyName = 'Fill.Color'
      end
      object TColorAnimation
        Duration = 0.200000002980232200
        Trigger = 'IsMouseOver=true;IsPressed=true'
        StartValue = xFF34BFFE
        StopValue = xFFA5D9FF
        PropertyName = 'Fill.Color'
      end
    end
    object TText
      StyleName = 'text'
      Align = alClient
      Position.Point = '(5,3)'
      Locked = True
      Width = 66.000000000000000000
      Height = 18.000000000000000000
      Padding.Rect = '(5,3,5,3)'
      HitTest = False
      Text = 'button'
    end
    object TGlowEffect
      Trigger = 'IsFocused=true'
      Enabled = False
      Softness = 0.200000002980232200
      GlowColor = x82005ACC
      Opacity = 0.899999976158142100
    end
    object TLayout
      Align = alRight
      Position.Point = '(76,0)'
      Locked = True
      Width = 15.000000000000000000
      Height = 24.000000000000000000
      object TPath
        StyleName = 'dropdownbutton'
        Align = alCenter
        Position.Point = '(3,9)'
        Width = 8.000000000000000000
        Height = 5.000000000000000000
        HitTest = False
        Fill.Color = claBlack
        Stroke.Kind = bkNone
        Data.Path = {
          04000000000000000000000000000000010000000000803F0000000001000000
          0000003F0000803F030000000000000000000000}
      end
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文