After you complete the development of an application software, you also need to make a standardized installer for the software. This is the last step in programming and an important step, because running the installer is often the first step for users to do. Step operation.
Many newspaper articles introduce many methods of using installshield and other tool software to create installation programs. This method can quickly establish a common installation mode, but there are some shortcomings in using this method, such as the installation program made is generally more Large; relatively single style; cannot flexibly control startup methods and shortcuts. In fact, the several running steps of the installer are relatively fixed, so we can write the installer ourselves. The following is a detailed introduction to how to use DELPHI to write an installation program with its own software characteristics.
Several principles that installers need to achieve, such as one-time configuration, have been introduced in related magazines, so I will not be described in detail. Here we mainly introduce several main processes in the writing of the installer:
1. Make the program that needs to be installed into resource files
Step 1: Write the myres.rc resource script file, which can be written by Notepad, etc.
MYDBSRC mydbsrcfile c:/resdemo/mynbdb.mdb
MYDBJET mydbjetfile c:/resdemo/mdac_typ.exe
MYDBAPP mydbappfile c:/resdemo/Tnbdemo.exe
7001 Icon c:/resdemo/setup.ico
Step 2: Use BRCC32 to compile and generate resource file myres.res.
brcc32.exe myres.rc
2. Write the installation program code
Step 1: Add the {$R mymyres.RES} statement to the setup.dpr file, so that the resource file is included when compiling the file.
Step 2: Write the background interface for installing the program.
//The interface color gradient effect is achieved by brushing adjacent rectangular blocks in sequence with gradient brushes.
//The installation interface with other special effects can also be realized.
var i,j:Integer;
Dct:TRect;
Begin
form1.WindowState:=wsMaximized;
j:=form1.width;
//Get form height
for i:=0 to 255 do
// Set a color value in RGB() here
Begin
Canvas.Brush.Color:=RGB(0,0,255-i);
//The brush color of the rectangle is drawn every time (left, top, right, bottom)
//Dct:=Rect(i*2,0,(i+1)*2,j);
Dct:=Rect(0,i*2,j,(i+1)*3);
//Rectangular area for each brushing
Canvas.FillRect(Dct);
Form1.Canvas.TextRect(Dct,30,40,''Ningbo University Courseware Management System v1.0'');
//Fill color
end;
end;
Step 3: Create the main program working directory
//Set directory environment and configuration files
if not DirectoryExists(EdtDir.Text) then
// if MessageBox(getfocus,''This directory does not exist, is it created?'','',''Question'',1) = idOK then
try
ForceDirectories(EdtDir.Text);
except
MessageBox(getFocus,''Create directory failed!'',''Information'',0);
Exit;
end
// else Exit;
// ModalResult := mrOK;
Step 4: Generate the main program configuration file
variable:
var
setupinfo : TiniFile;
//Configure the current data working path and select the driver related to your system.
setupinfo:= Tinifile.Create( trim(FrmPathSelect.EdtDir.Text)+''/setup.ini'');
setupinfo.WriteString(''access'',''workdbpath'',trim(FrmPathSelect.EdtDir.Text)+''/mynbdb.mdb'';
setupinfo.WriteString(''access'',''backdbpath'',trim(FrmPathSelect.EdtDir.Text)+''/mynbdbbak.mdb'');
setupinfo.Free;
Step 5: Restore the relevant files from the resource file.
variable:
var
res:tresourcestream;
resname,resnewname:string;
Regfile:TRegIniFile;
Begin
//Restore database file from resource file
resnewname:=trim(FrmPathSelect.EdtDir.Text)+''/mynbdb.mdb'';
resname:=''MYDBSRC'';
res:=tresourcestream.Create(hinstance,resname,pchar(''mydbsrcfile''));
res.savetofile(resnewname);
res.free;
//Restore the main program file from the resource file
resnewname:=trim(FrmPathSelect.EdtDir.Text)+''/Tnbdemo.exe'';
resname:=''MYDBAPP'';
res:=tresourcestream.Create(hinstance,resname,pchar(''mydbappfile''));
res.savetofile(resnewname);
res.free;
//Write the main program path name and execution file name information in the registry so that
//Used when upgrading the main program.
RegFile := TRegIniFile.Create;
RegFile.RootKey := HKEY_LOCAL_MACHINE;
RegFile.WriteString(''SOFTWARE/mynb'',''exepath'',trim(resnewname));
RegFile.Free;
end
Step 6: Install the database driver (corresponding to SETP 4)
//Installing the database driver
var
cmdlinepchar:array[0..120] of char;
startupinfo:tstartupinfo;
processinfo:tprocessinformation;
Begin
screen.cursor:=crhourglass;
strpcopy(cmdlinepchar,trim(FrmPathSelect.EdtDir.Text)+''/mdac_typ.exe /q'');
fillchar(startupinfo,sizeof(startupinfo),#0); //set 0
with startupinfo do
Begin
cb:=sizeof(startupinfo);
dwflags:=startf_useshowwindow or startf_usestdhandles;
wshowwindow:=sw_hide; //Hide the called program window
end;
if createprocess(nil,cmdlinepchar,nil,nil,true,0,nil,nil,startupinfo,processinfo) then
//Create a process
Begin
waitforsingleobject(processinfo.HProcess,infinite); //Waiting for the process to end
end
else
Begin
exit;
end;
screen.cursor:=crDEFAULT;
DeleteFile(PChar(trim(FrmPathSelect.EdtDir.Text)+''/mdac_typ.exe''));
end;
Step 7: Create a program group
variable:
var
smacro:string;
szmacro:array[0..254] of char;
sgroupdesc,sgroupname:string;
sprogdesc:string;
sprogicon:string;
//Create the main program group
//Open the dialogue channel
if ddeclientconv1.OpenLink then
Begin
//Create a group
sgroupdesc:=''Ningbo University Courseware Management System'';
sgroupname:=''Ningbo University Courseware Management System v1.0'';
//Make a macro
smacro:=''[creategroup(''+sgroupdesc+'',''+sgroupname+'')]'';
strpcopy(szmacro,smacro); //Convert to null-term string
ddeclientconv1.ExecuteMacro(szmacro,false); //Execute macro command
//Create ICON image
sprogicon:=trim(FrmPathSelect.EdtDir.Text)+''/Tnbdemo.exe'';;
sprogdesc:=''Ningbo University Courseware Management System'';
smacro:=''[additem(''+sprogicon+'',''+sprogdesc+'')]'';
strpcopy(szmacro,smacro);
ddeclientconv1.ExecuteMacro(szmacro,false);
ddeclientconv1.CloseLink;
end;
Step 8: Create a desktop shortcut
//uses shlobj,comobj,activex;
var
tmpObject:IUnknown;
tmpSLink:IShellLink;
tmpPfile:IPersistFile;
PIDL:PItemIDList;
StartupDirectory : array[0..MAX_PATH] of Char;
StartupFilename : String;
LinkFilename : WideString;
c1:THandle;
Begin
StartupFilename := ''''Tnbdemo.exe''''';
tmpObject := CreateComObject(CLSID_ShellLink);
tmpSLink := tmpObject as IShellLink;
tmpPFile := tmpObject as IPersistfile;
tmpSLink.SetPath(pChar(StartupFilename));
tmpSLink.SetWorkingDirectory(pChar(ExtractFilePath(StartupFilename)));
SHGetSpecialFolderLocation(0,CSIDL_DESKTOPDIRECTORY,PIDL);
SHGetPathFromIDList(PIDL, StartupDirectory);
LinkFilename := StartupDirectory + ''/mynotepad.lnk'';
showmessage(linkfilename);
tmpPFile.Save(pWChar(LinkFilename),FALSE);
c1:=windows.FindWindowEx(windows.FindWindowEx(windows.FindWindow(''Progman'',''Program Manager''),0,''SHELLDLL_DefView'','''''),0,''SysListView32'' ,''''');
PostMessage(c1,WM_KEYDOWN,VK_F5,0);
PostMessage(c1,WM_KEYUP,VK_F5,1 shl 31);
end;
Step 9: Restart the machine
Begin
ExitWindowsEx(2,0);
end;
The above is the main statement part of the program, which was cut out from my development system and used some WIN API calls. During the installation process, you can make some prompt interfaces and each step according to your needs. The installation program is refined and the style can be very special.