高手来帮忙!相信这个问题可以让很多人提高开发控件的水平! ( 积分: 100 )

  • 主题发起人 主题发起人 ericimex01
  • 开始时间 开始时间
E

ericimex01

Unregistered / Unconfirmed
GUEST, unregistred user!
我想改进原有的button控件,比如:在原来的button控件上画些线?如何实现?
 
我想改进原有的button控件,比如:在原来的button控件上画些线?如何实现?
 
继承tbutton重载paintwindow方法
 
能否给段代码?
 
或者你自己开发一个控件。继承自你要修改的控件。然后在绘制函数中进行自己的绘制就可以了!
 
为什么没人可以给出代码呢?
 
TMJSBtn = class(TCustomControl)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Paint; override;
published
{ Published declarations }
end;

procedure TMJSBtn.Paint;
var
rec : TRect;
begin
inherited;
with Canvas do
begin
Brush.Color := clRed;
Rectangle(0, 0, Width, Height);
if Caption <> '' then
begin
TextOut(10, 10, Caption);
Rec.Left := Width - 10;
rec.Top := 10;
rec.Right := 2;
rec.Bottom := Height - 10;
DrawFocusRect(rec);
end;
end;
end;

这是我的,看看吧,或许有帮助!
 
非常感谢!
不过有一个问题,为什么要从TCustomerControl继承,而不从tbutton继承?
 
我把川中小马的程序改了改,又发现新的问题:
unit MJSBtn;

interface

uses
SysUtils, Classes, Controls, Types, Graphics, Messages,ExtCtrls;

type
TMJSBtn = class(TCustomControl)
private
{ Private declarations }
protected
{ Protected declarations }
public
procedure paint; override;
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
procedure WMLBUTTONDOWN(var Message: TMessage); message WM_LBUTTONDOWN;
procedure WMLBUTTONUP(var Message: TMessage); message WM_LBUTTONUP;
{ Public declarations }
published
property Caption;
{ Published declarations }
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('xhh', [TMJSBtn]);
end;

{ TMJSBtn }


procedure TMJSBtn.CMTextChanged(var Message: TMessage);
begin
repaint;
end;

procedure TMJSBtn.Paint;
var
MainRect,rec : TRect;
begin
inherited;
with Canvas do
begin
Brush.Color := clBtnFace;
MainRect:=ClientRect;
frame3D(Canvas,MainRect,clBtnHighlight,clBtnShadow,2);
if Caption <> '' then
begin
TextOut(10, 10, Caption);
Rec.Left := Width - 10;
rec.Top := 10;
rec.Right := 2;
rec.Bottom := Height - 10;
DrawFocusRect(rec);
end;
end;
end;

procedure TMJSBtn.WMLBUTTONDOWN(var Message: TMessage);
var
MainRect: TRect;
begin
MainRect:=ClientRect;
frame3D(Canvas,MainRect,clBtnShadow,clBtnHighlight,2);
end;

procedure TMJSBtn.WMLBUTTONUP(var Message: TMessage);
var
MainRect: TRect;
begin
MainRect:=ClientRect;
frame3D(Canvas,MainRect,clBtnHighlight,clBtnShadow,2);
end;

end.
如果连续按这个按钮后,无论鼠标点在form的什么地方,这个按钮都会被按下。这是为什么?要怎么改呢?
 
TMJSBtn = class(TCustomControl)
private
{ Private declarations }
FCanvas:TControlCanvas;
protected
{ Protected declarations }
public
procedure xx;
constructor create(aowner:TComponent);override;
{ Public declarations }
published
{ Published declarations }
end;

constructor create(aowner:TComponent);override;
begin
inherited;
FCanvas:=TControlCanvas.create;
FCanvas.control:=self;
end;

procedure xx;
begin
//调用FCanvas,随便画点东西
end;
然后可以在form里调用xx方法.
 
无论鼠标点在form的什么地方,这个按钮都会被按下,是为什么呢?
 
因为在Delphi中
一般都是先定义一个完成所有功能的类
然后再定义一个把需要显示给用户的接口显示出来的类
这样的做的目的就是如果用户想开发新的控件
可以把一些不想让使用者看到东西隐藏起来
 
那就是单击窗体的事件,传给了单击Button的事件了;
这还用说么?
 
To: Mike1234567890
能再说的详细一点吗?你所说的和我问的题有什么关系,不太明白?
To: dongy_143
恐怕不是你说的那么简单吧?那你说说为什么单击窗体的事件会传给Button了呢?而且为什么不是每次都这样呢?而是连续多次按这个按钮后,有时会这样?
 
unit MJSBtn;

interface

uses
SysUtils, Classes, Controls, Types, Graphics, Messages,ExtCtrls,Dialogs;

type
TMJSBtn = class(TCustomControl)
private
{ Private declarations }
protected
{ Protected declarations }
public
procedure paint; override;
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
procedure WMLBUTTONDOWN(var Message: TMessage); message WM_LBUTTONDOWN;
procedure WMLBUTTONDBCLK(var Message: TMessage); message WM_LBUTTONDBLCLK ; //添加的语句
procedure WMLBUTTONUP(var Message: TMessage); message WM_LBUTTONUP;
{ Public declarations }
published
property Caption;
{ Published declarations }
end;


implementation
uses unit1;


procedure TMJSBtn.CMTextChanged(var Message: TMessage);
begin
repaint;
end;

procedure TMJSBtn.Paint;
var
MainRect,rec : TRect;
begin
inherited;
with Canvas do
begin
Brush.Color := clBtnFace;
MainRect:=ClientRect;
frame3D(Canvas,MainRect,clBtnHighlight,clBtnShadow,2);
if Caption <> '' then
begin
TextOut(10, 10, Caption);
Rec.Left := Width - 10;
rec.Top := 10;
rec.Right := 2;
rec.Bottom := Height - 10;
DrawFocusRect(rec);
end;
end;
end;

procedure TMJSBtn.WMLBUTTONDOWN(var Message: TMessage);
var
MainRect: TRect;
begin
MainRect:=ClientRect;
frame3D(Canvas,MainRect,clBtnShadow,clBtnHighlight,2);
end;

procedure TMJSBtn.WMLBUTTONDBCLK(var Message: TMessage);
begin
{} //添加的语句
end;

procedure TMJSBtn.WMLBUTTONUP(var Message: TMessage);
var
MainRect: TRect;
begin
MainRect:=ClientRect;
frame3D(Canvas,MainRect,clBtnHighlight,clBtnShadow,2);
end;

end.

这样如何?!
 
to yjingz:
高手!好了!
可否告诉我为什么?
 
说来也简单,
你的那个错误是由鼠标双击引起的,所以屏蔽掉它就好了.
 
to yjingz:
多谢!
能否留下QQ,以后多交流?或加我QQ:18110031
 
问题解决了,不给人家分!!
 
谁说我不给?
不给这些分我也留不住,不过怕结了贴就沉了。
还没取得联系方式呢。
 
后退
顶部