Solved

Send  Focus to anywhere control in other application by "label" in control

Posted on 2003-10-24
9
754 Views
Last Modified: 2007-12-19
Hello Experts,

I'm making an application that send mouse events and text to edit controls etc., to other applications but my problem it's that before send focus to anywhere control in the other application I need know the position of this control then the resolution is a problem before the position of the controls change.

In this days I'm using "Dragon NaturallySpeaking 5.0" , then when I run Dislplay Properties by default the initial Tab is "BackGround" then I say "Configuration" and "Dragon NaturallySpeaking" send the focus to the tab with label "Configuration" then I say "Colors" then send focus to control with label "Colors".

My cuestion is: how to send focus to anywhere control with label "anywherelabel" in other application? , please help me.

Thanks in advance.
0
Comment
Question by:MauricioGaviria
  • 8
9 Comments
 
LVL 2

Expert Comment

by:alsantos
ID: 9618180
You can use this code:

var
  T: HWND;
begin
  T := FindWindow('TForm1', nil); // TForm1 is the name of the window, you can use WinSight32 to detect it ( Winsight32 come with Delphi )
  T:=FindWindowex(T,0,'TEdit',nil); // TEdit is the name of the Edit that you want to set focus, you TLabel , of any name.
  if T<>0 then
    SendMessage(T, WM_SETFOCUS, 0, 0); // this code to send focus to it.
end;

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9618209
sorry, it don't bring to front the application with window name TForm1, it only set the focus to the edit . Put this code below to set focus to Edit and put the appplication to front.

procedure TForm1.Button1Click(Sender: TObject);
var
  T: HWND;
begin
  T := FindWindow('TfrmRG', nil); // TForm1 is the name of the window, you can use WinSight32 to detect it ( Winsight32 come with Delphi )
  SetForegroundWindow(T); // will put the application to the first ( front )
  T:=FindWindowex(T,0,'TEdit',nil); // TEdit is the name of the Edit that you want to set focus, you TLabel , of any name.
  if T<>0 then
    SendMessage(T, WM_SETFOCUS, 0, 0); // this code to send focus to it.
end;

alsantos
0
 
LVL 2

Accepted Solution

by:
alsantos earned 500 total points
ID: 9618216
I put TfrmRG because my test application here use the window name TfrmRG , You can use TForm or whatever window name you want.
0
 

Author Comment

by:MauricioGaviria
ID: 9628448
Hello alsantos,

I use your code , but in function "FindWindow" the parameters are FindWindow(nil,'TfrmRG')  else the result = 0 , in the function "FindWindowex(T,0,'TEdit',nil)"
 I don't understand if  'TEdit' is the "Text" in the control or I need know the name of the control, I test this with one app  that contain two controls  Button1 with caption "The Button1" and Button2 "default focus" with Caption "Second Button "  then I run this, immediately run other app with your code, then I replace the name of the Window
        T := FindWindow(nil,'TfrmRG');
        T > 0 (Windows found),
and replace the 'TEdit' with 'Button1' but T=0, then replace 'Button1' by ''The Button1'' (Caption) but the result = 0 .

Please, tellme what's wrong.

Thanks in advance.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:alsantos
ID: 9629031
You don't have to use TfrmRG, use TForm1 or whatever is the name of your form. For example, if your form has the name MauricioGaviria, you use FindWindow('TMauricioGaviria', nil); is TNameOfYourForm that you set on the property editor, by default, the name is Form1, so you use FindWindow('TForm1', nil);

So, you found the HWND of the Main Form. Now, you have to find the HWND of the Button, so use T:=FindWindowEx(T, 0, 'TButton', nil); and It will return the HWND of the Button. Now you can setfocus to button. If it isn't the button that you want, you want the next button, you have to use T:=GetWindow(T, GW_HWNDNEXT); and it will return the HWND of the next button. So, I put below, an example:


procedure TForm1.Button1Click(Sender: TObject);
var
  T: HWND;
begin
  T := FindWindow('TForm1', nil); // TForm1 , Form1 is the default name of form, if you change the name of your form, you put here TNameofYourForm.
  SetForegroundWindow(T);
  T:=FindWindowex(T,0,'TButton',nil); // get the HWND of the first button on your Form1
  T:=GetWindow(T, GW_HWNDNEXT); // get the HWND of the next button.
  if T<>0 then
    SendMessage(T, WM_SETFOCUS, 0, 0);
end;

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629086
You tell me that you use FindWindow(nil,'TfrmRG') , if you use FindWindow(1TForm1', nil); you are looking for the class name. If you use FindWindow(nil, 'TfrmRG'); you are looking for a window with title TfrmRG. You choose the way you want.
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629098
the same to FindWindowEx, you can use FindWindowEx(T, 0, 'TButton', nil); you will look for the First object of class TButton or you can look for the object with name Button1 using FindWindowEx(T, 0, nil, 'Button1');

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629154
one more thing... MauricioGaviria , you are a brazilian guy, aren't you? :) brasileiro...  ?

alsantos
0
 
LVL 2

Expert Comment

by:alsantos
ID: 9629487
and look.... name and caption is 2 things different. When you rename the name of Form from Form1 to SomeThing, you rename the class name of this form too. It will show on global var of the form like this:

var
  SomeThing: TSomeThing;

so... If you would like to find the window by the classname, you have to look for TSomeThing , FindWindow('TSomeThing', nil) . or use the caption to find the HWND, FindWindow(nil, 'CaptionNameOfTheForm');

but, when you rename the Name of a button, for example, you change only the name of this button, but the classname is the same: TButton , the same thing to Edit, Label....

I Think that you already understood that.

alsantos
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to manage invalidate between two tvirtualstringtree in same form? 1 104
Delphi 2 48
LAN or WAN ? 11 81
Installshield for Embarcadero EX 10.1 Berlin 4 18
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now