Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to display the filename in the form caption when mousedown clicked 1

Status
Not open for further replies.

guruca

Technical User
Mar 18, 2009
10
PH
How to display the filename in the form caption when mouse down clicked while not opening the file?
 
That would depent on the control-type containing the filename. If it was displayed in an edit box (TEdit) then...
Code:
procedure TForm1.Edit1Click(Sender: TObject);
begin
  Caption:= Edit1.Text
end;

Roo
Delphi Rules!
 
Yes delphi rules, thank you for that. But you got it wrong.

example:
my delphi application is running.

c:\windows\help\access.chm was mouse left down [and is not meant to open]

the filename[access.chm] displays on my delphi form caption.


i hope you got the point.
 
Sorry, you did say when "mouse down", so it would be:
Code:
procedure TForm.Edit1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Caption:= Edit1.Text
end;

Roo
Delphi Rules!
 
My correction was posted before reading your second post. SO, if left button it would be:
Code:
procedure TForm1.Edit1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbLeft then
    Caption:= Edit1.Text
end;
If Edit.Text contains "c:\windows\help\access.chm", the caption will be "c:\windows\help\access.chm".

If Edit.Text contains "access.chm", the caption will be "access.chm".

It would help to understand if we knew HOW the string was displayed.


Roo
Delphi Rules!
 
thank you for that, but i dont need the editbox, it is a windows hook application.

example2:
my delphi application is running.....

i open my drive, [drive c], i go to windows folder, clicked the notepad.exe without opening it.

i switch tab to my delphi app,
in my delphi form caption, it displays "c:\windows\notepad.exe"

i hope you got that point now.
 
guruca, be more specific / clear!
do you have actual working code?

is it the hooking part that is your problem?

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
What You See Is What You Get
Never underestimate tha powah of tha google!


Well I don't see the power of google, I search the whole, nothing came.

If you can answer that with a simple code, you got the power, a delphi power.

Thanks.
 
yes, I understand what you mean, please answer my question, do you have already some code for the hooking part or not?

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Yes I have some part for mouse hook: I found it in torry's delphi page.

Here:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, AppEvnts, StdCtrls;

type
TForm1 = class(TForm)
ApplicationEvents1: TApplicationEvents;
Button_StartJour: TButton;
Button_StopJour: TButton;
ListBox1: TListBox;
procedure ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
procedure Button_StartJourClick(Sender: TObject);
procedure Button_StopJourClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
FHookStarted : Boolean;
public
{ Public declarations }
end;

var
Form1: TForm1;


implementation

{$R *.dfm}

var
JHook: THandle;

// The JournalRecordProc hook procedure is an application-defined or library-defined callback
// function used with the SetWindowsHookEx function.
// The function records messages the system removes from the system message queue.
// A JournalRecordProc hook procedure does not need to live in a dynamic-link library.
// A JournalRecordProc hook procedure can live in the application itself.

// WH_JOURNALPLAYBACK Hook Function

//Syntax

// JournalPlaybackProc(
// nCode: Integer; {a hook code}
// wParam: WPARAM; {this parameter is not used}
// lParam: LPARAM {a pointer to a TEventMsg structure}
// ): LRESULT; {returns a wait time in clock ticks}


function JournalProc(Code, wParam: Integer; var EventStrut: TEventMsg): Integer; stdcall;
var
Char1: PChar;
s: string;
begin
{this is the JournalRecordProc}
Result := CallNextHookEx(JHook, Code, wParam, Longint(@EventStrut));
{the CallNextHookEX is not really needed for journal hook since it it not
really in a hook chain, but it's standard for a Hook}
if Code < 0 then Exit;

{you should cancel operation if you get HC_SYSMODALON}
if Code = HC_SYSMODALON then Exit;
if Code = HC_ACTION then
begin
{
The lParam parameter contains a pointer to a TEventMsg
structure containing information on
the message removed from the system message queue.
}
s := '';

if EventStrut.message = WM_LBUTTONUP then
s := 'Left Mouse UP at X pos ' +
IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);

if EventStrut.message = WM_LBUTTONDOWN then

<<<<<I NEED A COMMAND HERE>>>>>

s := 'Left Mouse Down at X pos ' +
IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);

