中易网

请教MFC如何声明实现接口

答案:1  悬赏:70  
解决时间 2021-01-19 15:55
  • 提问者网友:骨子里的高雅
  • 2021-01-18 15:17
请教MFC如何声明实现接口
最佳答案
  • 二级知识专家网友:千夜
  • 2021-01-18 16:55
在InternetExpress中支持一些有别于VCL TComponent控件的控件,如QueryForm等,它们都能生成html代码以在浏览器中显示自己,这些控件主要的不同之处在于它们都是接口。

本文的目标就是展示如何自己编写可在DataForm或QueryForm里使用的InternetExpress自制控件。在开始之前最重要的是搞清楚MidItems单元。它包含有TWebForm构件的定义,它是所有InternetExpress-aware构件的起点。从它衍生出两个类:TDataForm和TQueryForm。实际上TQueryForm是从TCustomQueryForm继承来的,TCustomQueryForm是TWebForm的子类并且实现了IQueryForm接口。IQueryForm是一个没有方法的接口,也许偶尔可以用来在运行时区分DataForm和QuyeryForm(不然实在没办法解释为什么会在首要位置使用一个空的接口)。
实际的子构件也可以在MidItems单元时里找到,包括TFieldText、TTextColumn和TQueryText。它们的祖先类是TWebTextInput,也我们自制InternetExpress控件的基类,TWebTextInput拥有许多我们应该重载或实现的有趣的方法。最有趣的方法是ControlContents和AddAtrributes,它们是用来生成代表控件的HTML代码。正是它们使得RadioButton成其为无线钮、CheckBox成其为检查框。//后面会讲到,已经用过InternetExpress和页面编辑器的程序员可能注意到了:我们有几乎每一种HTML控制可用,除了Check box。我也觉得很奇怪,不过也没什么关系。
Latest News: John Kaster of Borland DevRel told me the reason they didn't implement checkbox support (yet). The state of the checkbox when off is indeterminate -- no value is passed back UNLESS the checkbox is turned on.
Borland hasn't found a solution they like, yet, and you'll be facing the same problem with my component. We're still working on that, so stay tuned...

TWebTextInput AddAttributes方法定义如下:
procedure TWebTextInput.AddAttributes(var Attrs:string);
begin
AddQuotedAttrib(Attrs,'NAME',GetHTMLControlName);
AddIntAttrib(Attrs,'SIZE',DisplayWidth);
AddIntAttrib(Attrs,'TABINDEX',TabIndex);
AddQuotedAttrib(Attrs,'STYLE',Style);
AddQuotedAttrib(Atrtrs,'CLASS',StyleRule);
AddCustomAttrib(Attrs,Custom);
end;
先不管AddQuotesAttrib和AttIntAttrib等方法,相信你能够接受AddAttributes方法输出HTML控制名字(a lookup function)、DisplayWidth、TabIndex,Style和StyleRule到一个字符串Attrs里了。ControlContents方法则使用该字符串生成我们需要的完整的HTML代码。要加入特定的HTML型控件,需要重载ControlContent方法,并使用Attrs作为上面的AddAttributes方法的返回值。

TWebCheckBox
要制作我们的简单的WebCheckBox控件,需要从TWebTextInput派生一个类,并重载ControlContents方法,新类的定义及实现如下:
type
TWebCheckbox=class(TWebTextInput)
protected
function ControlContent(Opetions:TWebContentOptions):string:override;
published
property DisplayWidth;
property ReadOnly;
property Caption
property CaptionAttributes;
property CaptionPosition;
property TabIndex;
property Style;
property Cutsom;
property StyleRule;
end;

impleamentation

function TWebCheckbox.ControlContent(Opetions:TWebContentOptions):string;
var
Attrs:string;
begin
AddAttributes(Attrs);
Result:=Format('',[Attrs]);
end;

就这么简单,等会我们注册这个控件之后(方式见本文最后),就可以在Web Page Editor里看到它了,所以现在就可以在我们的InternetExpress应用程序里使用了。然而,这个控件只能作为DataForm的子对象,要想能作为QueryForm的子对象显示,还得做些特别的事情:
TQueryCheckBox
要让一个InternetExpress控件与QueryForm配合工作,需要加入一个接口定义并实现之(新鲜,不实现加它干嘛)。我们从前面的TWebCheckBox派生一个新的TQueryCheckBox,并实现IQueryField接口以形成一个新的可加到QueryForm里的InternetExpress自制控件。在动手之前,先看一眼IQueryField接口,如下:
type
IQueryField=interface(IHTMLField)
['{7C3211150-FCFB-11D2-AFC3-00C04FB16EC3}']
funtcion GetParamName:string;
procedure SetParamName(AParamName:string);
function GetText:string;
procedure SetText(const Value:string);
property ParamName:string read GetParamName write SetParamName;
property Text:string read GEtTExt write SetText;
end;
这意味着任何实现了IQueryField接口的类都得实现接口里定义的方法。咱们的TQueryCheckBox类的定义和实现如下(注意,咱们只实现了GetText/SetText方法,没有实现ParamName方法):
type
TQueryCheckbox=class(TWebCheckbox,IQueryField)
private
FText:string;
protected
function GetText:string;
procedure SetText(const VAlue:string);
public
class function IsQueryField:Boolean;voerride;
end;

implementation

class function TQeryCheckbox.IsQueryField:boolean;
begin
Result:=True;
end;
funciton TQueryCheckbox.GetText:string;
begin
Result :=FText;
end;
procedure TQueryCheckbox.SetText(const Value:string);
begin
FText:=Value;
end;
做为IQueryField接口的一部份,类方法IsQueryField确保任何构件可以"introspect" TQueryCheckBox构件,显然,它只对QueryForms有意义。(introspect,内省,意即构件通过在此回答“你是QueryField吗?”,上面的例子回答“是的”,已经表明心迹,QueryForm可以相信其是Query家族的成员,可以做为自己的子对象。呵呵,什么时候girl们实现这么一个方法就好了)。
Registration
最后一个任务,比起实现这些控件要复杂一些,是注册这些控件。由于我们处理的不是常规出现于控件面板的控件,需要用一个不同的方法进行注册也就不足为奇了。这回得用RegisterWebComponents过程,它接受一个InternetExpress控件数组及一个可选的(控件)编辑器参数。我们只需要做个简单的调用:
procedure Register
begin
RegisterWebComponets([TWebCheckBox,TQueryCheckBox]);
end;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息