How To Create a Notepad?

Monday, July 25, 2011 0 comments
How To Create a Notepad with all the features like font setup,page setup,printer setup,find,find and replace,file open and save dialog box.

If u wanna create ur own personal notepad u can make it. It's a easy task.

Lets discuss about it
Only Using 200 lines of code you can create ur personal notepad.

Let's have a look at the code
(The code is in Delphi)

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, XMLBrokr, DB, DBClient, MConnect,
Menus, StdCtrls,Printers, FileCtrl, ComCtrls, ExtCtrls, ToolWin;

type
TForm1 = class(TForm)
Memo1: TMemo;
MainMenu1: TMainMenu;
File1: TMenuItem;
Open1: TMenuItem;
Save1: TMenuItem;
Edit1: TMenuItem;
Font1: TMenuItem;
Find1: TMenuItem;
FontDialog1: TFontDialog;
Print1: TMenuItem;
Exit1: TMenuItem;
Find2: TMenuItem;
PageSetup1: TMenuItem;
PrintDialog1: TPrintDialog;
PrinterSetupDialog1: TPrinterSetupDialog;
FindDialog1: TFindDialog;
ReplaceDialog1: TReplaceDialog;
PageSetupDialog1: TPageSetupDialog;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
PrinterSetup1: TMenuItem;
procedure Font1Click(Sender: TObject);
procedure Open1Click(Sender: TObject);
procedure Save1Click(Sender: TObject);
procedure Print1Click(Sender: TObject);
procedure PageSetup1Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure PrinterSetup1Click(Sender: TObject);
procedure Find2Click(Sender: TObject);
procedure Find1Click(Sender: TObject);
procedure FindDialog1Find(Sender: TObject);
procedure ReplaceDialog1Replace(Sender: TObject);
private
{ Private declarations }
FSelPos: integer;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Exit1Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TForm1.Find1Click(Sender: TObject);
begin
ReplaceDialog1.Execute;

end;

procedure TForm1.Find2Click(Sender: TObject);
begin
FSelPos := 0;
FindDialog1.Execute ;

end;



procedure TForm1.FindDialog1Find(Sender: TObject);
var
S : string;
startpos : integer;
begin
with TFindDialog(Sender) do
begin
{If the stored position is 0 this cannot be a find next. }
if FSelPos = 0 then
Options := Options - [frFindNext];

{ Figure out where to start the search and get the corresponding
text from the memo. }
if frfindNext in Options then
begin
{ This is a find next, start after the end of the last found word. }
StartPos := FSelPos + Length(Findtext);
S := Copy(Memo1.Lines.Text, StartPos, MaxInt);
end
else
begin
{ This is a find first, start at the, well, start. }
S := Memo1.Lines.Text;
StartPos := 1;
end;
{ Perform a global case-sensitive search for FindText in S }
FSelPos := Pos(FindText, S);
if FSelPos > 0 then
begin
{ Found something, correct position for the location of the start
of search. }
FSelPos := FSelPos + StartPos - 1;
Memo1.SelStart := FSelPos - 1;
Memo1.SelLength := Length(FindText);
Memo1.SetFocus;
end
else
begin
{ No joy, show a message. }
if frfindNext in Options then
S := Concat('There are no further occurences of "', FindText,
'" in Memo1.')
else
S := Concat('Could not find "', FindText, '" in Memo1.');
MessageDlg(S, mtError, [mbOK], 0);
end;
end;
end;

procedure TForm1.Font1Click(Sender: TObject);
var
dlgFont : TFontDialog;
begin
dlgFont := TFontDialog.Create(Form1);
if dlgFont.Execute then
begin
Memo1.Font.Name := dlgFont.Font.Name;
Memo1.Font.Size:= dlgFont.Font.Size;
Memo1.Font.Style:=dlgFont.Font.Style;
Memo1.Font.Color:=dlgFont.Font.Color;

end;

end;

procedure TForm1.Open1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;


procedure TForm1.PageSetup1Click(Sender: TObject);
begin
if PageSetupDialog1.Execute then
//if PageSetupDialog1.Execute then

end;


procedure TForm1.Print1Click(Sender: TObject);
begin
//PrintDialog1 := TPrintDialog.Create(Form1);
if PrintDialog1.Execute then
// Start printing
Printer.BeginDoc;
end;




procedure TForm1.PrinterSetup1Click(Sender: TObject);
begin
PrinterSetupDialog1.Execute;
end;



procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
var
SelPos, SPos, SLen, TextLength: Integer;
SearchString : string;
begin
with TReplaceDialog(Sender) do begin
TextLength:=Length(Memo1.Lines.Text);

SPos:=Memo1.SelStart;
SLen:=Memo1.SelLength;

SearchString := Copy(Memo1.Lines.Text,
SPos + SLen + 1,
TextLength - SLen + 1);

SelPos := Pos(FindText, SearchString);
if SelPos > 0 then begin
Memo1.SelStart := (SelPos - 1) + (SPos + SLen);
Memo1.SelLength := Length(FindText);

Memo1.SelText := ReplaceText;
end
else MessageDlg('Could not find "' + FindText +
'" in Memo1.', mtError, [mbOk], 0);
end;
end;


procedure TForm1.Save1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
Memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;

end.

0 comments:

Post a Comment

Recent Posts

WebInfo

 

©Copyright 2011 WebInfo