Fourth chapter-Text editor Design (二) (2)

Source: Internet
Author: User
Tags range textout valid

The Teditform.replace method in the routine responds to the Onreplace event, and the Replace method first determines that the control is

Select whether the string is equal to the replacement string, or replace it if it is unequal. And then according to the options in the way to follow

Loop for a lookup replacement. Until there is no matching string. The code is as follows:

Procedure Teditform.replace (Sender:tobject);

Var

Found:boolean;

Begin

With ReplaceDialog1 do

Begin

If Ansicomparetext (Memo1.seltext, FindText) = 0 Then

Memo1.seltext: = ReplaceText;

Found: = Searchmemo (Memo1, FindText, Options);

While Found and (Frreplaceall in Options) do

Begin

Memo1.seltext: = ReplaceText;

Found: = Searchmemo (Memo1, FindText, Options);

End

if (not Found) and (Frreplace in Options) then

ShowMessage (' cannot find ' + FindText + '. ');

End

End

4.4.4 Open the dialog box part

Opens the dialog box to open the dialog box for application display. Use the Execute method to display Open dialog box users can determine which files appear in the file list by selecting a file type in the file type Drop-down box. For example, if the user chooses the *.txt file type, only the text file in the current directory will appear in the file list. The file name extension is also commonly referred to as a filter.

The Open dialog box contains a filters (filter) property that determines the file type and order in the File type Drop-down box. The application can define multiple filters for the Open dialog box, and the FilterIndex property of the dialog box determines which filter is the default filter in the File type Drop-down box. If FilterIndex equals 2, the filter that appears in the File type Drop-down box when the program runs is the 2nd filter.

The code for opening a file in a routine is as follows:

Procedure Teditform.open/click (Sender:tobject);

Begin

If opendialog/. Execult Then

Begin

...

Open (Open dialog/. FileName)

End

End

Open, save the options attribute value in the dialog box, see table 4.4

Table 4.4 Options property values and implications for opening and saving dialog boxes

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Value meaning

──────────────────────────────────────

Ofallowmultiselect If True, allows multiple files to be selected in the list of file names.

Ofcreateprompt If it is true, when the user enters a file name that does not exist in the file edit box,

and select the OK button, a message box appears prompting the user that the file does not exist and

asks if you want to create a new file with this file name.

Ofexiengrondifferent If it is true, the file name extension returned from the dialog box is different from the default extension.

The value is stored in the Defaultext property.

Offilemustexist If it is true, when the user enters a filename in the file edit box that does not exist,

and select the OK button, a message box prompts the user that the file does not exist and consults

Ask if the correct path and file name have been entered.

Ofnochangedir If it is true, the current directory will be set to the first occurrence of the dialog box, and suddenly

Slightly any directory change.

Ofoverwriteprompt if the value is true, a message appears when the user attempts to save an existing file

box, prompts the user that the file already exists and asks if it is overwritten.

Ofpathmastexit If it is true, the user can only enter a valid pathname in the File name edit box, otherwise

The current message box prompts the user that the path is invalid.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Table 4.4 Options property values and meanings in the dialog box open and save

The File Save dialog box is similar to opening a dialog box, as shown in Figure 4.11. Its option attribute is shown in the table above. Routines Read and write to the file before saving the file, and if the file is a read-only file or a new file that does not specify a filename, the program does not save the file, or it backs up the file. The code is as follows:

Procedure Teditform.save1click (Sender:tobject);

Procedure CreateBackup (const filename:string);

Var

backupfilename:string;

Begin

BackupFileName: = Changefileext (Filename, Backupext);

DeleteFile (BackupFileName);

RenameFile (Filename, BackupFileName);

End

function isreadonly (const filename:string): Boolean;

Begin

Result: = Boolean (Filegetattr (Filename) and fareadonly);

If result then Messagedlg (Format ('%s ') Read only. ',

[Extractfilename (Filename)]), mtwarning, [Mbok], 0);

End

Begin

