Turn: Write the installer with Delphi

Source: Internet
Author: User

Http://www.okbase.net/doc/details/931 has not personally verified it, only the collection

When you have completed the development of an application software, then you need to do a standardized installation program for the software, which is the final step of the program design, but also a very important step, because running the installer is often the first step of the user to do.
A lot of newspaper articles about how to use InstallShield and other tools to make the installation program, this method can quickly establish a more common installation mode, but there are some shortcomings in this approach, such as the installation process is generally larger; style is relatively single ; Cannot flexibly control the startup mode and shortcut. In fact, the installation of a few steps to run the program is more fixed, we can completely write the installation program ourselves. Here is a detailed description of how to use Delphi to write the installation program with its own software features.
The installation program to achieve a few principles, such as the configuration of a one-time, in the relevant magazine has a number of articles introduced, no longer be described. Here are a few main procedures for programming the installer:
1, the need to install the program to make a resource file
Step 1: Write the myres.rc resource script file, which can be written in Notepad and so on.
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: Generate the resource file Myres.res using BRCC32 compilation.
Brcc32.exe myres.rc
2. Write the installer Code
Step 1: Add {$R mymyres to the SETUP.DPR file. RES} statement so that the file is compiled with a resource file.
Step 2: Write the installer background interface.
The color gradient effect of the interface is achieved by drawing a gradient brush to draw the adjacent rectangular block in turn.
You can also implement other special effects of the installation interface.
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);
Paint the color of the brush for each rectangle (left, top, right, bottom)
Dct:=rect (i*2
0
(i+1) * *
j);
Dct:=rect (0
I*2
J
(i+1);
Each painted rectangular area
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
Setting up the directory environment and configuration files
If not directoryexists (Edtdir.text) Then
If MessageBox (getfocus
' The directory does not exist
Do you want to create the directory? '
Ask
1) = IdOK Then
Try
Forcedirectories (Edtdir.text);
Except
MessageBox (GetFocus
' Failed to create directory! '
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 for 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
To restore a database file from a 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 Master 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;
Writes the main program path name and the execution file name in the registry so that the
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); Place 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); Wait for 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 a main program group
Open a Conversation path
If Ddeclientconv1. OpenLink Then
Begin
Set up a cluster
Sgroupdesc:= ' Ningbo University Courseware Management system ';
Sgroupname:= ' Ningbo University Courseware Management System v1.0 ';
Composing macro directives
Smacro:= ' [CreateGroup (' +sgroupdesc+ ')
' +sgroupname+ ')] ';
Strpcopy (Szmacro
SMACRO); Turn into a null-term string
Ddeclientconv1. Executemacro (Szmacro
FALSE); EXECUTE Macro command
Creating an 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, cut out from my development system, using some of the win API calls. In the installation process of some of the prompt interface and each of the steps you can make according to their own needs, so that the installation process is refined, and the style can be done very special.

Turn: Write the installer with Delphi

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.