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.
First, dynamically call form form
By default, the form generated by File / NewForm has the feature of adding a project file with "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;
The form FORM1 is only transferred in memory when it is called, and after the call is complete, it is cleared with delete. 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, with TEDIT as an example:
Edit1-> Text = "";
Edit2-> Text = ""
However, if there is ten controls like Edit1 on the form, you need to initialize the same initialization, and use the above method one by one, you will not bother! 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 I
Property Type Description
ComponentCountint Currently Form on Form
Total number of controls
ComponentstCompont * currently pointing on Form
Array of all controls
ControlCountint is currently a child on Form
Total number of various types of controls on the region
ControlstControl * Currently, Form points to a single
Array of all controls on the area
Take the figure as an example (figure) description, FORM1's ComponentCount = 6, while panel1's controlcount = 4.,
Where: array objects
Components [0] panel1
Components [1] label1
Components [2] Edit1
Components [3] label2
Components [4] Edit2
Components [5] Button1
Array object
Controls [0] label1
Controls [1] edit1
Controls [2] label2
Controls [3] edit2
The following code completed the initialization 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 = "";
}
}
Third, the method of controlling the focus switching with ENTER
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 the control. 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 ::
FormKeyPress (Tobject * Sender, Char & Key)
{
IF (Key == VK_RETURN)
{
SendMessage (this-> handle, wm_nextdlgctl, 0, 0);
Key = 0;
}
}
Fourth, add color to the text of 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];
---- After initialization Cellbuf, add the following code to the onDrawcell response event of the character grid control StringGrid1.
Void__fastcalltform1 :: stringgrid1drawcell
(TOBJECT * Sender, Intcol,
Introw, Treat & Rect, TGriddrawStateState
{
StringGrid1-> canvas-> font->
Color = Cellbuf [color] [ROW] .color;
StringGrid1-> canvas-> textout (Rect.Left 3,
Rect.top 3, Cellbuf [col] [row] .msg);
V. Implementation of software cover
---- The popular practice of modern software design is that before the program is initialized, first adjusting a picture as a cover, usually 1/4 screen size, showing 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)
{
Try
{
IF (FindWindow (NULL, "DEMO")! = 0)
{
Application-> MessageBox
("Program has been run!", "Warning", MB_ICONSTOP);
Return0;
}
Tsplashform * splash = newstsplashform (application);
Splash-> show ();
Splash-> Update ();
Application-> Initialize ();
Application-> CREATEFORM (__ classid (tform1), & form1);
Splash-> close ();
DELETESPLASH;
Application-> Run ();
}
Catch (Exception & Exception)
{
Application-> Showexception (& Exception);
}
Return0;
}
Sixth, how to permanently remove the deleted record in DBF
DBF records deleted with Table-> Delete () do not really be deleted from the DBF database, but only make 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 ("cannot delete record",
"error",
MB_ICONSTOP);
7. Implementation of I / O port reading
The careful readers will find that C Builder no longer supports inportb (), Outportb () a class 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 (unsignedsh
Intport, UnsignedCharvalue)
{
//MoveDX, * (& port);
__emit __ (0x8b, 0x95, & port);
// Moval, * (& value);
__emit __ (0x8A, 0x85, & Value);
// Outdx, Al;
__emit __ (0x66,0xee);
}
VoidOutportw (unsignedsh
Intport, unsignedshintintValue)
{
//MoveDX, * (& port);
__emit __ (0x8b, 0x95, & port);
//Movax, * (& value);
__emit __ (0x66, 0x8b, 0x85, & value);
// outdx, AX;
__emit __ (0xef);
}
UnsignedCharinportb (unsignedshintintport)
{
UNSIGNEDCHARVALUE;
//MoveDX, * (& port);
__emit __ (0x8b, 0x95, & port);
// INAL, DX;
__emit __ (0x66,0xec);
// Mov * (& value), Al;
__emit __ (0x88, 0x85, & value);
ReturnValue;
}
Unsignedshortintinportw (unsignedshortintport)
{
UNSIGNedshintintValue;
//MoveDX, * (& port);
__emit __ (0x8b, 0x95, & port);
// INAX, DX
__emit __ (0xed);
// Mov * (& value), AX
__emit __ (0x66, 0x89, 0x85, & value);
ReturnValue;
}
Eight, software distribution