Delphi experience skills highlights
---- 1. Add light color for the list box
---- In order to change the monotonic style of the Listbox and ComboBox controls, I want to add an icon in front of each of them. It is unlikely from the surface. Just when I walked, I suddenly found that both components had a style property. It can be set to lbownerdrawfixed, which draws me, then I found a related event ondrawitem from Delphi's online help. Write a code in this event, you can add a small icon in front of each item in the list box.
---- The following is the code added in the ListBox's OnDrawItem event:
Procedure TFORM1.ListBox1DrawItem (Control:
TWINCONTROL; index: integer;
Rect: TRECT; State: TownerDrawState;
VAR
Bitmap: tbitmap;
OFFSET: Integer;
Begin
With (Control as TListBox) .canvas do
Begin
FillRect (RECT);
OFFSET: = 2;
Bitmap: = Tbitmap.create;
Bitmap.LoadFromfile ('D: /TEMP1/1.BMP');
// Specify the diagram file is d: /TEMP1/1.BMP
Brushcopy (Bounds (Rect.Left 2, Rect.top,
Bitmap.width, Bitmap.height,
Bitmap, Bounds (0, 0, Bitmap.width,
Bitmap.height, CLRED);
OFFSET: = Bitmap.width 6;
Textout (Rect.Top, Rect.top,
(Control as tlistbox) .Items [index])
END;
END;
---- Further, the parameters of the OnDrawItem event can also add different icons in front of the different items. Also tell you, when you add an item as a list box or drop-down box or drop-down box, you will have an onDrawItem event, so that the newly added project has an icon.
---- II. Get count values
---- Delphi is the same as Power Builder, provides SQL language to implement access to the database, just different from the length of the statement. Delphi is developed on the PASCAL language, which provides powerful features, and various controls provide a wealth of properties and methods. We should make full use of these properties and methods. To know the number of records of a database, you can use a simple statement. For example, the total number of records of the DELPHI's DBDemos database is, assuming that the Table control has been added to the FORM, and the specific implementation statement is:
Edit1.text: = INTOSTR (Table1.Recordcount);
If you want to know the number of records that meet a certain condition, it is also very convenient, and the specific implementation statement is:
Edit1.text: = INTTOSTR (query1.recordcount);
The RecordCount property is used to represent the total number of records that are connected to the data set.
There is also a general method, specific implementation statement:
VAR i: integer;
Begin
I: = 0;
Table1.open;
Table1.first;
While Not Table1.eof DO
Beg1n
i: = i 1;
Table1.next;
END;
Edit1.Text: = INTOSTR (i); if the TQuery control is used, the specific steps are as follows:
Query1.close;
Query1.sql.clear;
Query1.sql.add ('... ");
// a query statement
Query1.open
Query1.first;
I: = 0;
While not query1.eof do
Beg1n
i: = i 1;
Query1.next;
End
Edit1.Text: = INTTOSTR (i);
----
3. Give the database report plus the grid line
---- Delphi 1.0 and 2.0 bundled the ReportSmith report production tool to implement report printing. If you print a very simple report, start ReportSmith in the program, it is more troublesome. For relatively simple reports, we generally realize report printing in accordance with the corresponding articles of this newspaper 63. For example, the report prints in the "Puying Business Marketing Management System" written in Delphi 1.0 are self-developed, and the entire system is short, and the integrity is better. Delphi 3.0 and Delphi 4.0 will no longer bundle ReportSmith, but increase the functionality of report controls in the QReport group, making report making more flexible. It is too easy to give the report plus the grid line. TQREXPR used to display data in the QReport component. It has a Frame property, press the F11 key, in "Object Inspect", modify the "frame" property, which provides 7 sub-properties, its functions are as follows:
Color Display Grid Line Color
Style report grid line shape, such as solid line, dotted line, etc.
Width grid line width
Drawbottom grid bottom
Drawlefl's left side
The right side of the DrawRight grid
Drawtop grid bottom
---- Before making a statement, it is best to add several records in the database table, then modify these properties as needed, use the report's preview feature to see the modified effect during the design, not satisfied, can be modified immediately.
---- IV. Custom Speedbar
---- In the visual design interface of Delphi, the most frequent place is located in the acceleration button bar (Speedbar) on the upper left corner of the screen. In practice, the author unintentionally discovered the custom method of SpeedBar, which did not mention it in the four Delphi reference books I have. Proceed as follows:
---- Right-click SpeedBar, select Properties in the pop-up menu, and a dialog named SpeedBar Editor will appear. Suppose you want to add a "syntax check" feature, you can select Project in the Categories list box on the left, then drag and drop the Syntax Check icon on the right, this feature has been added. If there is no extra space on the spetedbar to accommodate the new button, stop the mouse to the right box of Speedbar, turn the right distance to the right, and drag to the right to take a space. If you want to delete a button on the SpeedBar, you can simply drag it out of the speedbar range and you can delete it. It is very convenient to operate. It is recommended to add Syntax Check, Project Options, WindowsAPI Help to Speedbar, which can increase development efficiency.
---- Five. Shortcuts when designing Form
---- Use shortcuts to speed up design speed. The shortcuts listed below can be skilled in order to master according to the actual situation.
Del: Delete the selected component;
ESC: Select the container of the current component (usually Panel, Group or Form); F11: Switch between Form or Unit and Object Checkers;
F12: Switch between Form and code editing device;
Ctrl F12: Displays the "View Unit" dialog;
Shift F12: Displays the "View Form" dialog;
Tab: Next component;
SHIFT TAB: Previous component;
Direction Key: Select the nearest components in this direction;
Ctrl arrow keys: Move the selected component a point;
Shift arrow keys: Change the selected component to the size of a point;
Ctrl Shift arrow keys: move the selected component;
Shift Click: Press and hold the Shift key and click the component with the mouse to select multiple components.
---- The following is my exploitation: Ctrl Drag: Press and hold the CTRL button to drag the mouse in a container component (such as Panel, QReport, GroupBox, etc.) to force all the mouse that is dragged in the rectangular box of the mouse. This container is visible (excluding the container assembly).
---- 6. Use of command line parameters
---- Delphi provides an easy way to access the command line parameters, that is, using paramstr and paramcount functions. Where Paramstr (0) returns the current program name, such as c: /test/myprog.exe, Paramstr (1) Returns the first parameter, and push it; paramcount is the number of parameters. Examples are as follows:
VAR
I: word;
Y: integer;
Begin
Y: = 10;
For i: = 1 to Paramcount Do Begin
Canvas.TextOut (5, Y, Paramstr (i));
Y: = y canvas.textheight (paramstr (i)) 5;
END;
END;
----
Reuse for seven.dcu files (compiled library unit)
---- (1) Quote in the USES clause in the interface in Interface. Such as Windows, Dialogs, etc., requiring the referenced DCU must be placed in the Delphi4 / lib sub-directory.
---- (2) Quote in the USES clause in the Implementation. If it is a DCU file written by yourself, this method should be used and the referenced DCU file is placed in the subdirectory where the current project is located.
---- Eight. Skills using string resources
---- Different from other resources, string resources cannot be edited directly, you need to prepare a text file in the format, and then compile it into a resource file. Next, a simple example is explained below. First use the text editor to edit a file lb.rc, its contents are as follows:
StringTable
Begin
1, "Start"
2, "exit"
End
---- then find the BRCC32.EXE file under Delphi's bin subdirectory to compile, the command format is: Brcc32 lb.rc, and generate a resource file lb.RES after compiling. To use the resource file, you need to include the resource file: {$ r lb.res}. In this example, the above string is used to replace the CAPTION settings for a command button, and the function used is Windows API function loadstring. Here is the example of use:
// During the formcreat:
VAR
TXTCAPTION: ARRAY [0..255] of char; begin
//
If LoadString (Hinstance, 1, TXTCAPTION, SIZEOF
(TXTCAPTION)> 0 THEN
BtnStart.caption: = strpaas (txtcaption);
END;
// During the BTNStartClick process:
VAR
TXTCAPTION: ARRAY [0..255] OF CHAR;
Begin
if loading (Hinstance, 2, TXTCAPTION,
SIZEOF (TXTCAPTION)> 0 THEN
BtnStart.caption: = strpaas (txtcaption);
END;
--- This allows you to change the various properties while the program is run without the need to appear in the program. If you use another Delphi function LOADSTR, it will look simpler:
VAR
TXTCAPTION: STRING;
Begin
TXTCAPTION: = LOADSTR (2);
IF TXTCAPTION <> '' THEN
BtnStart.caption: = txtcaption;
END;
--- Perhaps you can see from the above process, use 123 to identify a string is a string, it is also very easy to make mistakes, so what should we take? We can take the method of using a string like C , for each String predefined an ID, such as:
Const
IDC-START = 1;
IDC-EXIT = 2;
---- Of course, put it in a Unit (similar to C .h file), you can use it in the cells you can, so that the application feel is not very cool?
TXTCAPTION: = LOADSTR (IDC-EXIT);
----
Nine. Let the image rotate
---- The image component in Delphi can display a bitmap, further, we can also use it to complete the bitmap.
---- After rotating a point around the origin, the relationship between the new coordinate position and the original coordinate position is:
X = xcosα-ysinα
Y = xinα ycoosα
For example, to rotate 90 degrees, the coordinate transformation formula is: x = -y y = x
---- The main problem with this formula is displayed on the Image component is that the bitmap displayed by the image component is only one quadmap, and x, y coordinates are also reversed, in order to solve this problem, must be in the image component Build a new coordinate original. The following is an example.
---- 1. Newly built project Project1, add image1, image2, image3, image4 on Form1, which is set to true, iMage1 is used to display the original image, image2, image3, image4, respectively, to display 90 degrees, respectively, An image of 180 degrees and 270 degrees. Double click on image1, select a BMP map.
---- 2. Add button1, button2, button3 and butt4 buttons, its CAPTION attribute is "original map", "rotation 90 degrees", "rotation 180 degrees", "rotation 270 degrees".
---- 3. Write the ON Click event of the "Rotating 90 degree" button.
Procedure TFORM1. Button2Click (Sender: TOBJECT);
VAR
I, J: Integer;
Begin
/ / Determine the size of the rear bitmap
Image2.picture.bitmap.height: = image1.Picture.Width; Image2.Picture.bitmap.width: = image1.Picture.Height;
For i: = 0 to image1.height do
For j: = 0 to image1.width do
Image2.canvas.pixels [(- i image1.height),
j]: = image1.canvas.pixels [j, i];
END;
---- 4. Write the ON Click event of the "Rotating 180 degree" button.
Procedure TFORM1.BUTTON3CLICK (Sender: TOBJECT);
VAR
I, J: Integer;
Begin
/ / Determine the size of the rear bitmap
Image3.Picture.bitmap.Height: = image1.Picture.height;
Image3.picture.bitmap.width: = image1.Picture.width;
For i: = 0 to image1.height do
For j: = 0 to image1.width do
Image3.canvas.pixels [(image1.width
-J), (image1.height-i)]: = image1.canvas.pixels [j, i];
END;
---- 5. Write the "Rotating 270 degree" button on the Click event. The code and step 3 are similar, just replace Image2 with image4, then replace the original statement in the FOR cycle with the following statement.
Image4.canvas.pixels [i, (image1.width-j)]: = image1.canvas.pixels [j, i];
----
Ten. Programming implementation file association
---- Document The purpose is to implement some of the extension files to open by the corresponding application. For the written application, due to the general designer specific extension data file, software preparatory Automatic correlation should be implemented in the program without manual implementation. The utility that implements file association is given below.
---- Implementing the core of the file is the operation of the registry, all file associations are under HKEY-CLASSES - ROOT, to implement file associations to add two key values in HKEY-CLASSES-ROOT, one, and The file extension corresponds to the type description, and the other is the application that is required to perform this type of file. The following is an example, this example implements the file that will define the file that is .xyz as "my own file" type, and double-click the open function that is executed when this type of file is performed using the Noteping program. Implement this feature requires the function of Delphi RegreateKey, RegSetValue. The implementation is as follows:
---- Place a button in the form, double-click the button to write the following code:
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
VAR
LPHKEY: HKEY;
SKEYNAME: STRING;
SKEYVALUE: STRING;
Begin
SKEYNAME: = 'myfile';
SKEYVALUE: = 'My Document';
RegcreateKey (HKEY-CLASSES-ROOT,
Pchar (SKEYNAME), LPHKEY
RegSetValue (LPHKEY, '', REG-SZ,
PCHAR (SKEYVALUE), 0);
SKEYNAME: = '.xyz';
SKEYVALUE: = 'myfile';
RegcreateKey (HKEY-CLASSES-ROOT,
Pchar (SKEYNAME), LPHKEY); RegSetValue (LPHKEY, '', REG-SZ,
PCHAR (SKEYVALUE), 0);
SKEYNAME: = 'myfile';
SKEYVALUE: = 'c: /windows/notepad.exe% 1';
RegcreateKey (HKEY-CLASSES-ROOT,
Pchar (SKEYNAME), LPHKEY
RegSetValue (LPHKEY, 'Shell / Open / Command', REG-SZ,
Pchar (SKEYVALUE), MAX-PATH);
END;
---- Establish a new file to extend its extension as .xyz, then double-click whether it is to start the Notepad.
---- Eleven. Displays the multi-database in a DBGRID
---- In database programming, it is not necessary to put all database fields of the application operation in a database file. The correct database structure should be: put the database field into multiple database files, and the relevant database contains a unique key field that can be set in the multi-database structure.
---- For example: To prepare a personnel management program, to simplify the demo, only two databases, each database only established two fields.
---- Personal Profile Jianjie.dbf
---- 1. Establishment of the database
Enter Database Desktop, the establishment of the database structure is as follows:
Jianjie.dbf
Name: Bianhao Size: 4 Type: Number
Name Field Name: Xingming Size: 10 Type: Character
gongzi.dbf
Name: Bianhao Size: 4 Type: Number
Salary field name: gongzi size: 4 DEC 2 TYPE: NUMBER
---- Note: Size, Type of the Bianhao field of the two databases must be consistent. In fact, the two database files can be distributed on different computers of the network to facilitate demonstrations, respectively, to "c: /test/jianjie.dbf" and "c: /test/gongzi.dbf", respectively.
---- 2. Preparation of the application
---- Start Delphi, create a new project, join the Query control query11 in the form, and the DatabaseName property is set to C: / Test;
---- Join the DataSource control DataSource1, the DataSet property is set to query1; join the DBGRID control DBGRID1, the DataSource property is set to DataSource1, set the query1.sql property to
Select Distinct A.Bianhao, A.XINGMING, B.Gongzi
From "jianjie.dbf" a, "gongzi.dbf" B
Where a.bianhao = B.Bianhao
---- Set the query1.enabled attribute to true, do not compile, DBGRID1 will display: bianhao, xingming, gongzi three fields. If there is record in jianjie.dbf and gongzi.dbf, the record will be displayed. Because of the boundary, this article only introduces the general method of displaying multiple database contents in DBGRID, readers can improve on this, so that this method can better adapt to your needs.