gpt4 book ai didi

delphi - 为什么我的滚动条并不总是正确绘制?

转载 作者:行者123 更新时间:2023-12-03 15:54:29 25 4
gpt4 key购买 nike

我有一个图形 TCustomControl 后代组件,上面有一个系统滚动条。问题是,当我将窗口移到屏幕之外然后将其拖回来时,滚动条就会消失(它没有被绘制)。我怎样才能解决这个问题 ?我在想,也许我应该在组件 Paint 中调用滚动条 Paint 方法,但我不知道怎么做。

这是代码。无需安装组件或在主窗体上放置任何内容,只需复制代码并分配 TForm1.FormCreate 事件:

Unit1.pas

unit Unit1;

interface

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

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;

var
Form1: TForm1;
List: TSuperList;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
List:=TSuperList.Create(self);
List.AlignWithMargins:=true;
List.Align:=alClient;
List.Visible:=true;
List.Parent:=Form1;
end;

end.

SuperList.pas

unit SuperList;

interface

uses Windows, Controls, Graphics, Classes, Messages, SysUtils, StdCtrls, Forms;

type

TSuperList = class(TCustomControl)
public
DX,DY: integer;
procedure Paint; override;
constructor Create(AOwner: TComponent); override;
procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
procedure CreateParams(var Params: TCreateParams); override;
published
property TabStop default true;
property Align;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Marus', [TSuperList]);
end;

procedure TSuperList.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style or WS_VSCROLL;
end;

procedure TSuperList.WMLButtonDown(var Message: TWMLButtonDown);
begin
DX:=Message.XPos;
DY:=Message.YPos;
Invalidate;
inherited;
end;

constructor TSuperList.Create(AOwner: TComponent);
begin
inherited;
DoubleBuffered:=true;
TabStop:=true;
Color:=clBtnFace;
BevelKind:=bkFlat;
Width:=200; Height:=100;
DX:=50; DY:=50;
end;

procedure TSuperList.Paint;
begin
Canvas.Brush.Color:=clWindow;
Canvas.FillRect(Canvas.ClipRect);
Canvas.TextOut(10,10,'Press left mouse button !');
Canvas.Brush.Color:=clRed;
Canvas.Pen.Color:=clBlue;
Canvas.Rectangle(DX,DY,DX+30,DY+20);
end;

end.

最佳答案

问题是由于设置 BevelKind:=bkFlat;

当在绘制控件的非客户区域期间调用 TWinControl.WMNCPaint 时,这将覆盖滚动条。

作为一个快速解决方法,您可以将 WMNCPaint 添加到控件并将区域更改为 1。然后 Delphi 将重新绘制整个非客户区域,这样效果会更好一些。

procedure WMNCPaint(var Message: TWMNCPaint); message WM_NCPAINT;

procedure TSuperList.WMNCPaint(var Message: TWMNCPaint);
var
TmpRgn: HRGN;
begin
TmpRgn := Message.RGN;
try
Message.RGN := 1;
inherited;
finally
Message.RGN := TmpRgn;
end;
// if you want to add some custom NC painting, you could do it here...
end;

更简洁的解决方案是您自己实现斜角绘画。这将减少闪烁。

关于delphi - 为什么我的滚动条并不总是正确绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26245565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com