BCB控件制作和消息处理

翻译|其它|编辑:郝浩|2005-06-29 10:27:00.000|阅读 2327 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


1 前言

作为和delphi类似的rad(rapid application development)工具,c++ builder的强大功能不仅体现在数据库开发方面,也凸现于应用程序开发上(令人称绝的是这两方面结合得非常好)。仅就应用程序而言,要真正体现c++ builder的优势,开发出高质量的软件,则在拖拉拽放之外,尚需用到一些进阶技术。如消息处理、dll、ole、线程、sdk编程。c++ builder在这些方面都或多或少有独到的优势。此外,可以方便地制作自定义控件,也是c++ builder的一大特色和高级功能。本文将通过制作一个标题棒在窗口左边的对话框控件,来示范一些c++ builder中关于控件制作和消息处理的概念,同时涉及到一点sdk编程。我们将要制作的是一个对话框,就如同opendialog等一样,一调用其execute()方法,就弹出一个如图一所示的窗口。这个窗口的标题棒位于左方,绿色,文字走向为由下而上的90度字形,其功能和一般的标题棒相同,可以?⑹蟊暌浦粮么?来移动该窗口。
首先来完成这个窗口,然后用它来制作对话框控件。


2 利用wm_nchittest消息制作竖直标题的窗口

wm_nchittest消息

c++builder将某些windows消息封装于事件(event)中,但无法囊括所有消息,如wm_nc**** 系列消息。wm_nchittest消息发生于游标(cursor)移动或鼠标按下、释放时,返回值指示目前游标所在位置,如返回hthscroll表示处于水平滚动条内,返回htcaption表示处于标题棒内(参见win32 sdk help)。其参数xpos、ypos分别表示游标的x、y坐标(相对于屏幕左上角),分别对应于lparam的低字和高字。如果拦截wm_nchittest消息,使得当鼠标在窗口左边按下时,人为地将返回值设为htcaption,则系统将以为是在标题棒内,于是将可以移动窗口,完成了标题棒的功能,至于颜色和文字,则与消息无关,将在下面叙述其原理。


windows消息

消息就是windows操作系统送往程序的事件。但事件数以百计,操作系统并?]有为各个事件设计不同的消息结构,而是以一个一般性的结构来来描述消息,这个结构在c++ builder中定义为tmessage。另外c++ builder对常见消息定义了专用结构,二者对等。可以直接将消息转换为专用结构,也可以自行解释tmessage参数。以wm_nchittest消息为例,它的定义如下:
struct twmnchittest
{
   cardinal msg;
   long unused;
   union
  {
   struct
    {
    windows::tsmallpoint pos;
    long result;
     };
     struct
   {
     short xpos;
     short ypos;
    };
  };
};


对照tmessage定义:
struct tmessage
{
   cardinal msg;
   union
    {
       struct
        {
          word wparamlo;
          word wparamhi;
          word lparamlo;
          word lparamhi;
          word resultlo;
          word resulthi;
          };
     struct
      {
        long wparam;
        long lparam;
        long result;
       };
    };
};


可以发现,tmessage的lparam成员对应twmnchittest的pos成员,就是说以下两行语句
等价:

tpoint pt=tpoint(msg.lparam); //此时msg类型为tmessage
tpoint pt=tpoint(msg.pos); //此时msg类型为twmnchittest


c++ builder处理消息的宏

在c++ builder中自定义消息处理是较为方便的,结合wm_nchittest举例如下:
在窗口类的protected部分加入如下宏定义:
begin_message_map
message_handler(wm_nchittest,tmessage,onnchittest)
end_message_map(tform)


message_handler包含3个参数:wm_nchittest,消息标识,也可以为自定义消息如wm_mymessage,这时只需加一个宏如#define wm_mymessage wm_app+1等;第二个参数tmessage代表消息类型,也可以为符合要求的自定义消息结构类型如tmymsg等,onnchittest为消息处理函数。这样,一旦有wm_nchittest消息传给tform,对该消息的响应就完全交由onnchittest函数处理。onnchittest函数只有一个参数,类型为message_handler中第2个参数的引用,即tmessage &或tmymsg &。


完成图一的窗口

开始一个新应用程序(new application),将form1命名为vcform,对应单元文件为vcap.cpp,头文件为vcap.h。vcform的boarderstyle设置为bsnone,其上放置一个位图按钮bitbtn1,caption为&ok,kind为bkok,onclick事件处理函数中加入一句close()。然后在vcap.h的protected部分加入如前所述消息处理宏和函数onnchittest的声明,以处理标题条的拖动功能。为完成标题的着色和文字输出,双击vcform的onpaint事件以定制formpaint函数,具体代码见下面源码。此外为使窗口有立体感,重载虚函数createparams,以修改窗口的风格。完整的vcap.h和vcap.cpp如下:

//vcap.h
#ifndef vcaph
#define vcaph
#include
#include
#include
#include
#include
class tvcform : public tform
{
  __published: // ide-managed components
  tbitbtn *bitbtn1;
  void __fastcall formpaint(tobject *sender);
  void __fastcall bitbtn1click(tobject *sender);
  private: // user declarations
  protected:
  void __fastcall onnchittest(tmessage & msg);
  void __fastcall createparams(tcreateparams& params);
  begin_message_map
  message_handler(wm_nchittest,tmessage,onnchittest)
  end_message_map(tform)
  public: // user declarations
  __fastcall tvcform(tcomponent* owner);
  };
  extern package tvcform *vcform;
#endif
//vcap.cpp
#include
#pragma hdrstop
#include "vcap.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
tvcform *vcform;
__fastcall tvcform::tvcform(tcomponent* owner)
: tform(owner)
{
}
void __fastcall tvcform::formpaint(tobject *sender)
{
//绘制宽20的绿色标题条
rect rc;
setrect(&rc,0,0,clientwidth,clientheight);
canvas->pen->color=clgreen;
canvas->brush->color=clgreen;
canvas->rectangle(0,0,20,clientheight);
//输出旋转文字
char* msg=caption.c_str();
logfont fontrec;
memset(&fontrec,0,sizeof(logfont));
fontrec.lfheight = -13;
fontrec.lfweight = fw_normal;
fontrec.lfescapement = 900; //旋转角度900x0.1度=90度
lstrcpy(fontrec.lffacename,"宋体");
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一般都是这一句的。


 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP