"Smart programmers use Delphi!" Now, more and more people are starting to use Delphi. However, after all, there are a few experts, and novices like me are everywhere, haha. Since I am a rookie, I naturally have a lot of very good questions, such as how to set up this and how to write that... So, below, I will make a summary speech based on the little experience I have accumulated and the common small questions on the forum. (The audience applauded warmly! Alas, I took the flowers, don’t throw them up any rotten tomatoes)
==System Applications==
A. Delphi has replaced a large number of commonly used controls, such as Delphi5, and the Delphi6 released this year is 1.5 times that of Delphi5. With so many controls, even at the resolution of 1024*768, they cannot display all their component panels, and it is really inconvenient to press those two small buttons to display more panels each time! However, by adding the following registry key value, the component panel can be automatically expanded:
[HKEY_CURRENT_USER/Software/Borland/Delphi/X.0/Extras]
"AutoPaletteSelect"="1"
(Note: X.0 indicates the version number of your Delphi; if there is no Extras primary key, just create a new one, the same below)
B. Every time I create a new form and then set its font and size... it is really troublesome! Then add the following key value and let the system automatically set it:
[HKEY_CURRENT_USER/Software/Borland/Delphi/X.0/FormDesign]
"DefaultFont"="宋体,9,_"
(Note: _ represents space, and can also be replaced by Bold (bold) etc.)
C. Customize the work environment to make it more suitable for you.
•Settings toolbar. Right-click on the Delphi toolbar, select Customize, then select the required button and drag the tool, and drag the unwanted ones out. What I often use are: Open, Open PRoject, Save, Save All, Undo, Redo under the Standard toolbar; Pause, Add Watch, Program Reset, Run under the Debug toolbar; Save Current Desktop, Set Debug under the Desktop toolbar Desktop; Component Palette toolbar.
•Set window layout. In order to be able to write programs efficiently, it is important to arrange each window reasonably. My commonly used windows are: Object Inspector, Project Manager (View|Project Manager), Watch List (Run|Add Watch...). The specific parking location is shown in Figure 1.
After setting up each form, press the Save Current Desktop button on the Desktop toolbar to save.
•Set the display color. Make the color of the code more in line with your habits, which is conducive to clear thinking when writing. Tools|Editor Options Open Editor Properties, select the Color tab, and set the code color. Here are only a few commonly used ones:
Whitespace: the blank space, that is, the background;
Comment: Comment;
Reserved Word: reserved words;
String: string;
Number: number;
Execution Point: the current line being executed (that is, the line that arrives when F7 steps into);
Enabled break: breakpoint;
Error line: The error occurred line;
D. Mastering some shortcut keys in Delphi programming can greatly improve efficiency and will not make you bored by too many boring settings. Now if you don’t use these shortcut keys, I will be very uncomfortable with such programming! Here are some shortcut keys I often use:
Esc: Select the container of the currently selected component (usually Panel, GroupBox, Form, etc.);
Ctrl+arrow key: moves the selected component by one pixel;
Ctrl+Shift+arrow keys: move the selected component by a large grid (similar to the effect of holding Shift in fireworks and then moving with the arrow keys);
Shift+Dragon keys: Adjust the size of the selected component, one pixel at a time;
Shift+Select components: Hold down the Shift key and click on the component with the mouse. You can select multiple components at the same time;
Ctrl+left mouse button: Use other functions in the procedure (such as a custom process). Press and hold Ctrl at this call and find that when the mouse moves up, it becomes a format similar to a hyperlink. Click the mouse You can directly go to the implementation part of this function called. I have used this function the most, making it very convenient for me to find modules!
Ctrl+Shift+C: Declare a method in an object, then put the cursor on it, press Ctrl+Shift+C to establish a basic framework for its implementation, such as:
TForm1 = class(TForm)
Private
{ Private declarations }
public
{ Public declarations }
procedure Mine;
end;
Put the cursor on the procedure Mine line, press Ctrl+Shift+C, and the following code will be automatically inserted into the unit:
procedure TForm1.Mine;
Begin
end;
==Form Operations==
In programs with multiple forms, Delphi defaults to create them all when the program starts (it's just hidden, only one main form is displayed), so that those forms that are temporarily unnecessary to start occupy a lot of memory space. So we need to transfer those forms that are not started for the time being from "Auto-create forms" to "Available forms" under the Forms tab of the menu Project|Options... In this way, the creation and release of these forms require code to implement.
A. Comparison of two ways to create a form. For these non-automatically created forms, we must create them before Show (Self) or Create (application). For example, Form2.Creat(Self) and Form2.Create(Application), but what is the difference between them? Actually, there is not much difference between the two.
But if the Owner belongs to another window or control, when its Owner is released, what the Owner has will be released. The key difference is who manages the resources of the form. Created in the first method, the resources of the form are managed by the programmer; created in the second method, the resources of the form are managed by the Application.
B. Several ways to release forms. Since it is to save memory, the used-up form should "mov out" the memory. Here I will list a few similar methods:
(1)
procedure TForm1.Button1Click(Sender:TObject);
Begin
......
if Form2 = nil then
Form2:=TForm2.Create(Application);
Form2.ShowModal;
......
end;
procedure TForm2.close(Sender: TObject; var Action: TCloseAction);
Begin
ModalResult := mrNone;
Action := caFree;
Form2 := nil;
end;
(2)
procedure TForm1.Button1Click(Sender:TObject);
Begin
If not Assigned(Form2) then
Begin
Application.CreateForm(TForm2,Form2);
try
Form2.ShowModal;
Finally
Form2.Free;
Form2 := nil;
end;
end
else
Form2.ShowModal;
end;
(3)
procedure TForm2.Button1Click(Sender:TObject);
Begin
Form2.Close;
Form2.Release;
end;
==Control usage
A. The famous RXLib control package is written by three Russian experts and is very outstanding! However, there is too much content, and it is not easy to use it well. I'll just say a few points here to RxRichEdit.
•Many people have asked how to install RxLib, it seems to be a bit complicated, haha, so I'll also say it by the way:
Run rxinst.exe to install RxLib into /Program Files/Borland/Delphi5 (or 6)/RX folder, then open Delphi, select File/Open..., and turn RXCTL5.DPK, RXDB5.DPK, RXBDE5.DPK After opening, press compile, continue to open: DCLRX5.DPK, DCLRXDB5.DPK and DCLRXBD5.DPK. Press these three to compile and install :-)
•AllowObjects property. The default setting of Win98 is True. There is no problem with the right-click menu, but when set to False, the shortcut menu will appear twice in a row; when set to True, the right-click menu will not pop up, but when False, it will be displayed normally. Therefore, for system compatibility, it is necessary to determine the current OS version:
procedure TFormMain.FormCreate(Sender: TObject);
var
OSVI:OSVERSIONINFO;
......
Begin
OSVI.dwOSVersionInfoSize:=sizeof(OSVERSIONINFO);
GetVersionEx(OSVI);
case OSVI.dwPlatformId of
1:RxRichEdit1.AllowObjects:=true;
2:RxRichEdit1.AllowObjects:=false;
end;
.........
end;
But I don’t know why this attribute has such a problem, so I hope the expert will give me some advice.
•OnURLClick event. RXRichEdit automatically converts web page addresses to hyperlink format, but the specific behavior still requires writing code by itself, otherwise these addresses will become empty shells:
procedure TFormMain.RXRichEdit1URLClick(Sender: TObject; const URLText: String; Button: TMouseButton);
Begin
if Button = mbLeft then
ShellExecute(Handle, nil, PChar(URLText), nil, nil, SW_SHOW);
end;
B. In order to save memory and optimize operation, we always create components dynamically. But after using it up, if it is not cleared out of memory in time, it will go against our original intention. But how to "kill" the created component? For example, I created an Edit control and now I want it to disappear, but after using Edit.Free, calling Edit.Text still exists... We know that Free is not possible, this It just frees up the memory space pointed to by Edit, but the pointer is not set to nil. When Edit.Text is called, Delphi will still access the released memory area according to the pointer provided by Edit, so access Violation will be generated... .mistake. Therefore, we need to nil the pointer after Free Edit:=nil or FreeAndNil (only valid under Delphi5) to ensure normal operation in the future.
C. Displays the prompt text of each control on the status line. This application's OnHint event can be used, but since Application is an incompatible object, the following method can be used:
......
public
procedure DisplayHint(Sender: TObject); //Declare a method to display prompt text
end;
......
procedure TForm1.DisplayHint(Sender: TObject); //Implementation part
Begin
StatusBar1.SimpleText := GetLongHint(Application.Hint);
end;
procedure TForm1.FormCreate(Sender: TObject);
Begin
Application.OnHint := DisplayHint;
end;
This way the text displayed in the status bar is the same as the prompt box (the small yellow box that automatically appears). What if you want the prompt text in the status bar to be more detailed? In fact, as long as the Hint property of the control is written like this: MyHint|It's just a MyHint!, the content before "|" can be displayed in the prompt box (Help Hint box), and the content after "|" is displayed in the status bar. Now.
D. Some key points in the use of ListView. The following is a two-column ListView as an example.
→Add a line:
with ListView1 do
Begin
ListItem:=Items.Add;
ListItem.Caption:='First column content';
ListItem.SubItems.Add('Second column content');
end;
→Clear ListView1:
ListView1.Items.Clear;
→ Get the line number of the currently selected row and delete the current row:
For i:=0 to ListView1.Items.Count-1 Do
If ListView1.Items[i].Selected then //i=ListView1.Selected.index
Begin
ListView1.Items.Delete(i); //Delete the currently selected row
end;
Of course, ListView has an OnSelectItem event, which can determine which row you selected and assign it to a global variable.
→Operation of reading a row and a column:
Edit1.Text := listview1.Items[i].Caption; //Read the first column of the i-th row
Edit2.Text := listview1.Items[i].SubItems.strings[0]; //Read the 2nd row of the i-th column
Edit3.Text := listview1.Items[i].SubItems.strings[1]; //Read the i-th row, column 3
By analogy, the entire column can be read out using a loop.
→ Move the focus up one line:
For i:=0 to ListView1.Items.Count-1 Do
If (ListView1.Items[i].Selected) and (i>0) then
Begin
ListView1.SetFocus;
ListView1.Items.Item[i-1].Selected := True;
end;
However, in Delphi6, ListView has an ItemIndex attribute, so just
ListView1.SetFocus;
ListView1.ItemIndex:=3;
You can set the focus.
==Others==
• In order to optimize the software and make it more perfect, dynamic arrays are often used when using arrays. When declaring, such as: A: array of string, and then before using the array, SetLength(A,3) sets the array length to 3, that is, A[0], A[1], A[2], so that's it . When it is necessary to expand the array length, again: SecLength(A,5), then A[3] and A[4] are added, and in the previous A[0], A[1], A[2] The value of , will not be cleared; similarly, if SecLength(A,3) and SecLength(A,1) are then SecLength(A,1), only A[0] is left, and A[1] and A[2] are automatically cleared from memory. Note: Dynamic arrays start with "0", so after SetLength(A,1) High(A)=0! !
Finally, when this dynamic array is no longer used and the program has not yet exited, in order to save memory, use: A:=nil to release this array, and now it truly achieves "green and environmental protection" :)
•The question "How to get the path to the current program running" can often be seen in forums. Indeed, this function is used from time to time in programming. There are actually several functions that can achieve this effect:
→GetCurrentDirectory(): WinAPI function, the specific usage is as follows:
var
MyCurrentDir:Array[0..144] of Char; //Storage the current absolute path
Begin
GetCurrentDirectory(SizeOf(MyCurrentDir),MyCurrentDir); //Get the current absolute path, and the final format is as follows: C:/WINDOWS/SYSTEM
MyCurrentDir:=MyCurrentDir+'/MySoftName.exe';
end;
→GetCurrentDir(): A function encapsulated by GetCurrentDirectory by Delphi, the usage is simple: MyCurrentDir:=GetCurrentDir. The final format is: C:/WINDOWS/SYSTEM
There are also functions that can directly obtain the path: ExtractFilePath(ParamStr(0)), ExtractFilePath(Application.exename), etc.
• Messages of system shutdown, restart, etc. This is also a question that people ask, and I will mention it here:
Shutdown: ExitWindowsEx(EWX_SHUTDOWN,32);
Restart: ExitWindowsEx(EWX_REBOOT,32);
Logout: ExitWindowsEx(EWX_LOGOFF,32);
Power off: ExitWindowsEx(EWX_POWEROFF,32);
•In Delphi, in addition to using #39, how to deal with "single single quotes" in a program (such as ShowMessage)? Both ''' and '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' In fact, four single quotes represent a single quote, that is, '''', the second and third indicate that this is a "'", and the first and fourth indicate that this is a character type. Haha, very interesting :)
•Chinese issues in ShowMessage. The buttons in ShowMessage are all in E-text, which makes people feel uncomfortable. In fact, after our DIY, we can make them all into Chinese.
First, save the current project, and then select Project|Languages|Add... The Add Language dialog box appears, select the project you want to Chinese (you can also customize the folder), Next; select the last item "Chinese (China) ”, Next; Next; Next; Finish; OK; Then the Translation Manager dialog box appears, select "Chinese (China) | Resource Scripts |Projet1_DRC (This depends on your project file name)" on the left, and then the Find the "Const_SMsgDlgOK" item in the list, change its "Chinese (translated)" pattern to "OK" (original)), and exit after saving. Now, run the program again, haha, is it just the Chinese button?
However, a message dialog box of ShowMessage is very simple and can be displayed to developers during system debugging. But because it is too simple, I recommend not to use such dialog boxes in the official version of the software. Moreover, it is best to use MessageBox instead of ShowMessage to reduce the size of the software. For example, for the same DLL file, the DLL file compiled with MessageBox is 58K, and after ShowMessage, it becomes 301K! !