C Builder experience talk
C Builder 3.0 is Borland (now renamed INSPRISE) in 1998, a new generation of C language-based RAD development tools. C Builder3.0's adventive, users who have hobbies C language are not a gospel. Because in the past, no C language-based visual programming language is available under Windows. If you want to use VB or Delphi's visual programming language to program, you have to revisit the Basic or Pascal language, no pointers like C language, no " ", " "Such a cute operation, in short, it is not as good as the C language. Now all the troubles don't exist. C Builder3.0 not only supports traditional C language, but also supports Borland's OWL and Microsoft MFC. It can be said that C Builder 3.0 is the most powerful C language compiler that Windows under Windows. Since C Builder3.0 has been introduced soon, the relevant information is not a lot, and the following combines the use of the author, talk about some experience, experience. 1. Dynamic call Form FORM By default, all forms of adding a form of adding a project file have "AutoCreate" feature "AutoCreate". That is, as long as the program is running, the form exists in memory, regardless of whether it is called. Forms with this feature are generally applicable to the case where the form properties are fixed, often called. The advantage is that the speed is fast, and the disadvantage is to take up memory. In the actual programming, a large number of forms of similar dialog function will meet, they are used to display status or input information, and only need to call it in the program, complete their function, no resident memory. At this point, you can move the corresponding form in the "Auto - CreateForms" by selecting Project / Options / Forms, such as Form1, with the ">" button to move to the "AvailableForms" column, and call the form at the program. , Add the following statement: tform1 * myform = newTform1 (this); myform-> showmodal (); deleteMyform; Form1 is only transferred in memory only when you need to call, after the call is completed, that is, use Delete to clear the memory. This reduces the occupancy of the program to memory resources. Second, the method of traversing the form control to access or modify the control on the form, the method is simple, tedit as an example: edit1-> text = "; edit2-> text =" "; but if there are ten on the form To come like Edit1, you need to perform the same initialization, use the above method one by one, don't trouble! So it is necessary to master the method of trailing form controls. Before introducing this method, let's take a look at the form Form's Components and Controls properties. See Table 1.
Table One Property Type Description ComponentCountint The total number of various controls on the FORM componentstCompont * current Form is currently pointing to the array of all controls. ControlCountint currently on a sub-area on a sub-area in the Form ControlstControl * Current Form points to a sub-area The array of controls is described as an example (figure), and the componentcount = 6 of Form1, while panel1's controlcount = 4., Where: array object Components [0] panel1components [1] label1components [2] Edit1components [3] label2components [ 4] Edit2Components [5] Button1 array object Controls [0] label1controls [1] edit1controls [2] label2controls [3] Edit2 The following code completed the initialization of all TEDIT controls on Panel1. The reader can be changed slightly to traverse other controls. There is a small trick here, and we put the controls that need to be initialized on a panel1, distinguish between controls that do not need to initialize, so that it is easy to program. AnsistringNameString = "TEDIT"; for (inti = 1; icontrolcount; i ) {if (Panel1-> Controls [i] -> classnameis (namestring)) {TEDIT * P = Dynamic_Cast (Panel1-> Controls [i]); p -> text = "";}} 3, using the Enter key to control the focus switching in the Windows environment, you have to get a control, you can click on the control with the mouse, or press the Tab button to move the focus to This control is on. This method of controlling focus switches sometimes does not conform to the user's habits. As shown in the figure, the user wants to use the Enter key to control the focus by edit1 to Edit2. To achieve such a function, you need to use the WinAPI function SendMessage. The method is: first set the keypreview attribute of Form1 to True, then add the following code in the ONKEYPRESS event of Form1. In this way, the user can move by pressing the ENTER, the key to control the focus is moved in order to define the good Taborder! Void__fastcalltform1 :: formypress (Tobject * sender, char & key) {if (key == vk_return) {sendMessage (this-> handle, wm_nextdlgctl, 0, 0); key = 0;}} four, add color to TSTRINGGRID --- TstringGrid is a character grid control for C Builder to provide users. It is not enough in beauty that it does not provide a method of modifying each unit font color, size. In fact, this function is to be implemented for TStringGrid, just need to be slightly processed in the program. The method is to customize a two-dimensional array Cellbuf, and its subscript corresponds to the grid unit list, used to store the color, text, etc. of each mesh unit. Structcellstru {ANSISTRINGMSG; // text information TCOLORCOLOR; // text color}; cellstrucellbuf [maxcol] [maxRow]; ---- Initialization Cellbuf, then add the following code, adding the following code can.
void__fastcallTForm1 :: StringGrid1DrawCell (TObject * Sender, intCol, intRow, TRect & Rect, TGridDrawStateState) {StringGrid1-> Canvas-> Font-> Color = cellbuf [Col] [Row] .color; StringGrid1-> Canvas-> TextOut (Rect.Left 3, Rect.top 3, Cellbuf [Col] [ROW] .msg);} 5. The implementation of the software cover ---- The popular practice of modern software design is to adjust one before the program is started. The picture is made as a cover, usually 1/4 screen size, showing information about the software name, author, version and other information. To implement such functions with C Builder, the method is simple: 1 Customize a window body class TSPLASHFORM, set it to "Transparent Window", ie all options under Bordericons are fixed into false, borderstyle = bsnone, FormStyle = FSSTAYONTOP, POSITION = POSCREENCENTER; 2 Place a TPANEL on the TSPLASHFORM form; 3 Place a TIMAGE control on the TPANEL to transfer the desired graphics; 4 is slightly modified to the WinMain function, add the following Show the code. It should be pointed out that this code passes the function FINDWINDOW, searching for the window titled "DEMO" application in memory, if there is, then exit the operation of the program. This feature prevents the program from running again. This design is required in some occasions. WinApiwinmain (Hinstance, Hinstance, LPSTR, INT) {{if (FindWindow (NULL, "DEMO")! = 0) {Application-> MessageBox ("Program has been run!", "Warning", MB_ICONSTOP); Return0;} TSPLASHFORM * SPLASH = NewTSPLASHFORM (); splash-> show (); splash-> update (); application-> infitialize (); application-> createform (__ classid (tform1), & form1); splash-> close (); DELETESPLASH; Application-> Run ();} catCH (Exception & Exception) {Application-> Showexception (& Exception);} Return0;} 6. How to permanently remove the deleted record in DBF Table-> delete () deleted DBF Record, did not really be deleted from the DBF database, but only a delete tag. How to implement the functionality of the pack command in DBASE? Please see the code below.
Table-> close (); for (;;) try {table-> eXclusive = true; table-> open (); break;} catch (...) {} if (dbipacktable (Table-> DbHandle, Table- > Handle, NULL, SZDBASE, TRUE)! = Dbierr_none) Application-> MessageBox ("No Record", "Error", MB_ICONSTOP); Seven, I / O port read and write to achieve careful readers discover, C Builder no longer supports information, such as InportB (), Outportb (), type I / O port read and write instruction. Accurately, in a Windows environment, BorlandC only supports port operation of 16-bit applications, and does not support 32-bit application port operations, while C Builder developed is 32-bit. I personally think that this is the failure of the C Builder designer. Because of the PC, I / O address space and memory address space have been independent. Look at Delphi, don't you realize access to I / O ports through the port array? I don't know why C Builder does not provide similar mechanisms? The following functions are the author from the Internet, verified, in the Windows95 environment, you can indeed read and write to the I / O port. Readers can learn from it. voidoutportb (unsignedshortintport, unsignedcharvalue) {// movedx, * (& port); __ emit __ (0x8b, 0x95, & port); // moval, * (& value); __ emit __ (0x8a, 0x85, & value); // outdx, al; __ emit__ (0x66,0xee);} voidoutportw (unsignedshortintport, unsignedshortintvalue) {// movedx, * (& port); __ emit __ (0x8b, 0x95, & port); // movax, * (& value); __ emit __ (0x66,0x8b, 0x85, & value ); // outdx, ax; __ emit __ (0xef);} unsignedcharinportb (unsignedshortintport) {unsignedcharvalue; // movedx, * (& port); __ emit __ (0x8b, 0x95, & port); // inal, dx; __ emit __ (0x66,0xec ); // mov * (& value), al; __ emit __ (0x88,0x85, & value); returnvalue;} unsignedshortintinportw (unsignedshortintport) {unsignedshortintvalue; // movedx, * (& port); __ emit __ (0x8b, 0x95, & port); / / INAX, DX__EMIT __ (0xED); // Mov * (& Value), AX__Emit __ (0x66, 0x89, 0x85, & value); ReturnValue;} Both applications developed under Windows generally large, program operation Often inventory of a lot of unknown system DLL files. In order to generate applications that can be disengaged from the C Builder environment, stand-alone, readers must set a certain setting for the compiler.
The method is to set up Project / Option / Packages / RunwithRuntimePackages for Disable, set Project / Option / Linker / UseSDynamicrtl to disable, recompilate the program, so that the generated EXE file can be run off from the C Builder environment. But if your program applies a database, only the above operation is not enough - because you have to install BDE (BorlandDatabaseEngineer). The installation of BDE is more troublesome. The reader is best to make the installation disk with InstallShieldExpress with C Builder 3.0, package the application and BDE. If you can't find it, INSTALLSHIELDEXPRESS that can be included with Delphi3.0 can be used. The method of use of InstallShield is limited to space, no longer introduced. Conditional readers can check the relevant information online. Posted on July 11, 2004 2:57 PM