frequently asked questions (FAQ)
Having problems with some code? Want to know how to reach your goal? Well, then this might probably the right place for you. Note that at the moment there are very few items here, cos a FAQ can only grow, if people ask questions. So, if you have a question or new ideas, please mail to support@neveprise.de.
Desktop > top of page
Hiding the taskbar
Q: How can I hide or show the windows taskbar?
A: By retrieving the taskbar's handle and using ShowWindow() you can completely hide the bar from Desktop.
function ShowBar(Show: boolean): boolean;
var hWnd: THandle;
begin
hWnd:= FindWindow('Shell_TrayWnd', nil);
if Show = false then
Result:= ShowWindow(hWnd, sw_Hide)
else
Result:= ShowWindow(hWnd, sw_Show);
end;
Registering an own application as taskbar
Q: How can I register my application as a taskbar?
A: Use SHAppBarMessage() to register your form as an AppBar with proper Desktop metrics.
procedure RegisterTaskBarH(FormHandle: THandle; Metrics: TRect; Align: byte; UnRegister: boolean); var AppStruct: TAppBarData; begin // ... fill AppStruct fields, then: SHAppBarMessage(ABM_New, AppStruct); SHAppBarMessage(ABM_SetPos, AppStruct); SHAppBarMessage(ABM_Activate, AppStruct); SHAppBarMessage(ABM_WindowPosChanged, AppStruct); end;
Animating forms and controls
Q: How can I apply window animation fx to my forms?
A: Use DrawAnimatedRects() to create trace/wipe effects on open/close.
procedure ShowWindowH(Show: boolean; Win: TWinControl);
var StartPos, EndPos: TRect; MPos: TPoint;
begin
GetCursorPos(MPos);
if Show = false then begin
with Win do begin
Hide;
StartPos:= Rect(Left, Top, Left+Width, Top+Height);
EndPos:= Rect(MPos.X, MPos.Y, MPos.X, MPos.Y);
DrawAnimatedRects(Handle, 3, StartPos, EndPos);
end;
end else begin
with Win do begin
Hide;
EndPos:= Rect(Left, Top, Left+Width, Top+Height);
StartPos:= Rect(MPos.X, MPos.Y, 0, 0);
DrawAnimatedRects(Handle, 3, StartPos, EndPos);
Show;
end;
end;
end;
Setting screen resolution
Q: How can I set another screen resolution?
A: Use ChangeDisplaySettings() with a TDevMode record specifying width, height, color depth and refresh rate.
Closing windows from other apps
Q: How can I close windows from other applications?
A: Get the window handle via FindWindow() and send WM_Quit:
procedure ExitOtherWindowH(Caption: string);
var WinHandle: THandle;
begin
WinHandle:= FindWindow(nil, PChar(Caption));
if WinHandle <> 0 then
PostMessage(WinHandle, WM_Quit, 0, 0);
end;
Graphics / DirectX > top of page
Creating icons from bitmaps
Q: How can I create icons within my Delphi app?
A: Use CreateIconIndirect() with a color bitmap and a mask bitmap. Helper functions handle color replacement to create the transparency mask.
DirectX: where to get units
Q: Where do I get DirectX conversion units for Delphi?
A: Hori's DelphiX (VCL, good for beginners) or Erik Unger's 1:1 C++ header conversions on Delphi JEDI. Get the DirectX SDK from Microsoft for docs.
DirectX: initializing devices
Q: How to create DirectDraw and Direct3DRM devices?
A: Use DirectDrawCreate, QueryInterface for IDirectDraw2, SetCooperativeLevel, CreateSurface for primary/back/zbuffer, then Direct3DRMCreate and CreateDeviceFromSurface.
DirectX: enumerating display devices
Q: How to enumerate DirectDraw devices?
A: Use DirectDrawEnumerate() with a callback that stores device GUIDs in a TStringList via AddObject().
DirectX: enumerating display modes
Q: How to enumerate video modes?
A: Create a temp DirectDraw2 object and call EnumDisplayModes() with a callback storing width, height, and bit depth.
File related > top of page
Getting a list of all installed drives
Q: How to get a list of all drives?
A: Call GetLogicalDrives() and loop through 26 letters:
procedure GetDrivesH(Drives: TStrings);
var DI: DWord; i: integer;
begin
DI:= GetLogicalDrives;
Drives.BeginUpdate;
for i:= 0 to 25 do
if (DI and (1 shl i)) <> 0 then
Drives.Add(Char(Ord('A') + i) + ':\');
Drives.EndUpdate;
end;
Check if a disk is inserted
Q: How to check if a disk is in a drive?
A: Suppress error dialogs with SetErrorMode(), then try ChDir(). If IoResult <> 0, the drive is not ready.
Delete running executable
Q: How to delete a running executable?
A: Write a command to the Registry RunOnce key. Windows executes it on next boot and removes the entry.
Network / Internet > top of page
Check if computer is online
Q: How to check if the machine is online?
A: Call InternetGetConnectedState() from WinInet.pas.
function IsOnlineH: boolean;
const Internet_Connection_Modem = 1;
Internet_Connection_LAN = 2;
Internet_Connection_Proxy = 4;
var dwConnectionTypes: dWord;
begin
dwConnectionTypes:= Internet_Connection_Modem +
Internet_Connection_LAN + Internet_Connection_Proxy;
Result:= InternetGetConnectedState(@dwConnectionTypes, 0);
end;
Check if connected to LAN
Q: How to check for LAN connection?
A: Use GetSystemMetrics(SM_Network):
function IsNetConnectedH: boolean; begin Result:= (GetSystemMetrics(SM_Network) and $01) = $01; end;
Miscellaneous > top of page
Execute and wait
Q: How to execute an app and wait until finished?
A: Use CreateProcess() with WaitForSingleObject():
function ExecAndWaitH(Filename, Parameter: string;
nCmdShow: integer): longword;
var StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb:= SizeOf(StartupInfo);
StartupInfo.dwFlags:= StartF_UseShowWindow;
StartupInfo.wShowWindow:= nCmdShow;
if not CreateProcess(PChar(Filename), PChar(Parameter),
nil, nil, false,
Create_New_Console or Normal_Priority_Class,
nil, nil, StartupInfo, ProcessInfo)
then Result:= 0
else begin
WaitForSingleObject(ProcessInfo.hProcess, Infinite);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
end;
end;
Detect math coprocessor
Q: How to check for a math coprocessor?
A: Most machines since 80486 have it built in. Check the Registry key HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\FloatingPointProcessor.
Compress data
Q: How to compress files?
A: Use ZLib (included since Delphi 3) which works on a stream base, or download Zip components from the Delphi Super Page.
Exit / reboot Windows
Q: How to shutdown or reboot?
A: Call ExitWindows() or ExitWindowsEx() with flags like ewx_Reboot, ewx_Shutdown, ewx_PowerOff, ewx_Force.
Delay code progress
Q: How to pause code like Delay() in TurboPascal?
A: Use GetTickCount() in a loop:
procedure DelayH(App: TApplication; ms: longint);
var xTime: longint;
begin
xTime:= GetTickCount + ms;
while GetTickCount < xTime do
Application.ProcessMessages;
end;
Monitor suspend mode
Q: How to send the monitor to sleep?
A: Send SC_MonitorPower via SendMessage:
procedure MonitorPowerH(AppHandle: THandle; PowerOn: boolean);
begin
if PowerOn then
SendMessage(AppHandle, WM_SysCommand, SC_MonitorPower, -1)
else
SendMessage(AppHandle, WM_SysCommand, SC_MonitorPower, 0);
end;
-- Blazko
