Examples Delphi

Title: WINDOWS UNTAPPED RESOURCES
Question: Programming applications for Windows can be like playing with Lego. You have an abundance of various bricks or building blocks. Even a very intricate plan can be accomplished just by arranging them, experimenting and having fun. Of course there is no need to reinvent the wheel every time. But in this pursuit the curiosity and energy of a discoverer can be quite handy. COM objects in Windows offer plenty of built-in, ready and easy to use solutions. Often they are hidden, undocumented and unexplored. You have to find for yourself when, where and what exactly of this Lego treasure to implement.
Answer:
THRIFTY SCANNING
You can buy, or try to write yourself, a special component to add an image scanning ability to your application. But you don't really need to do this: you already have everything available in Windows. Its name is Imaging for Windows and it ships as part of the operating system. The setup program for Imaging installs the The Image Scan control (OCX) and the 32-bit TWAIN DLLs, and makes the necessary entries in the Windows Registry, so you are ready to work with TWAIN compliant scanners.
Once you have the Imaging installed, importing its capabilities to your Delphi application is a straightforward task. The most efficient way is by importing the type library and, in Delphi 5, generating a component wrapper as shown on Figure 1:
Figure 1: Importing the Imaging type libraries in Delphi 5.
If you just need to scan an image and to save it to a file, the Image Scan Control is enough. But you could also import the Kodak Image Edit Control, which allows you to present and to edit the scanned image onto your application's form and the Thumbnail control, so that you can command the full functionality of Imaging from your Delphi application. When distributing your image-enabled publications,all you need is to make sure that your end users set up Imaging for Windows before installing your applications.

The next code shows how easy it is to access the Imaging controls' interfaces using the component wrapping in Delphi 5:
{ Imgscan: TImgScan}
If Imgscan.ScannerAvailable then //checks if there is a scanner
try
Imgscan.OpenScanner ;
ImgScan.ScanTo := 2; //file
Imgscan.Image:=stFName; //the scanned image will be saved
to this destination
Imgscan.StartScan ;
Application.ProcessMessages;
finally
Imgscan.CloseScanner ;
end
else
exit;
More information about the properties, methods and events of the Imaging controls is available in the imgocxd.hlp help file which you will hopefully find in your OS help directory or on the Kodak Imaging site.
AUTOMATING WINDOWS SCRIPT
Automating another built-in Windows utility - the Microsoft Windows Script Host can sound paradoxical, but in Delphi it can save much time in accomplishing such tasks as creating a shortcut, mapping a network drive or accessing network information. If you like extravagance (or don't like the Windows API) you can even use it to execute another program from your application. Trough its object model Windows Script Host gives easy access to the network or to Windows shell, something like a back entrance for the curious developer. This tool is hidden in the OS directory and the documentation or a new version can be loaded from microsoft.com.
In Delphi you can approach both the early and the late binding to access it. If you prefer to import the library, search for Winscript.exe (first it should be added to the Import Type Library list) and you will have 2 new components. I find it even easier to use the late binding in this case.
Using the Shell object, you can easily create a shortcut or an internet shortcut , to change its icon or to define its target. Accessing the Shell SpecialFolders collection, the shortcut can be placed onto the desktop, in the Start Menu, in "My Documents" folder. All this can be achieved with a few simple lines of code.
The next example creates a shortcut and places it onto the desktop:
procedure CreateShortcut( sName,sPath:string);
var
objShortcut, objShell :variant;
strDesk:string;
begin
objShell:= CreateOleObject('Wscript.Shell');
strDesk:= objShell.SpecialFolders.Item('Desktop');
objShortcut:= objShell.CreateShortcut(strDesk +'\'+ sName +'.lnk');
objShortcut.Targetpath:= sPath;
objShortcut.Save;
objShell:=UnAssigned;
objShortcut:=UnAssigned;
end;
Using the same CreateShortcut method, but with '.url' extension forms an Internet shortcut. If you use a 'mailto:: ' instead of a 'http://:' target, you will have a shortcut to a new email message for a constant receiver. You can also add '&subject=' and '&body=' in the target string and thus create an email template for Outlook. The Run method of the Shell object executes any given executable, for instance 'Shell.Run('Calc') is enough to execute the Calculator. Shell also has a Popup method which shows a message on the screen.
The second important Windows Scripting Host object is Network. The next code uses it to obtain network information:
function ShowNetwork:string;
var
ObjNetwork :variant;
begin
ObjNetwork:= CreateOleObject('Wscript.Network');
Result:= 'User Domain : ' + ObjNetwork.UserDomain + #10 +#13 + 'User Name :
' +
ObjNetwork.UserName + #10 +#13 + 'ComputerName : ' +
ObjNetwork.ComputerName ;
ObjNetwork:=UnAssigned;
end;
Scanning and creating shortcuts from Delphi application are only 2 examples of Windows untapped resources. Exploring your OS you can discover many other ways of completing your tasks simply by playing with ready for use COM blocks. Considering this and the new component wrapping option of Delphi 5 one can say that tens or even hundreds of new Delphi components are already waiting for you in your own PC: to be found, explored and used.
Lubomir Rosenstein is a Delphi developer, specialising in database application programming. He has also written articles on computer programming on the Internet. He is currently working for the NTR Company in Tel Aviv, Israel, and can be reached on lubo@ntr.co.il