以下代码是来自“ameken”的。问题是:如何实现Panel有鼠标触发焦点呢?就是能1格1格的点击。procedure TForm1.FormCreate(Sender: TObject);
Var
MyPanel : Array of TPanel;
i,j,k: Integer;
begin
SetLength(MyPanel,12);
for i := 0 to 11 do
begin
MyPanel[i] := TPanel.Create(Self);
MyPanel[i].Width := 130;
MyPanel[i].Height:= 80;
MyPanel[i].Parent:=self;
j:=Trunc(i/4); //取整 行
K:=i mod 4 ; //取余 列
if i<4 then
begin
MyPanel[i].Top:=30;
MyPanel[i].Left:=30+i*130;
end else
begin
MyPanel[i].Top:=30+80*j;
MyPanel[i].Left:=30+k*130;
end;
end;
end;
Var
MyPanel : Array of TPanel;
i,j,k: Integer;
begin
SetLength(MyPanel,12);
for i := 0 to 11 do
begin
MyPanel[i] := TPanel.Create(Self);
MyPanel[i].Width := 130;
MyPanel[i].Height:= 80;
MyPanel[i].Parent:=self;
j:=Trunc(i/4); //取整 行
K:=i mod 4 ; //取余 列
if i<4 then
begin
MyPanel[i].Top:=30;
MyPanel[i].Left:=30+i*130;
end else
begin
MyPanel[i].Top:=30+80*j;
MyPanel[i].Left:=30+k*130;
end;
end;
end;
写自定义MouseDown事件,比如MyMouseDown参数和panel的mousedown参数一致,然后在创建panel时把mousedown事件指向MyMouseDown事件procedure TForm1.FormCreate(Sender: TObject);
Var
MyPanel : Array of TPanel;
i,j,k: Integer;
begin
SetLength(MyPanel,12);
for i := 0 to 11 do
begin
MyPanel[i] := TPanel.Create(Self);
MyPanel[i].Width := 130;
MyPanel[i].Height:= 80;
MyPanel[i].Parent:=self;
MyPanel[i].OnMouseDown := MyMouseDown; //加上设句话
j:=Trunc(i/4); //取整 行
K:=i mod 4 ; //取余 列
if i<4 then
begin
MyPanel[i].Top:=30;
MyPanel[i].Left:=30+i*130;
end else
begin
MyPanel[i].Top:=30+80*j;
MyPanel[i].Left:=30+k*130;
end;
end;
end;
begin
//your code
end;
procedure MyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
楼主可以重载他的paint方法,然后当鼠标按下的时候,将获得焦点变量设置成true
然后invalidate区域
这样,获得焦点的时候,界面重绘制,在上面画一个焦点框就可
谢谢suiyunonghen,能否给下代码参考。
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Panel1: TPanel;
procedure FormCreate(Sender: TObject);
procedure MyPanelClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
MyPanel : Array of TPanel;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
Var
i,j,k: Integer;
begin
SetLength(MyPanel,12);
for i := 0 to 11 do
begin
MyPanel[i] := TPanel.Create(Self);
MyPanel[i].Width := 130;
MyPanel[i].Height:= 80;
MyPanel[i].Parent:=self;
MyPanel[i].Tag:= i ; //用tag标识
MyPanel[i].OnClick :=MyPanelClick; //赋予事件
j:=Trunc(i/4); //取整 行
K:=i mod 4 ; //取余 列
if i<4 then
begin
MyPanel[i].Top:=30;
MyPanel[i].Left:=30+i*130;
end else
begin
MyPanel[i].Top:=30+80*j;
MyPanel[i].Left:=30+k*130;
end;
end;
end;procedure TForm1.MyPanelClick(Sender: TObject); //自己定义的事件
Var
i : Integer;
begin
i := TPanel(Sender).Tag;
MyPanel[i].Caption:=IntToStr(i);
ShowMessage(IntToStr(i));
end;end.
不知道是不是你想要的?
我想在Panel上实现这样的效果.上面的代码在Panel上触发到了,但是看不到触发的焦急.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;type
TPanel = class(ExtCtrls.TPanel)
protected
procedure Paint;override;
procedure WMLBtnDown(var msg: TWMLButtonDown);message WM_LBUTTONDOWN;
procedure WMLostFocus(var msg: TMessage);message WM_KILLFOCUS;
end;
TForm2 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;implementation{$R *.dfm}procedure TForm2.FormCreate(Sender: TObject);
beginend;{ TPanel }procedure TPanel.Paint;
var
r: TRect;
c: TColor;
begin
inherited;
if Focused then
begin
c := Canvas.Brush.Color;
Canvas.Brush.Color := clYellow;
r := ClientRect;
r.Right := r.Right - BorderWidth - 1;
r.Bottom := r.Bottom - BorderWidth - 1;
Canvas.DrawFocusRect(r);
Canvas.Brush.Color := c;
end;
end;procedure TPanel.WMLBtnDown(var msg: TWMLButtonDown);
begin
inherited;
SetFocus;
invalidate;
end;procedure TPanel.WMLostFocus(var msg: TMessage);
begin
Invalidate;
end;end.
谢谢不得闲,
不是的,我想实现DBCtrlGrid那种焦点的效果.
在Paint中,你自己绘制啊,你想要什么效果都可以做出来的。把我那个代码扩充一下,应该就可以了
贴图还是填充颜色,随便你怎样都行