📄 bcjq035.txt
字号:
hfont hfont=createfontindirect(&fontrec);
hfont hold=::selectobject(canvas->handle,hfont);
::setrect(&rc,0,0,20,clientheight);
::settextcolor(canvas->handle,rgb(255,255,255));
::textout(canvas->handle,3,clientheight-3,msg,lstrlen(msg));
::selectobject(canvas->handle,hold);
::deleteobject(hfont);
}
void __fastcall tvcform::bitbtn1click(tobject *sender)
{
close();
}
void __fastcall tvcform::onnchittest(tmessage & msg)
{
tpoint pt;
pt.x=loword(msg.lparam);
pt.y=hiword(msg.lparam);
pt =screentoclient(pt);
rect rc;
setrect(&rc,0,0,20,clientheight);
if (ptinrect(&rc,pt))
msg.result = htcaption;
else
defaulthandler(&msg);
}
void __fastcall tvcform::createparams(controls::tcreateparams&
params)
{
tform::createparams(params);
params.style |= ws_popup;
params.style ^= ws_dlgframe;
}
vcform的消息处理已经介绍过,这里再对标题条的绘制作简要说明。由于c++builder的tfont没有定义文字旋转旋转的属性,因此用传统的sdk绘图方法。canvas->handle即是代表gdi绘图的hdc。
3 制作对话框控件在开始制作控件之前,先将vcap.cpp中的#pragma
package(smart_init)行注释掉。创建控件的步骤是:创建一个单元文件,在其中完成控件的类定义和注册,然后就可以安装了。控件类一般从某个现有类继承导出。制作控件与一般类定义的主要区别在于属性(property)和事件(event),事件也是属性。由属性就带来了属性的存取方法、缺省值、属性编辑器等问题。为简单起见,本控件只涉及到上述一部分概念,但能涵盖控件制作的一般过程。
.开始一个空控件
由于要制作的对话框控件的最小必要功能是一个execute()方法,因此可以从tcomponent类继承。命名控件名为tvcaptiondlg,定义控件的单元文件命名为vcapdlg.cpp,其头文件为vcapdlg.h。用component
wizard或手工方法完成如下文件:
//vcapdlg.h
#ifndef vcapdlgh
#define vcapdlgh
#include
#include
#include
#include
class package tvcaptiondlg: public tcomponent
{
private:
protected:
public:
virtual __fastcall tvcaptiondlg(tcomponent *owner);
__published:
};
#endif
//vcapdlg.cpp
#include
#pragma hdrstop
#include "vcapdlg.h"
#pragma package(smart_init)
static inline tvcaptiondlg * validctrcheck()
{
return new tvcaptiondlg(null);
}
namespace vcapdlg //同控件定义单元文件名,首字母大写,其余小写
{
void __fastcall package register()
{
tcomponentclass classes[1]={__classid(tvcaptiondlg)};
registercomponents("mailuo",classes,0);
}
}
__fastcall tvcaptiondlg::tvcaptiondlg(tcomponent * owner)
:tcomponent(owner)
{
}
registercomponents("mailuo",classes,0)指示在控件面板上的mailuo页(没有则创建该页)上生成classes数组包含的所有控件,这里是一个tvcaptiondlg控件。当然此时的tvcaptiondlg控件不具备tcomponent类之外的任何能力。
.将要用到的form文件包含进来
这只需在vcapdlg.cpp的#include "vcapdlg.h"后加入一行#include
"vcap.cpp"(vcapdlg.*与vcap.*在同一目录)即可,重申一句:vcap.cpp中的#pragma
package(smart_init)行要去掉。将整个vcap.cpp和vcap.h的内容包括在vcapdlg.cpp中也是可以的,这样就用不着vcap.*文件了.即将类vcform的定义与vcapdlg放在一个文件里,反正vcform只不过是vcapdlg要用到的一个类定义罢了。不过这样一来,在生成vcform的实例对象时,上面所说bitbtn1的caption、kind等与缺省值不等的属性都需要运行时设置,因为非缺省属性是保存在.dfm文件里的。这也是使用了form的类常用单独的单元文件保存的原因。
.添加接口属性
这里只提供一个caption属性供控件使用者用于读取或设置对话框的标题。为此只需在类tvcaptiondlg的声明体的private区加入一个ansistring
fcaption变量作内部存储用,并在__published
区加入一行:
__property ansistring caption={read=fcaption, write=fcaption};
因为属性类型是ansistring,所以不需专门的属性编辑器处理设计时属性的编辑。另外在设计时该属性值的改变不需引起什么立即的处理和过程,因此存取方法采用最简单的立即存取(read=fcaption,
write=fcaption)。
.添加执行方法
vcaptiondlg的execute()方法的功能是创建一个类vcform的实例对象并模式显示之。这只需如下代码:
void __fastcall tvcaptiondlg::execute()
{
vcform=new tvcform(application);
vcform->caption=caption;
vcform->showmodal();
delete vcform;
}
其中vcform为vcap.cpp中已声明的tvcform类类型的一个实例变量。相应地在vcapdlg.h里需加入一个execute方法的声明。
另外可以加入一些无关紧要的代码,如tvcaptiondlg的构造函数中加入成员变量的初始化语句等。至此整个控件的制作完成。完整的控件代码如下:
//vcapdlg.h
#ifndef vcapdlgh
#define vcapdlgh
#include
#include
#include
#include
class package tvcaptiondlg: public tcomponent
{
private:
ansistring fcaption;
protected:
public:
virtual __fastcall tvcaptiondlg(tcomponent *owner);
virtual void __fastcall execute();
__published:
__property ansistring caption={read=fcaption, write=fcaption};
};
#endif
//vcapdlg.cpp
#include
#pragma hdrstop
#include "vcapdlg.h"
#include "vcap.cpp"
#pragma package(smart_init)
static inline tvcaptiondlg * validctrcheck()
{
return new tvcaptiondlg(null);
}
namespace vcapdlg
{
void __fastcall package register()
{
tcomponentclass classes[1]={__classid(tvcaptiondlg)};
registercomponents("mailuo",classes,0);
}
}
__fastcall tvcaptiondlg::tvcaptiondlg(tcomponent * owner)
:tcomponent(owner)
{
fcaption="mailuo's sample";
}
void __fastcall tvcaptiondlg::execute()
{
vcform=new tvcform(application);
vcform->caption=caption;
vcform->showmodal();
delete vcform;
}
控件的安装不再赘述。
4 结语
本文旨在演示c++ builder的控件制作和消息处理、sdk等高级编程技术。以上代码全部在pwin98/c++
builder 3.0上通过调试。顺便指出,c++ builder的帮助文档中的creating
custom components讲控件制作讲得非常好,是学习编写控件的不可多得的好教程。但其中making
a dialogbox a component一篇中有两处小小瑕疵:一是including
the form unit中所讲用pragma link vcap.obj的方法是一个相对麻烦的方法,因为需要先将vcap.cpp放入一个无关项目中编译以生成obj文件(或是用命令行编译但要指定参数),不如本文一条#include"vcap.cpp"简单。二是该文档中没有指出对这种自己生成的form,有一个限制就是一定要注释掉#pragma
package(smart_init)行,否则安装时虽可生成包文件但控件不能被装上。它举的about对话框的例子中恰好没有这一句。而用ide产生的form一般都是这一句的。
if (ad==1) {document.write(''+'');}
if (ad==2) {document.write(''+'');}
if (ad==3) {document.write(''+'');}
C++ Builder开发者®
2000年06月01日 站长:唐朝