Solved

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

Posted on 2003-10-24
9
757 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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