if (filename = ') or isreadonly (filename) Then

Saveas1click (Sender)

Else

Begin

CreateBackup (Filename);

Memo1.Lines.SaveToFile (Filename);

Memo1.modified: = False;

End

End

Where the CreateBackup procedure is used to change the extension of the file that needs to be backed up. IsReadOnly is used to determine file attributes.

4.5 File Printing

In Delphi, there are two ways to print a file:

1. Assign a file variable to a printer, and after you create or open a file with this variable name, any text you write to the file variable is considered to be output to the printer, and the following procedure enables you to print the file.

Procedure Teditform,print1click (Sender:tobject);

Var

Line:integer;

PrintText:System.Text;

Begin

If Printdialog1.execute Then

Begin

ASSIGNPRN (Printtext)

Rewrite (Printtext);

Print.canvasfont: = Memo1.font;

For line: = 0 to Memo1.lines.count-1 do

Writeln (Printtext,memo1.line[line];

System.close (Printtext);

End

End

2. Using the Tprinter object defined in the Printers unit for file printing, this example uses this method to print the file.

4.5.1 Tprinter Object

The Tprinter object can invoke Windows printers, and the Tprinter instance printer is defined in the printer unit, which can be used directly by the user.

Call the Tprinter Begindoc method to start a print job, calling the EndDoc method to end a work that has been successfully sent to the printer. Call the Abort method if a problem occurs during the send or if the user wants to terminate the print operation halfway through.

You can test whether a print job is currently available by checking the printing property, and if the print job is terminated, the Abort property is true.

The Canvas property represents the print surface, and the Brush,font,pen property determines the characteristics of the printed font or image.

The Printers property contains the list of installed printers, and the Printerindex property is the currently selected print

Machine, the Fonts property has the font supported by the current printer. The Orientertion property determines the direction in which to print.

The pageheight,pagewith contains the current height and width. Pagenanber is the value of the current page.

Set the Title property to determine the text that appears on the Windows Print Manager or network.

4.5.2 tprintdialog Print dialog box

The Tprintdialog part displays a print dialog box. In the dialog box, you can select the printer, the number of pages to print, and how many copies to print. The Print Setup dialog box appears when the user selects the Setup button in the dialog box.

Call the Execute method to display the Print dialog box. As shown in Figure 4.12. Use the Option property to set the form that the Print dialog box displays. The options are set as shown in table 4.5.

The PrintRange property defines the range of printing. If the value of the PrintPage is Prpagenums, you can set the FromPage and ToPage properties to determine the print range. Set the Minpage,maxpage property to restrict the user's print range.

Table 4.5 The value and meaning of the option attribute of the Print dialog box

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Value meaning

──────────────────────────────────────

Pohelp if the value is true, the dialog box appears with the Help button.

Popagenums If it is true, the page buttons are valid and the user can set the print range.

Poprinttofile If it is true, the file Print check box appears in the dialog box, and the user can select

Optional file printing.

Poselection If it is true, the Select button is valid and the user can print the text selected in the file.

Powarning if the value is true, the user chooses OK to press the button when the printer is not installed

The warning message is now.

Podisableprinttotofile if the value is true and the poprinttofile is truth, when the dialog box appears, the text

The Print dialog box will not work.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This chapter is a routine that uses printer canvas for text printing. When the user selects the Print menu, the Print dialog box pops up and the user can set various parameters. When the user selects the Print button, the print work is sent, the Print Cancel dialog box will pop up, see Figure 4.13, the user can abort the printing work. The code for printing and printing cancellation is as follows:

Procedure Teditform.print1click (Sender:tobject);

Var

Distanceline,line:integer;

PrintText:System.Text;

Begin

If Printdialog1.execute Then

Begin

Printer.Canvas.font: = Memo1.font;

Distanceline: = Trunc (1.5*fontdialog1.font.size);

Openprintcanceldialog;

Printer.begindoc;

For line: = 0 to Memo1.lines.count-1 do

Begin

Printer.canvas.textout (0,distanceline*line,memo1.lines[line]);

End

Printer.enddoc;

Btnbottomdlg.free;

End

End

Procedure Teditform.openprintcanceldialog;

Begin

Btnbottomdlg: = tbtnbottomdlg.create (application);

Btnbottomdlg.show;

BtnBottomDlg.canvas.Brush.Color: = Clactiveborder;

BtnBottomDlg.canvas.TextOut (50,20, ' Print ' +filename);

BtnBottomDlg.canvas.TextOut (30,40, ' If you want to

Stop, please choice Cancel Button.

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.