BCB control production and message processing

xiaoxiao2021-03-06  40

1 Preface As the RAD (Rapid Application Development) tool similar to Delphi, the powerful function of C Builder is not only reflected in database development, but also conveys the application development (it is very good to be very good). Only in terms of application, it is necessary to realize high quality software, and some advanced technology is still needed outside the drag. Such as messaging, DLL, OLE, thread, SDK programming. C Builder has more or less advantages in these aspects. In addition, custom controls can be easily created, and a major feature and advanced feature of C Builder. This article will demonstrate some concepts about control production and message processing in C Builder by making a title rod on the left side of the window, and involves a point of SDK programming. We will make a dialog, just like OpenDialog, one calls its execute () method, pops up a window shown in Figure 1. The title rod of this window is located on the left, green, and text towards the lower 90 degree word, the function and the general title rod, can you move the window? First complete this window, then use it to make a dialog control. Figure 1 2 With WM_NCHITTEST messages to create a vertical title window. Wm_nchittest message C Builder encapsulates some Windows messages in events, but cannot cover all messages, such as WM_NC **** series messages. When the WM_NCHITTEST message occurs, the return value indicates the current cursor, if returned to Hthscroll indicates that the current cursor is in the horizontal scroll bar, and returns HTCAPTION indicates that in the title rod (see Win32 SDK Help). Its parameters XPOS, YPOS represent the X, Y coordinates of the cursor (relative to the upper left corner of the screen), respectively correspond to the low words and high words of LParam. If the WM_NCHITTEST message is intercepted, when the mouse is pressed on the left side of the window, the return value will be set to htcaption, the system will only be in the title rod, which will be able to move the window, complete the function of the title rod, as the color and Text, the message is not related to the message, will be described below. The .windows message message is an event for the Windows operating system to send the program. However, hundreds of incidents, operating systems and?] There are different messages structures for each event, but to describe the message in a general structure, this structure is defined in the C Builder as TMessage. In addition, C Builder defines a dedicated structure for common messages, both. You can directly convert messages to a dedicated structure, or you can explain the TMESSAGE parameters.

Take the WM_NCHITTEST message as an example, it is defined as follows: struct twmphittest {cardinal msg; long unused; union {structure {windows :: tsmallpoint pos; long result;}; struct {short xpos; short ypos;};};}; Tmessage defined: 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;};};}; It can be found that the LPARAM member of TMessage corresponds to the POS member of TWMnchittest, that is, the following two line statements is equivalent: tpoint pt = tpoint (msg.lparam); // This time the MSG type is TMESSAGETPOINT PT = TPOINT (MSG.pos); / / msg type macro case twmnchittest.c builder processing a message in c builder custom message handling is more convenient, for example binding WM_NCHITTEST follows: Add the following macro definitions in the protected part of the window class: begin_message_mapmessage_handler (wm_nchittest, tmessage, onnchittest End_Message_Map (TForm) message_handler contains 3 parameters: WM_NCHITTEST, message identifier, or a custom message such as WM_MYMESSAGE, simply add a macro as #define WM_MYMESSAGE WM_APP 1, etc.; the second parameter TMESSAGE represents the message type, You can also provide a custom message structure type, such as TMYMSG, etc., and onnchittest is the message processing function. Thus, once there is a WM_NCHITTEST message to TFORM, the response to the message is completely handled with the onnchittest function. OnnchitTest function has only one parameter, the type of the second parameter in the message_handler, ie TMESSAGE & or TMYMSG &. Complete the window of Figure 1. Start a new application, name the FORM1 VcForm, the corresponding unit file is vcap.cpp, the header file is vcap.h. Vcform's BoardersTyle is set to bsnone, placing a bitmap button bitbtn1, caption is & OK, Kind is a BKOK, and a close () is added to the OnClick event handler. Then add the declaration of the message processing macro and function onnchittest at the VCAP.H to process the drag function of the title bar. To complete the coloring and text output of the title, double-click the onpaint event of VcForm to customize the FormPaint function, and the specific code is shown below. In addition, the window has three-dimensional, overloads the virtual function CreateParams to modify the style of the window.

Complete vcap.h and vcap.cpp follows: //vcap.h#ifndef vcaph # define vcaph # include #include #include #include #include class tvcform: public tform {__ published: // ide-managed componentstbitbtn * bitbtn1; void __fastcall formpaint (tobject * sender); void __fastcall bitbtn1click (tobject * sender); private: // user declarationsprotected: void __fastcall onnchittest (tmessage & msg); void __fastcall createparams (tcreateparams & params); begin_message_mapmessage_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 (ooner) {} void __fastcall tvcform :: formpt (TOBJECT * Sender) {// Draw a green headline RECT for width 20 Rc; SetRect (& RC, 0, 0, ClientWidth, ClientHeight); Canvas-> Pen-> Color = CLGreen; canvas-> brush-> color = clgreen; canvas-> Rectangle (0, 20, 20, clientHeight); / / Output Rotate Text char * msg = caption.c_str (); logfont fontrec; memset (& fontrec, 0, sizeof (logfont)); fontrec.lfheight = -13; fontrec.lfweight = fw_normal; fontrec.lfescapement = 900; // rotation angle 900x0. 1 degree = 90 degrees LSTRCPY (FontRec.Lffacename, "Song"); hfont hfont = CreateFontIndirect (& FONTREC); HFONT HOLD = :: SelectObject (canvas-> handle, hfont); :: setRect (& RC, 0, 0, 20 :: setTextColor (Canvas-> Handle, RGB (255, 255, 255)); :: Textout (canvas-> handle, 3, clientheight-3, msg, lstrlen (msg)); :: SelectObject (canvas-> handle :: deleteObject (hfont);} void __fastcall tvcform :: bitbtn1click (Tobject * sender) {close ();} void __fastcall tvcform :: ONNCHITTESTEST (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 (& RCT) , pt)) msg.result = htcaption; elsedefaulthandler (& msg);} void __fastcall tvcform :: createparams (controls :: tcreateparams & params) {tform :: createparams (params); params.style | = ws_popup; params.style ^ = WS_DLGFRAME;} VCFORM message processing has been introduced, here is a brief description of the drawings of the title bar. Since the TFont of C Builder does not define the properties of the text rotation, use the traditional SDK drawing method. Canvas-> Handle is an HDC representative of GDI drawing. 3 Making dialog controls before starting the control, release the #pragma package (Smart_init) line in vcap.cpp before starting the control. The steps to create controls are: Create a unit file, complete the class definition and registration of the control, and then install it. Controls are generally exported from a certain existing class. The main difference between making controls and general class definitions is that attributes and events (event), events are also attributes. Problems with attributes, default, attribute editor, etc. For the sake of simplicity, this component only involves the above part of the concept, but can cover the general process of controlling control. Start an air control Since the minimum necessary function of the dialog control to be made is an execute () method, so you can inherit from the Tcomponent class. Name the control named TVCAPTIONDLG, define the unit file of the control name vcapdlg.cpp, and its header file is vcapdlg.h. Following completion of file component wizard or manual methods: //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 // with the control definition unit file name, initials, rest lowercase {void __fastcall package register () {tcomponentclass classes [1] = {__ classid (tvcaptiondlg)}; registercomponents ( "mailuo", classes, 0);}} __ fastcall tvcaptiondlg: : tcaptiondlg (tComponent * Owner): TComponent (Owner) {} RegisterComponents ("Mailuo", Classes, 0) Indicates all controls containing the CLASSES array on the Mailuo page (not creating this page) on the control panel, here is A TVCAPTIONDLG control.

Of course, the TVCAPTIONDLG control does not have any capabilities other than the Tcomponent class. The FORM file that will be used is included in this only need to join the #include "vcap.cpp" (vcapdlg. * And VCAP. * At the same directory) after the #include "vcapdlg.h" of vcapdlg.cpp. * In the same directory) One sentence: #pragma package (smart_init) in vcap.cpp is going down. The content of the entire vCAP.cpp and VCAP.H is also possible in vcapdlg.cpp, so it is not used by vCAP. * Files. The definition of the class Vcform is placed in a file, anyway Vcform is just vcapdlg A class definition to be used. However, when generating an instance object of VcForm, the attributes of BitBTN1, Kind, etc., which are mentioned above, the attributes of default values, are runtime settings, as the non-default attribute is saved in the .dfm file. This is also why the use of Form's class is often saved. Add Interface Properties Here you only provide a CAPTION property for the control user to read or set the title of the dialog. For this purpose, just add an Ansistring Fcaption variable to the private storage in the Private area of ​​the class TVCAPTIONDLG, and join a line in the __published area: __Perty Ansistring Caption = {read = fcaption, Write = fcaption}; because the attribute type is ANSISTRING Therefore, there is no need for a special attribute editor to process the editing of design time attributes. In addition, changes in the attribute value do not need to have immediate processing and procedures, so access method is the simplest access (read = fcaption, Write = fcaption). The function of adding execution method vcaptiondlg's execute () method is to create an instance object of a class Vcform and scheduled. This only needs the following code: void __fastcall tvcaptiondlg :: execute () {vcform = new tvcform (application); vcform-> caption = caption; vcform-> showModal (); delete vcform;} where Vcform is declared in vcap.cpp. A instance variable for the TVCForm class type. The declaration of an Execute method is required in VcApdlg.h. Alternatively, some irrelevant code can be added, such as the initialization statement of the member variable, such as the constructor of TVCAPTIONDLG. The result of the entire control is completed.

转载请注明原文地址:https://www.9cbs.com/read-51997.html

New Post(0)