如何给Panel加上滚动条?(300分)

  • 主题发起人 主题发起人 ak_2004
  • 开始时间 开始时间
A

ak_2004

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在需要给Panel加上滚动条,不要告诉我用ScrollBox.因为我现在需要改原来的程序,但不改控件。
我现在通过覆盖TPanel的CreateParams方法已经给Panel加上了滚动条,但是该滚动条还不能滚动,请问如何才能实现该功能,请指点。
我的测试代码:
unit Unit1;

interface

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

type
TPanel =class(ExtCtrls.TPanel)
protected
procedure CreateParams(var Params: TCreateParams); override;
end;

TForm1 = class(TForm)
Panel1: TPanel;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

{ TPanel }

procedure TPanel.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
with Params do
Style :=Style or WS_HSCROLL or WS_VSCROLL;
end;

end.
 
简直是自己开发类似与SCROLL控件一样,我一点该,怎么改都象抄袭SCROLL代码。
 
type
TMyPanel=class(TPanel)
private
FHDelta,FVDelta:Integer;
FHMax,FVMax:Integer;
FHPageSize,FVPageSize:Integer;
procedure WMHScroll(var Message:TMessage);message WM_HSCROLL;
procedure WMVScroll(var Message:TMessage);message WM_VSCROLL;
protected
procedure CreateParams(var Params: TCreateParams); override;
public
constructor Create(Owner:TComponent);override;
end;

{ TMyPanel }

constructor TMyPanel.Create(Owner: TComponent);
begin
inherited Create(Owner);
if Owner is TWinControl then
Parent:=Owner as TWinControl;
FHDelta:=0;
FVDelta:=0;
FHMax:=100;
FVMax:=100;
FHPageSize:=10;
FVPageSize:=10;
end;

procedure TMyPanel.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
with Params do
Style :=Style or WS_HSCROLL or WS_VSCROLL;
end;

procedure TMyPanel.WMHScroll(var Message: TMessage);
var
lPos:Integer;
begin
inherited;
lPos:=GetScrollPos(Handle,SB_HORZ);
case LoWord(Message.WParam) of
SB_LINELEFT:
lPos:= lPos - 1 ;
SB_LINERIGHT:
lPos:= lPos + 1 ;
SB_PAGEDOWN:
lPos:= lPos + FHPageSize;
SB_PAGEUP:
lPos:= lPos - FHPageSize;
SB_THUMBTRACK:
lpos:=HiWord(Message.WParam);
end;
if lPos<0 then lPos:=0;
if lPos>FHMax then lPos:=FHMax;
SetScrollPos(Handle,SB_HORZ,lPos,True);
ScrollWindow(Handle,FHDelta-lPos,0,nil,nil);
FHDelta:=lPos;
Invalidate;
end;

procedure TMyPanel.WMVScroll(var Message: TMessage);
var
lPos:Integer;
begin
inherited;
lPos:=GetScrollPos(Handle,SB_VERT);
case LoWord(Message.WParam) of
SB_LINEUP:
lPos:= lPos - 1 ;
SB_LINEDOWN:
lPos:= lPos + 1 ;
SB_PAGEDOWN:
lPos:= lPos + FVPageSize;
SB_PAGEUP:
lPos:= lPos - FVPageSize;
SB_THUMBTRACK:
lpos:=HiWord(Message.WParam);
end;
if lPos<0 then lPos:=0;
if lPos>FVMax then lPos:=FVMax;
SetScrollPos(Handle,SB_VERT,lPos,True);
ScrollWindow(Handle,0,FVDelta-lPos,nil,nil);
FVDelta:=lPos;
Invalidate;
end;

//以下为测试代码,TMyPanel可以不用在不用注册的情况下测试。
procedure TForm1.Button1Click(Sender: TObject);
begin
APanel:=TMyPanel.Create(Self);

with APanel do
begin
Left:=20;
Top:=20;
Height:=200;
Width:=300;
end;
with TButton.Create(APanel) do
begin
Parent:=APanel;
Left:=0;
Top:=0;
Caption:='Button1';
end;
with TButton.Create(APanel) do
begin
Parent:=APanel;
Left:=270;
Top:=0;
Caption:='Button2';
end;
end;
 
谢谢smokingroom。
你的方法实现了我的需求,但还有点小问题,就是在滚动的时候,该panel会闪烁
。2。滚动条一直存在,能不能在超出显示范围时再显示滚动条。
 
判断一下不就行了
 
在FormCreate时加入self.doublebuffer:=true;就不会闪烁了。
 
还是不行,继续求解.
 
GX
标记一下.
 
要panel不闪烁可以用lockwindowupdate禁止刷新
禁止刷新:lockwindowupdate(handle)
恢复刷新:lockwindowupdate(0)

滚动条一直有我觉得没什么不好看的,一定要超出范围再显示可以自己判断一下是否超出边界,再用showscrollbar隐藏或显示滚动条。
procedure TPanel.WMPaint(var Message:TMessage);
var i:integer;
ROver,BOver:boolean;
begin
inherited;
ROver:=false;
BOver:=false;
for i:=0 to ControlCount-1 do
begin
if not Rover then
if Controls.Left+Controls.Width>ClientWidth then
Rover:=true;
if not Bover then
if Controls.Top+Controls.Height>ClientHeight then
Bover:=true;
end;
if ROver then
showscrollbar(handle,SB_HORZ,true)
else
showscrollbar(handle,SB_HORZ,false);

if BOver then
showscrollbar(handle,SB_VERT,true)
else
showscrollbar(handle,SB_VERT,false);
end;

免费视频、源码下载论坛:http://bbs.eston.com.cn
欢迎编程累的时候来论坛灌水。
 
lockwindowupdate(0)就是刷新桌面,不要乱用。
 
to kinneng:
没用过不要乱发表意见
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
728
import
I
后退
顶部