if EventStrut.message = WM_RBUTTONDOWN then
s := 'Right Mouse Down at X pos ' +
IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);

if (EventStrut.message = WM_RBUTTONUP) then
s := 'Right Mouse Up at X pos ' +
IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);

if (EventStrut.message = WM_MOUSEWHEEL) then
s := 'Mouse Wheel at X pos ' +
IntToStr(EventStrut.paramL) + ' and Y pos ' + IntToStr(EventStrut.paramH);

if (EventStrut.message = WM_MOUSEMOVE) then
s := 'Mouse Position at X:' +
IntToStr(EventStrut.paramL) + ' and Y: ' + IntToStr(EventStrut.paramH);

if s <> '' then
Form1.ListBox1.ItemIndex := Form1.ListBox1.Items.Add(s);
end;
end;

procedure TForm1.Button_StartJourClick(Sender: TObject);
begin
if FHookStarted then
begin
ShowMessage('Mouse is already being Journaled, can not restart');
Exit;
end;
JHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, hInstance, 0);
{SetWindowsHookEx starts the Hook}
if JHook > 0 then
begin
FHookStarted := True;
end
else
ShowMessage('No Journal Hook availible');
end;

procedure TForm1.Button_StopJourClick(Sender: TObject);
begin
FHookStarted := False;
UnhookWindowsHookEx(JHook);
JHook := 0;
end;

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
{the journal hook is automaticly camceled if the Task manager
(Ctrl-Alt-Del) or the Ctrl-Esc keys are pressed, you restart it
when the WM_CANCELJOURNAL is sent to the parent window, Application}
Handled := False;
if (Msg.message = WM_CANCELJOURNAL) and FHookStarted then
JHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, 0, 0);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
{make sure you unhook it if the app closes}
if FHookStarted then
UnhookWindowsHookEx(JHook);
end;

end.


Thanks.
 
How is it going, is it hard? unfortunately the power of google cannot help, where are the so called "delphi geeks"?
Waiting more than the power of google.... the "delphi savants
 
guruca, no reason to be impolite, people are only trying to help here....

anyway, I must admit this is not something you'll find with google, so let's get creative :)

I took following approach:

lookup all explorer windows (there could be more than one)
lookup files listview, get text from selected item in the listview. this sounds easy but unfortunately it aint!

