How to get a dynamic path during runtime? - delphi

I have problems with getting the specific path of an image file that is uploaded to my application during runtime and I would love some help.
Basically I am trying to save a picture to a database, and I can partly achieve that. I am using the following:
procedure TForm12.Button7Click(Sender: TObject);
begin
ADOQuery4.SQL.Text := 'SELECT Profile_Picture, username FROM profile WHERE username='+QuotedStr(edit12.Text);
ADOQuery4.Open();
ADOQuery4.Edit();
TBlobField(ADOQuery4.FieldByName('Profile_Picture')).LoadFromFile('aaa.jpg');
ADOQuery4.Post();
end;
The code selects the correct place I want it to be places in the database, and correctly updates it with the file I have selected (the aaa.jpg file).
But this works only if I program it as such and it cannot be changed during runtime, in other words the user of the application cannot change the file throught the interface unless he opens the whole thing and change the code.
What I am asking is, is it possible to get the path of a file through a command during runtime?
For example, if I wanna save whatever picture is uploaded with an OpenPictureDialog component, how is it possible to do that? To select whatever specific file is selected by the OpenPictureDialog during runtime.

TOpenPictureDialog tells you the full path of the selected file, eg:
procedure TForm12.Button7Click(Sender: TObject);
begin
if OpenPictureDialog1.Execute then
begin
...
TBlobField(ADOQuery4.FieldByName('Profile_Picture')).LoadFromFile(OpenPictureDialog1.FileName);
...
end;
end;

Related

how to get the selected text from acropdf component to an edit directly with Delphi 7

I study preparing a dictionary programme with delphi. So far I have solved my problems about Word documents but I've got some problem about PDF documents.
I imported and installed the AcroPdf component with Delphi 7 and I want to get the word (or text) which was selected by dblclicking by user from pdf document which was viewed by the ACROPDF component in Delphi. If I can get it I'll send it the dictionary database directly.
If you help me I'll be glad. Thank you...
Remzi MAKAK
The following shows one way to get the selected text from a Pdf document which is
open in Adobe Acrobat Professional (v.8, English version).
Update The original version of this answer neglected to check the Boolean result of calling MenuItemExecute and specified the wrong argument to it. Both these points are fixed in the updated version of this answer. It turned out that the reason the call to MenuItemExecute was failing was that it is essential to call BringToFront on the Acrobat document before trying to copy the text selected in to to the clipboard.
Create a new Delphi VCL project.
In D7's IDE go to Projects | Import Type Library, and in the Import Type Library pop-up, scroll down until you see something like "Acrobat (Version 1.0) in the list of files, and
"TAcroApp, TAcroAVDoc..." in the Class names box. That is the one you need to import. Click the Create unit button/
In the project's main form file
a. Make sure it USES the Acrobat_Tlb.Pas unit from step 2. You may need to add the path to wherever you saved Acrobat_Tlb.Pas to the SearchPath of your project.
b. Drop a TButton on the form, name it btnGetSel. Drop a TEdit on the form and name it edSelection
Edit the source code of your main form unit as shown below.
Set a debugger breakpoint on Acrobat.MenuItemExecute('File->Copy'); Do not set a breakpoint within the GetSelection procedure as this is likely to defeat the call to BringToFront in it.
Close any running instance of Adobe Acrobat. Check in Task Manager that there are no hidden instances of it running. The reason for these step is to make sure that when you run your app, it "talks" to the instance of Acrobat that it starts, not another one.
Compile and run your app. Once the app and Acrobat are open, switch to Acrobat, select some text, switch back to your app and click the btnGetSel button.
Code:
uses ... Acrobat_Tlb, ClipBrd;
TDefaultForm = class(TForm)
[...]
private
FFileName: String;
procedure GetSelection;
public
Acrobat : CAcroApp;
PDDoc : CAcroPDDoc;
AVDoc : CAcroAVDoc;
end;
[...]
procedure TDefaultForm.FormCreate(Sender: TObject);
begin
// Adjust the following path to suit your system. My application is
// in a folder on drive D:
FFileName := ExtractfilePath(Application.ExeName) + 'Printed.Pdf';
Acrobat := CoAcroApp.Create;
Acrobat.Show;
AVDoc := CoAcroAVDoc.Create;
AVDoc.Open(FileName, FileName); // := Acrobat.GetAVDoc(0) as CAcroAVDoc; //
PDDoc := AVDoc.GetPDDoc as CAcroPDDoc;
end;
procedure TDefaultForm.btnGetSelClick(Sender: TObject);
begin
GetSelection;
end;
procedure TDefaultForm.GetSelection;
begin
// call this once some text is selected in Acrobat
edSelection.Text := '';
if AVDoc.BringToFront then // NB: This call to BringToFront is essential for the call to MenuItemExecute('Copy') to succeed
Caption := 'BringToFront ok'
else
Caption := 'BringToFront failed';
if Acrobat.MenuItemExecute('Copy') then
Caption := 'Copy ok'
else
Caption := 'BringToFront failed';
Sleep(100); // Normally I would avoid ever calling Sleep in a Delphi
// App's main thread. In this case, it is to allow Acrobat time to transfer the selected
// text to the clipboard before we attempt to read it.
try
edSelection.Text := Clipboard.AsText;
except
end;
end;

