| Useful Code Snippets |
|
Most of these code snippets will require knowledge programming in Delphi such as creating variables and/or functions. This code snippet shows how the drives and their types can be listed in a TCheckListBox component : Code: var Drive: Char; DriveLetter: String[4]; begin Checklistbox1.Items.Clear; for Drive := 'A' to 'Z' do begin DriveLetter := Drive + ':\'; case GetDriveType(PChar(Drive + ':\')) of DRIVE_REMOVABLE: checklistbox1.Items.Add('Removable Drive '+DriveLetter ); DRIVE_FIXED: checklistbox1.Items.Add('Fixed Drive '+DriveLetter ); DRIVE_REMOTE: checklistbox1.Items.Add('Remote/Network Drive '+DriveLetter ); DRIVE_CDROM: checklistbox1.Items.Add('DVD/CDROM '+DriveLetter ) ; DRIVE_RAMDISK: checklistbox1.Items.Add('RamDisk Drive '+DriveLetter ); end; end; end; This code snippet demonstrates how to refresh the TDriveComboBox component. The first part of this code needs to be defined after the Forms declaration : Code: type TRefreshComboBox = class(TDriveComboBox); Then either use a TButton or create a specific procedure/function to contain this code which refreshes the TDriveComboBox component : Code: var drive : char; begin Drive := DriveComboBox1.Drive; TRefreshComboBox(DriveComboBox1).BuildList; DriveComboBox1.Drive := Drive; end; The reason for the drive variable is that when the TDriveComboBox is refreshed, it does not default to any drive leaving it blank. The drive variable gets the current drive that is displayed and then assigns it back to the TDriveComboBox afterwards. This is following code snippet shows how a window can be closed down. First, a window handle variable needs defining : Code: var closewindow : HWND; Next follows the code that can be entered to close an open window. In this example the code is to close the control panel window: Code: begin Closewindow:=findwindow(nil,'Control Panel'); PostMessage(closewindow, WM_CLOSE, 0, 0) ; end; The findwindow locates the window and returns a window handle to the closewindow variable. Then , using the postmessage function with the WM_CLOSE call, it closes the window. This code snippet gives the program the ability to shutdown , reboot, and power off a PC. The shutdown side of this function , bu default, does not power off the PC whereas the power off call does. It also can log off a user and force an application to close. To force an application to close will cause data loss and so this is a last resort option to use. Here is the code that will shutdown windows but not power off the pc : Code: exitwindowsex(EWX_SHUTDOWN,0); To power off the PC this code will be needed : Code: exitwindowsex(EWX_POWEROFF,0); If a user needs logging off then this will work : Code: exitwindowsex(EWX_LOGOFF,0); To force an application to close, loosing data in the process this code will work : Code: exitwindowsex(EWX_FORCE,0); Finally to reboot a PC this code will work : Code: exitwindowsex(EWX_REBOOT,0); |