this sample unit will lookup all selected files (code is tested under windows xp, I'm not sure this will work under vista), it is up to you to bind it into your hookapp.

here you go:

to use this unit, create a new VCL forms application, drop a TButton and a TMemo on the form and copy/paste code.

Code:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, CommCtrl,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

function LvGetSelectedItemtext(LvHandle : Thandle) : String;

var Index       : Integer;
    Written     : Cardinal;
    cItemInfo   : TLVITEM;
    pItemInfo   : PLVITEM;
    pString     : PChar;
    Buffer      : array[0..1023] of Char;
    hProcess    : THandle;
    FPid        : Cardinal;

begin
 Result := '';
 GetWindowThreadProcessID(LvHandle, FPid);
 if FPid = 0 then Exit;
 Index := SendMessage(LvHandle, LVM_GETITEMCOUNT, 0, 0);
 ZeroMemory(@cItemInfo, SizeOf(cItemInfo));
 hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, FPid);
 //Alloc mem in the external process
 pItemInfo := VirtualAllocEx(hProcess, nil, SizeOf(cItemInfo), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
 pString := VirtualAllocEx(hProcess, nil, 1024, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
 try
   while Index > 0 do
    begin
     // first look if there are items selected
     Dec(Index);
     if SendMessage(LvHandle, LVM_GETITEMSTATE, Index, LVIS_SELECTED) and LVIS_SELECTED <> 0 then
      begin
       // read selected item
       if Assigned(pItemInfo) then
        begin
         cItemInfo.cchTextMax := 1024;
         cItemInfo.pszText := pString;
         cItemInfo.iSubItem := 0;
         if WriteProcessMemory(hProcess, pItemInfo, @cItemInfo, SizeOf(cItemInfo), Written) then
          begin
           SendMessage(LvHandle, LVM_GETITEMTEXT, Index, Integer(pItemInfo));
           ReadProcessMemory(hProcess, pString, @Buffer, 1024, Written);
           Result := Buffer;
          end;
        end;
      end;
    end;
 finally
  if Assigned(pItemInfo) then
   VirtualFreeEx(hProcess, pItemInfo, 0, MEM_RELEASE);
  if Assigned(pString) then
   VirtualFreeEx(hProcess, pString, 0, MEM_RELEASE);
 end;
end;

function SameClassNameAndWindowName(Hwnd : THandle; ClassName, WindowName : string) : Boolean;

var Str: array[0..255] of char;

begin
 Result := False;
 GetClassName(Hwnd, Str, 255);
 if AnsiSameText(ClassName, Str) then
  begin
   GetWindowText(Hwnd, Str, 255);
   Result := AnsiSameText(WindowName, Str);
  end;
end;

procedure FindExplorerWindows(List : TList);

var hwnd : THandle;

begin
 hwnd := FindWindow('ExploreWClass', nil);
 while hwnd <> 0 do
  begin
   List.Add(Pointer(hwnd));
   hwnd := FindWindowEx(getDesktopWindow, hwnd, 'ExploreWClass', nil);
  end;
end;

function FindChildWindow(ParentHwnd : THandle; ClassName, WindowName : String) : THandle;

var Child : THandle;

begin
 // get all child windows from parent hwnd, filter for Classname and windowname
 Result := 0;
 Child := FindWindowEx(ParentHwnd, 0, nil, nil);
 while (Child <> 0) and (Result = 0) do
  begin
   if SameClassNameAndWindowName(Child, ClassName, WindowName) then
    begin
     Result := Child;
     Break;
    end
   else
    Result := FindChildWindow(Child, ClassName, WindowName);
   if Result = 0 then
    Child := FindWindowEx(ParentHwnd, Child, nil, nil);
  end;
end;

procedure TForm2.Button1Click(Sender: TObject);

var List : TList;
    Index : Integer;
    Hwnd : THandle;

begin
 Memo1.Lines.Clear;
 List := TList.Create;
 try
  FindExplorerWindows(List);
  for Index := 0 to List.Count -1 do
   begin
    Memo1.Lines.Add(Format('Explorer-> 0x%x', [Integer(List[Index])]));
    Hwnd := FindChildWindow(THandle(List[Index]), 'SysListView32', 'FolderView');
    if Hwnd <> 0 then
     begin
      Memo1.Lines.Add(Format('Folderview-> 0x%x - %s', [Hwnd, LvGetSelectedItemtext(Hwnd)]));
     end;
   end;
 finally
  FreeAndNil(List);
 end;
end;

end.

oh, and I'm not a Delphi "geek" or "savant", I'm just a human being [afro]

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Oh!

Great Daddy, I think you've overcome the power of google.

I test the code and it shows me in the right direction.

But

Could you please make it simple, why do I have to click the Button to see the display filename.

What if I click/select the filename and it shows on the form display, just as I want too.


Thanks

Makes Human to machine, You! lol
guruca
 
I search google and I've found this, but its a c++ and it's a .dll, and I don't like to use dll's.


Daddy your at the peak, your a delphi elite.

Could you please make it simple, just as I want too.
And also, I test it on my drive[d]\myfolders and selected a file, and it turns out that I can only see [myfolders] folder name.
 
guruca said:
What if I click/select the filename and it shows on the form display, just as I want too.

just link my code to your hook. the only thing you need to is lookup the hwnd and feed it to my app.

guruca said:
I search google and I've found this, but its a c++ and it's a .dll, and I don't like to use dll's.

Could you please make it simple, just as I want too.
And also, I test it on my drive[d]\myfolders and selected a file, and it turns out that I can only see [myfolders] folder name.

maybe that's also a way to go, write a shell extension, i'm sure will be more fault prone than my quick hack attempt.

I am not going to make a complete solution for you here, I just pointed you in the right direction, the rest is up to you! people like me have other work to do, if you know what I mean!

guruca said:
Daddy your at the peak, your a delphi elite.

thank you's here are expressed with giving stars, click
on the "Thank whosrdaddy for this valuable post!" link to thank me...

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
OK, thank you, I understand, I know where the stars are.

About impolite, I'm not, I'm only joking, no harm.

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top