Question:
I want to make sure that my application was running only one time. But I dont know that my application already running or not. How?
Answer:
I have an example to detect delphi running or not. Surely you can use this (with little modification at function DelphiLoaded. You should try it.
Place it at form create and if your application detected running, you can close the new one. That way will avoid your application running twice.
function WindowExists(AppWindowName, AppClassName: string): Boolean;
var
hwd: LongWord;
begin
hwd := 0;
hwd := FindWindow(PChar(AppWindowName), PChar(AppClassName));
Result := False;
if not (Hwd = 0) then {window was found if not nil}
Result := True;
end;
function DelphiLoaded: Boolean;
begin
DelphiLoaded := False;
if WindowExists('TPropertyInspector', 'Object Inspector') then
if WindowExists('TMenuBuilder', 'Menu Designer') then
if WindowExists('TAppBuilder', '(AnyName)') then
if WindowExists('TApplication', 'Delphi') then
if WindowExists('TAlignPalette', 'Align') then
DelphiLoaded := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if DelphiLoaded then
begin
ShowMessage('Delphi is running');
end;
end;
function DelphiIsRunning: Boolean;
begin
Result := DebugHook <> 0;
end;
Question:
For evaluation, I would like to have application that shows the list of installed software in my computer. Can you give me?
Answer:
Yup, since those data keep inside registry, we cant avoid to use registry unit.
Here the codes:
uses
Registry;
procedure TForm1.Button1Click(Sender: TObject);
const
UNINST_PATH = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';
var
Reg: TRegistry;
SubKeys: TStringList;
ListItem: TlistItem;
i: integer;
sDisplayName, sUninstallString: string;
begin
{
ListView1.ViewStyle := vsReport;
ListView1.Columns.add;
ListView1.Columns.add;
ListView1.Columns[0].caption := 'DisplayName';
ListView1.Columns[1].caption := 'UninstallString';
ListView1.Columns[0].Width := 300;
ListView1.Columns[1].Width := 300;
}
Reg := TRegistry.Create;
with Reg do
try
with ListView1.Items do
try
BeginUpdate;
Clear;
RootKey := HKEY_LOCAL_MACHINE;
if OpenKeyReadOnly(UNINST_PATH) then
begin
SubKeys := TStringList.Create;
try
GetKeyNames(SubKeys);
CloseKey;
for i := 0 to subKeys.Count - 1 do
if OpenKeyReadOnly(Format('%s\%s', [UNINST_PATH, SubKeys[i]])) then
try
sDisplayName := ReadString('DisplayName');
sUninstallString := ReadString('UninstallString');
if sDisplayName <> '' then
begin
ListItem := Add;
ListItem.Caption := sDisplayName;
ListItem.subitems.Add(sUninstallString);
end;
finally
CloseKey;
end;
finally
SubKeys.Free;
end;
end;
finally
ListView1.AlphaSort;
EndUpdate;
end;
finally
CloseKey;
Free;
end;
end;
Question:
Sometimes, windows moves startmenu's directory while installed big program like DCS of Big Enterprises system. I wish to have application to get real directory of Start Menu's.
Answer:
With ActiveX, we can get real start menu's directory,
please try this code. I wish this answer your question.
uses
ShlObj, ActiveX;
procedure FreePidl(pidl: PItemIDList);
var
allocator: IMalloc;
begin
if Succeeded(SHGetMalloc(allocator)) then
begin
allocator.Free(pidl);
{$IFDEF VER100}
allocator.Release;
{$ENDIF}
end;
end;
function GetStartMenu: string;
var
pidl: PItemIDList;
buf: array[0..MAX_PATH] of Char;
begin
if Succeeded(SHGetSpecialFolderLocation(Form1.Handle, CSIDL_STARTMENU, pidl)) then
SHGetPathFromIDList(pidl, buf);
Result := StrPas(buf);
FreePIDL(pidl);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption := GetStartMenu;
end;
Question:
I want to put CPU speed on my 'About' Form. Can you help me?
Answer:
I will give you systax for getting CPU Speed.
I think your About Form will looks nice with this.
function GetCPUSpeed: Double;
const
DelayTime = 500;
var
TimerHi, TimerLo: DWORD;
PriorityClass, Priority: Integer;
begin
PriorityClass := GetPriorityClass(GetCurrentProcess);
Priority := GetThreadPriority(GetCurrentThread);
SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL);
Sleep(10);
asm
dw 310Fh
mov TimerLo, eax
mov TimerHi, edx
end;
Sleep(DelayTime);
asm
dw 310Fh
sub eax, TimerLo
sbb edx, TimerHi
mov TimerLo, eax
mov TimerHi, edx
end;
SetThreadPriority(GetCurrentThread, Priority);
SetPriorityClass(GetCurrentProcess, PriorityClass);
Result := TimerLo / (1000 * DelayTime);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Format('Your CPU speed: %f MHz', [GetCPUSpeed]));
end;
{Note:
You should call the GetCPUSpeed
function more than
one time to get a good result. }
Question:
It will be nice if I can associate an application to a file extension. I know, windows already has it, but I wish to have more simple and faster application. How?
Answer:
This can only be done with work around in registry,
thanks God, and thanks to borland that delphi has uses to complete this work done.
Here the example.
uses
registry, shlobj;
procedure TForm1.RegisterFileType(prefix: string; exepfad: string);
var
reg: TRegistry;
begin
reg := TRegistry.Create;
try
reg.RootKey := HKEY_CLASSES_ROOT;
reg.OpenKey('.' + prefix, True);
try
reg.Writestring('', prefix + 'file');
finally
reg.CloseKey;
end;
reg.CreateKey(prefix + 'file');
reg.OpenKey(prefix + 'file\DefaultIcon', True);
try
reg.Writestring('', exepfad + ',0');
finally
reg.CloseKey;
end;
reg.OpenKey(prefix + 'file\shell\open\command', True);
try
reg.Writestring('', exepfad + ' "%1"');
finally
reg.CloseKey;
end;
finally
reg.Free;
end;
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RegisterFileType('pci', 'c:\file.exe');
end;
Question:
I want to disable keyboard shortcut for paste!
Answer:
No problem, I have example for disabling CTL-V for TMemo.
Here it is ...
{
Prevent users from pasting text in your Memo.
}
uses Clipbrd;
...
procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if ((Key=Ord('V'))and(ssCtrl in Shift)) then
begin
Clipboard.Clear;
Memo1.SelText:='Delphi tricks rules!';
Key:=0;
end;
end;
Question:
Umm, I want to play windows system sound ....
Answer:
Ok, use this tiny systax.
This example is to play sound of SYSTEMSTART, you may change to other sound.
uses
MMSystem;
PlaySound(PChar('SYSTEMSTART'), 0, SND_ASYNC);
{
Other System sounds:
Andere Systemsounds:
SYSTEMSTART
SYSTEMEXIT
SYSTEMHAND
SYSTEMASTERISK
SYSTEMQUESTION
SYSTEMEXCLAMATION
SYSTEMWELCOME
SYSTEMDEFAULT
}
Question: How to Check if a String is numeric?
Answer:
If you mean string is 1,2,3,4 ....0 in string mode.
Here the code:
function IsStrANumber(const S: string): Boolean;
var
P: PChar;
begin
P := PChar(S);
Result := False;
while P^ <> #0 do
begin
if not (P^ in ['0'..'9']) then Exit;
Inc(P);
end;
Result := True;
end;
Question:
Is it possible to bouild my own swf to exe converter with delphi?
Answer:
Since delphi is the greatest programming language,
anything is possible, include make simple swf converter.
Here the code. Its top secret.
function Swf2Exe(S, D, F: string): string;
var
SourceStream, DestinyStream, LinkStream: TFileStream;
flag: Cardinal;
SwfFileSize: Integer;
begin
Result := 'something error';
DestinyStream := TFileStream.Create(D, fmCreate);
try
LinkStream := TFileStream.Create(F, fmOpenRead or fmShareExclusive);
try
DestinyStream.CopyFrom(LinkStream, 0);
finally
LinkStream.Free;
end;
SourceStream := TFileStream.Create(S, fmOpenRead or fmShareExclusive);
try
DestinyStream.CopyFrom(SourceStream, 0);
flag := $FA123456;
DestinyStream.WriteBuffer(flag, SizeOf(Integer));
SwfFileSize := SourceStream.Size;
DestinyStream.WriteBuffer(SwfFileSize, SizeOf(Integer));
Result := '';
finally
SourceStream.Free;
end;
finally
DestinyStream.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Swf2Exe('c:\somefile.swf', 'c:\somefile.exe',
'c:\Program Files\Macromedia\Flash MX\Players\SAFlashPlayer.exe');
end;
Question:
What types of FoxPro indexes are not supported by the BDE?
When trying to open some tables, I get an "Invalid Index
Descriptor" error.
Answer:
This error occurs when the production index (.CDX) associated
the table has an index tag which has an expression which the BDE
cannot evaluate. The solution is to delete the tag using FoxPro
create an eqivalent index that the BDE can understand.
The following conditions are not supported by the BDE and will
cause "Invalid Index Descriptor" error.
DTOC(, 1) format not supported; Use DTOC().
ALLTRIM function not supported; Use LTRIM(RTRIM(Field)).





