Solved

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

Posted on 2003-10-24
9
753 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

16 Experts available now in Live!

Get 1:1 Help Now