How to get the file path of a dropped folder in delphi firemonkey2

Hi as the title says i want to know what code i should use and how i would go about setting up my program so that when i drag a file over a panel or the main form,
It gives me the file path of that folder.
Please Help.
It's as simple as this:
Put a TDropTarget component on the main form.
Set the Filter property. Put * instead of . if you want it to accept folders as well as files.
In the OnDropped event, the list of fully qualified file and or folder names is in Data.Files. You should be able to get everything you need from there.
Is this what you are trying to accomplish, or do you have something else in mind?
Update:
By request, added an example that shows the name of the first file that was dropped.
procedure TForm1.DropTarget1Dropped(Sender: TObject; const Data: TDragObject;
const Point: TPointF);
begin
ShowMessage(Data.Files[0]);
end;

How do I get a list of control-panel items and then execute one?

1. How can I get a list of control panels, including their names and icons, so I can create a menu like the one the Start menu shows?
2. When I click an entry, how do I execute the corresponding control panel?
By the way, what controls are used to do this kind of PopupMenu? But it has right click event.
update :
I use PItemIDList to get a Folder:
var:
PIDL, TempPIDL: PItemIDList;
Path: array[0..MAX_PATH] of Char;
FI: SHFILEINFOW;
begin
SHGetSpecialFolderLocation(0, CSIDL_FAVORITES, PIDL);
SHGetPathFromIDList(PIDL , Path);
Memo1.Lines.Add(Path);
SHGetFileInfo(LPCTSTR(PIDL), 0, FI, SizeOf(FI), SHGFI_PIDL or SHGFI_DISPLAYNAME or SHGFI_ICON);
Memo1.Lines.Add(FI.szDisplayName);
Image1.Picture.Icon.Handle := FI.hIcon;
it display normal , but when I change CSIDL_FAVORITE to CSIDL_CONTROLS , I always get error .
this is a wrong way to get controls panel items ?
I also use another method by use CPL
copy from here
But it can not display complete Items.
You can check the registry for all registered applets.
This describes how to register them: http://msdn.microsoft.com/en-us/library/windows/desktop/bb757044.aspx
Similarly you may scan registry to check already registered applets and their run methods.
However on 64-bit windows there would be 64-bit applets that your 32-bit application would not be able to load, so extracting icon might be a pain. I don't know if you can call LoadLibraryEx with AsResourceLibrary flag over 64-bit DLLs for mere icon extraction though.
Another approach would be using Windows explorer namespaces. Get some Shell component suite that provides opening virtual paths like My Computer and My Documents rather than c:\ and such. Control Panel has a special GUID (that i do not remember right of. But Microsoft TweakUI tool can create Control Panel in any folder using that GUID). You can probably use some Shell UI to open Control Panel special virtual folder into kind of ListView , then get then enumerate items and extract correspondent pictures and re-arrange them as menu. Then executing would be probably done as double-click over item in that shell listview.
Control panel applets are CPL files that are located in your system folder
EG : C:\Windows\system32
My suggestion is to list those files and then extract icons and get their file name
If you have trouble with the code post it here so that we can help
CPL files are just DLL files they can contain multiple applets
After a google search I found this tutorial :
http://delphi.about.com/od/kbwinshell/l/aa062403a.htm
In your help, I solved the problem! Special thanks to David Heffernan
1.Get control panel items I use Windows Shell to get control panel items , use CPL files not get complete items .
Code :
var
psfDeskTop: IShellFolder;
psfControl: IShellFolder;
pidControl: PITEMIDLIST;
pidChild: PITEMIDLIST;
pidAbsolute: PItemIdList;
pEnumList: IEnumIDList;
celtFetched: ULONG;
FileInfo: SHFILEINFOW;
begin
OleCheck(SHGetDesktopFolder(psfDeskTop));
OleCheck(SHGetSpecialFolderLocation(0, CSIDL_CONTROLS, pidControl));
OleCheck(psfDeskTop.BindToObject(pidControl, nil, IID_IShellFolder, psfControl));
OleCheck(psfControl.EnumObjects(0, SHCONTF_NONFOLDERS or SHCONTF_INCLUDEHIDDEN or SHCONTF_FOLDERS, pEnumList));
while pEnumList.Next(1, pidChild, celtFetched) = 0 do
begin
pidAbsolute := ILCombine(pidControl, pidChild);
SHGetFileInfo(LPCTSTR(pidAbsolute), 0, FileInfo, SizeOf(FileInfo), SHGFI_PIDL
or SHGFI_DISPLAYNAME);
// SHGetFileInfo can get name and icon
//Do something to save item name and icon
end;
2. Execute must have to use ShellExecuteEx to execute a PIDL item.
var
ShExeInfo : SHELLEXECUTEINFO;
begin
ZeroMemory(#ShExeInfo, SizeOf(ShExeInfo));
ShExeInfo.cbSize := SizeOf(ShExeInfo);
ShExeInfo.lpVerb := 'Open';
// control panel item's PIDL
ShExeInfo.lpIDList := pidAbsolute;
ShExeInfo.nShow := SW_SHOWNORMAL;
ShExeInfo.fMask := SEE_MASK_IDLIST;
end
and use
ShellExecuteEx(#ShExeInfo);
Finally thanks to David Heffernan again. help me a lot.
Following the suggestion of Arioch 'The
Reference: http://www.geoffchappell.com/studies/windows/shell/shell32/classes/controlpanel.htm
The other "two or three methods" I was thinking of are detailed there:
the [MMCPL] section of the CONTROL.INI file, nowadays mapped to the
registry key HKEY_CURRENT_USER\Control Panel\MMCPL;
the registry key
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Control Panel\CPLs;
Also, detailed there as well are the don't load lists:
A candidate CPL module is rejected if its filename appears as a value
in either of the following registry keys:
HKEY_CURRENT_USER\Control Panel\don't load
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Control Panel\don't load
Like was said, the challenge of this project is sweeping all the proper locations and getting the data in the right way to be able to act on it. This is because of how a few of the "newer" design control panel items (and shell folders that appear there, I'm not really sure I discovered how to access those yet) are presented. I don't have the data handy, but I can copy an example or two in if it would further the discussion.

Rave Reports - How to find which File format user selects during setup?

I am using Delphi 7. I have the reporting mechanism implemented using Rave. Also exporting to HTML, PDF and TXT is used.
There is a problem though: When user saves report into file directly (without previewing it first), file extension is not added to the filename by Rave. So the file created looks like Report (instead of Report.pdf) and user needs to specify application to view this report manualy. In order to solve this problem I wrote the following code to RvSystem1 BeforeRrint event:
procedure TRepAllForm.RvSystem1BeforePrint(Sender: TObject);
begin
if RvSystem1.ReportDest=rdFile then
RVSystem1.OutputFileName:=RVSystem1.OutputFileName+'.pdf';
End;
But the problem is what if user selects different format? Does anybody know how to identify which rendering user selects? i.e. HTML, PDF or TXT?
Thank you,
Tofig Hasanov
Seems that I have found solution myself ) I used RenderObject property of RVSystem.
Here is the code:
procedure TRepAllForm.RvSystem1BeforePrint(Sender: TObject);
begin
if RvSystem1.ReportDest=rdFile then
Begin
if RVSystem1.RenderObject=RvRenderPDF1 then
RvSystem1.OutputFileName:=RvSystem1.OutputFileName+'.pdf'
else if RvSystem1.RenderObject=RvRenderHTML1 then
RvSystem1.OutputFileName:=RvSystem1.OutputFileName+'.html'
else if RvSystem1.RenderObject=RvRenderText1 then
RvSystem1.OutputFileName:=RvSystem1.OutputFileName+'.txt';
End;
end;

Get/Set TShellListView Path/Folder as String (Not Using .Root)

I want to set the path for a TShellListView to display a directory of files using Delphi 2007. I can initially use TShellListView.Root to set the root path like this and it shows the directory I want:
View := TShellListView.Create(Self);
// ...
View.Root := 'C:\Windows';
But if the user navigates away from that directory using backspace and I try to set the .Root back to the original directory, the directory displayed does not change. It looks like .Root is meant to define the root of the shell namespace, not the current directory.
Also, if the user navigates around (using backspace, etc.) the .Root property does not update to reflect the currently displayed path. There is no .Path property like there is for TShellTreeView.
What I want is a way to get and set the current path as a string without being required to link the TShellListView to a TShellTreeView and set TShellTreeView.Path or hack ShellCtrls.pas since the relevant methods of TShellListView all look private. I find it hard to believe there isn't a straightforward way to get/set the path, so I assume I'm missing something simple here, but this component is not documented at all.
You can get the currently loaded path using
ShellListView1.RootFolder.PathName
Setting the Root property works, but it isn't updated when you change folders interactively. So you need to force it to think there's a change. This works if you're always resetting it to the same original path:
ShellListView1.Root := View.RootFolder.PathName; // Updates to current location
ShellListView1.Root := 'C:\Windows';
Alternatively, for arbitrary paths you could just add/remove the trailing \ in order to fool the SameText check in SetRoot:
if ShellListView1.Root[Length(ShellListView1.Root)] = '\' then
ShellListView1.Root := ExcludeTrailingPathDelimiter(ANewPath)
else
ShellListView1.Root := IncludeTrailingPathDelimiter(ANewPath);
To get the current folder as a string, you can access the RootFolder-property.
procedure TForm2.Button1Click(Sender: TObject);
begin
showmessage(ShellListView1.RootFolder.PathName);
end;
To set the current folder as a string, you use the root-property.
procedure TForm2.Button2Click(Sender: TObject);
begin
ShellListView1.Root := 'C:\windows';
end;

Resources