?
Unit 3, common dialogue and scrapbook
3.1 Using a common dialogue
Common dialogue
3.1.1 Using the Synchronization Talks
Making Open Old Geng Options Add OpenDialog. Click the omit button on the right side of the Filter feature to appear in the Filter Edit dialog.
Enter the information according to the following image below. Set the FilterIndex characteristic to 2 (the preset filter condition is * .pas). Delete ScrollBox and join the Richedit component. Double-click the file | Open the old file, complete the following procedure.
Procedure Tform1.Open1Click (Sender: TObject); Begin if OpenDialog1.Execute then begin RichEdit1.Lines.LoadFromFile (OpenDialog1.FileName); Caption: = Format ( '% s -% s', [ExtractFileName (OpenDialog1.FileName), Application .Title]); Richedit1.Setfocus; richedit1.modified: = false; richedit1.readonly: = ofreadonly in opendialog1.options;
Making an option to open a new file to open a new file-in-mail event handler fill in
Procedure tform1.new1click (sender: Tobject); begin caption: = format ('% s -% s ", [' untricted ', Application.title]); Richedit1.Lines.clear; Richedit1.Modified: = false;
The private program declared in the PRIVATE part of the category of the table.
Type ... private ffilename: string; procedure setcaption; public {public declarations} end;
Add below in the actual part.
Procedure tform1.setcaption; begin ffilename: = filename; caption: = Format ('% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s -% s, "
Change the original program to
Procedure Tform1.Open1Click (Sender: TObject); Begin if OpenDialog1.Execute then begin RichEdit1.Lines.LoadFromFile (OpenDialog1.FileName); SetCaption (OpenDialog1.FileName); RichEdit1.SetFocus; RichEdit1.Modified: = False; RichEdit1.ReadOnly: = OfreadOnly In OpenDialog1.Options; end; End; Procedure TFORM1.NEW1CLICK (Sender: TOBJECT); begin setcaption ('untricted'); richedit1.lines.clear; richedit1.modified: = false; end;
3.1.2 Using the Archive Common Dialog
Options for making storage files are added to the SaveDialog component and set their Filter's characteristics as the OpenDialog. Default is set to "PAS", and FilterIndex is set to 2. Select File | function menu to save the file, which in the event handler OnClick fill procedure TForm1.Save1Click (Sender: TObject); begin if FFileName = 'Untitled' then SaveAs1Click (Sender) Else begin RichEdit1.Lines.SaveToFile (FFileName) Richedit1.modified: = false; end;
Making a Save a Save Note Option Select Archive | Save the new file function menu, fill in its OnClick event handler
procedure TForm1.SaveAs1Click (Sender: TObject); begin if not RichEdit1.Modified then exit; SaveDialog1.FileName: '.' = FFileName SaveDialog1.DefaultExt; if SaveDialog1.Execute then begin if FileExists (SaveDialog1.FileName) then if MessageDlg ( Format ( 'Do you want to replace the original file% s', [SaveDialog1.FileName]), mtConfirmation, mbYesNoCancel, 0) <> idYes then Exit; RichEdit1.Lines.SaveToFile (SaveDialog1.FileName); setCaption (SaveDialog1.FileName) Richedit1.modified: = false; end;
Setting the initial value: Add the initial value to select the object FORM1 in the table's oncreate event, double-click the oncreate event, enter in the program editor
procedure TForm1.FormCreate (Sender: TObject); begin OpenDialog1.InitialDir: = ExtractFilePath (ParamStr (0)); SaveDialog1.InitialDir: = OpenDialog1.InitialDir; SetCaption ( 'Untitled'); end;
3.1.3 Thoughtful design
Before an open file or store file, check whether the program is changed, and processes.
Add a line of Issafe under the declaration of SetCaption.
... private ffilename: string; procedure setcaption; procedure issafe; public {public declarations}
In the actual part, add the following.
Procedure TFORM1.issafe; var resp: integer; begin if not richedit1.modified kilod; resp: = messageDLG (Format ('Do you want to store more to file% s?', [ffilename]), MTConfirmation, mbyesnocancel, 0) Case Resp of iDYES: Save1Click (Self); IDNO: {Don't do what processing}; IDCANCEL: ABORT; END;
Double-click the ONCLOSEQUERY event of Form1, enter the following program.
Procedure TForm1.FormCloseQuery (Sender: bobject; var canclose: boolean); recompt canclose: = false; end; end; part of the Issafe is also checked in part of the file and archive.
3.2 Use a scrapbook
3.2.1 Using TclipBoard objects
The use of the scrapbook is announced to find the IMPLEMentation part of the unit, and the Clipbrd is added to the USES clause. Characteristics and methods provided by Richedit
Features / Method Description SELTEXT Currently selected text SelLength Currently, the length of the text selected by the user. SELSTART Currently, the use of the text selected by the user. Selectall All text content in Richedit Clearselection clears the current user selected Text CutToClipboard cuts the selected text and copy it to the scrapbook CopyToClipboard to copy the selected text to the scrapbook PASTEFROMCLIPBOARD Copy the contents of the scrapbook to the location of the cursor.
Making Edit menu Select Edit | Cut, enter the following code.
Procedure tform1.cut1click (sender: TOBJECT); Begin Richedit1.cuttoclipboard;
Select Edit | Copy, enter the following code code.
Procedure TFORM1.COPY1CLICK (Sender: TOBJECT); Begin Richedit1.copyToclipboard; End;
Select Edit | Post, enter the following code.
Procedure TFORM1.PASTE1Click (Sender: TOBJECT); Begin Richedit1.PastefromClipboard; End;
Select Edit | Restore, enter the following code.
Procedure tform1.undo1click (sender: TOBJECT); Begin with Richedit1 Do if HandleAllocated Ten SendMessage (Handle, EM_UNDO, 0, 0); END;
Select Edit | Select all, enter the following code.
Procedure tform1.selectall1click (sender: TOBJECT); begin richedit1.selectall;
Select Edit | Look, Enter the following code.
Procedure TFORM1.FIND1CLICK (Sender: TOBJECT); Begin FindDialog1.execute; End;
Add the following code in the onfind event of FindIalog1
procedure TForm1.FindDialog1Find (Sender: TObject); var FoundAt: LongInt; StartPos, ToEnd: integer; begin with RichEdit1 do begin {begin the search after the current selection if there is one} {otherwise, begin at the start of the text} If SelLength <> 0 Then Startpos: = SELSTART SELLENGTH Else Startpos: = 0; {TOEND Is The Length from Startpos To The end of the text in the rich edit control} toend: = length (text) - startpos ;fact: = Findtext (FindDialog1.FindText, StartPos, Toend, [StmatchCase]); iffact: = Foundat; SELLENGTH: = Length (FindDialog1.FindText); end; end; end; decision menu use time : Unselected, you cannot double-click the editing menu for the function menu design tool. Complete the following procedures:
procedure TForm1.Edit1Click (Sender: TObject); var HasSelection: Boolean; begin Paste1.Enabled: = Clipboard.HasFormat (CF_TEXT); HasSelection: = RichEdit1.SelLength> 0; Cut1.Enabled: = HasSelection; Copy1.Enabled: = HasSelection ;
Execute a look.
3.2.2 Using TiniFile objects
How to get a post to the last state?
Write the second program unit to select File | New, select Unit in the New Items dialog box. Complete the following procedures:
Unit Utils; Interface Uses SysUtils, Forms, IniFiles; Procedure SaveFrmPos (AForm: TForm; Name: String); Procedure SetFrmPos (AForm: TForm; Name: String); Implementation // own internal use function and record type type TPosRec = Record Top, Left, Height, Width, State: Integer; End; {Convert Word type content to the list} Function INTTOWINSTATE (INT: WORD): TwindowState; Begin Case Int of 0: Result: = WSNORMAL 1: Result: = wsminimized; 2: Result: = wsmaximized else result: = wsnormal; end; end; {Convert to INI file to use the string} Function PosRecostr (POS: tposrec): string; begin with pos do if State <> 2 THEN // Test Whether to maximize the status of the state result: = INTTOSTR (state) // Current table Status '' INTOSTR (TOP) // Location '' INTOSTR (LEFT) // Location '' ' INTOSTR (HEIGHT) // Height' ' INTOSTR (Width) // Width else result: = INTOSTR (STATE); END; {Find numbers from strings} Function StrtoposRec (Astring : String: tposrec; var temp: string; i, j: integer; blanks: array [1..4] of integer; begin {setting variable initial value} i: = 1; J: = 1; Fillchar (Result SizeOf (Result), # 0); Fillchar (Blanks, Sizeof (Blanks), # 0); // Check View the status of the window - if it is wsmaximized, then there is no need to do any processing Temp: = COPY (Astring, 1, 1); result.State: = strt (TEMP); if Result.State = 2 dam; // Find blank repeat if astring [i] = '' Then BEGINKS [J] = i; inc (j); end; inc; (i); uns: = ip; uNd; = length (astring) OR (J> 4); // remove the desired number Temp: = Copy from the string: = Copy (Astring, Blanks [1] 1, Blanks [2] -blanks [1] -1); Result.top: = start (STRTOINT) Temp: = Copy (Astring, Blanks [2] 1, Blanks [3] -blanks [2] -1); Result.Left: = start (TEMP); TEMP: = Copy (Astring, Blanks [3 ] 1, Blanks [4] -blanks [3] -1); Result.Height: = start (TEMP); TEMP: =
COPY (Astring, Blanks [4] 1, Length (Astring) -blanks [4]); Result.Width: = start (TEMP); END; // Write the size / status / and position of the table to the INI file procedure SaveFrmPos (AForm: TForm; Name: String); var IniFile: TIniFile; PosRec: TPosRec; Begin // sets the initial value FillChar (PosRec, SizeOf (PosRec), # 0); IniFile: = TIniFile.Create (ChangeFileExt (ExtractFileName (Paramstr (0)), '.ini')); TRY / / Table of Table Features with Posrec Do Begin State: = ORD (AFORM.WINDOWSTATE); top: = AFORM.TOP; Left: = AFORM.LEFT; Width : = AFORM.WIDTH; Height: = AFORM.HEIGHT; END; // Write the information to the file in INIFILE.WRITESTRING ('POSREC)); Finally InIfile.Free; end; end; ///////// Read the form of the table from the INI file Procedure setFrmpos (AFORM: TForm; Name: string); var inifile: tinifile; POSSTRING: STRING; POSSTRING: STRING; Posrec: tposrec; begin // Setting initial value Fillchar (Posrec, Sizeof (Posrec), # 0); inIfile: = tinifile.create (ChangefileExt (PARAMSTR (0)), '.ini')); TRY / / Read the information from the INI file POSSTRING: = Inifile.ReadString ('Positions', Name, 'Default'); Finally Inifile.Free; end; // If you don't read something, then you don't have to do any further processing i. F Posstring = 'default' TEN EXIT; POSREC: = STRTOPOSREC (POSSTRING); // Setting the location of the table with AFORM DO BEGIN WindowState: = INTTTOWINSTATE (Posrec.State); if Posrec.State = 2 Then Exit; Top: = Posrec.top; left: = posrec.left; width: = posrec.width; height: = posrec.height; end; end; end. Save location
Enter the following program in the ONDESTROY event of Form1:
Procedure TForm1.FormDestroy (Sender: Tobject); Begin SaveFRMPOS (Form1, Form1.className);
Restore table
Inform1's oncreate event input: