473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Using API to set focus to application

Hi,

I'm trying to use SendKeys to control an application, however it keeps losing focus when i call it, cuold anyone tell me how to set the focus to the window using the API?

Cheers

(Visual Basic 2005 Express)
Jul 25 '07 #1
13 17578
Killer42
8,435 Expert 8TB
I'm trying to use SendKeys to control an application, however it keeps losing focus when i call it, cuold anyone tell me how to set the focus to the window using the API? (Visual Basic 2005 Express)
Not sure you need to use the API. I don't know about 2005, but in VB6 there's an AppActivate statement which sets focus to another window. Perhaps you can use that?
Jul 26 '07 #2
chezz
13
I know that vb 6 that if u have a text box or something you can do this

Expand|Select|Wrap|Line Numbers
  1. TextBox.SetFocus
  2.  
This places the focus on the specified object in this case the text box.

It should be something similar to vb 2005
Jul 26 '07 #3
Killer42
8,435 Expert 8TB
I assumed we were talking about setting the focus to another application. I could be wrong, of course.
Jul 26 '07 #4
hariharanmca
1,977 1GB
Hi,

I'm trying to use SendKeys to control an application, however it keeps losing focus when i call it, cuold anyone tell me how to set the focus to the window using the API?

Cheers

(Visual Basic 2005 Express)
can you explain

Why and what exactly you want to do? (if no problem, you can post what you had tried. Because, we are not getting your problem properly).
Jul 26 '07 #5
Thanks for all the replies...

I'm trying to control a second application from my own VB 2005 project... In an ideal world i would like it to happen all in the background.

Basically i create a new instance of an applicaiton, use sendkeys to make it open a new command file, then again to force it to execute the command file, however when it opens it can often lose focus, so i wanted a call (i thought to the api) to ensure that the window is focused.

I originally wrote this in vb for applications and used the appactiviate with success, but unfortunately I can't find a direct replacement for it in 2005.

This is my code to launch and control the application...

Expand|Select|Wrap|Line Numbers
  1.      Private Sub launchApplication()
  2.         Dim Application As Process = New Process()
  3.         Me.WindowState = FormWindowState.Minimized
  4.        Application.StartInfo.FileName = sPath & "\Application.exe"
  5.        Application.StartInfo.WindowStyle = _
  6.            ProcessWindowStyle.Maximized
  7.         Try
  8.             Application.Start()
  9.         Catch ex As Exception
  10.             Me.WindowState = FormWindowState.Normal
  11.             Err.Raise(53)
  12.         End Try
  13.         Application.WaitForInputIdle(4000)
  14.         SendKeys.Send("%F")
  15.         SendKeys.Send("O")
  16.         SendKeys.Flush()
  17.         Application.WaitForInputIdle(100)
  18.         SendKeys.SendWait(sPath & "\" & commandFileName)
  19.         SendKeys.Flush()
  20.         SendKeys.SendWait("~")
  21.         SendKeys.Flush()
  22.         SendKeys.SendWait("%FE")
  23.         Application.WaitForExit(60000)
  24.         If Not Application.HasExited Then
  25.             Application.Kill()
  26.         End If
  27.         Me.WindowState = FormWindowState.Normal
  28.         MsgBox("Done")
  29.     End Sub
  30.  
  31.  
As I said above, in an ideal world I would like the application to never appear (as it requires no user input), my second question is to ask can you launch the application hidden and set the focus to it? (i know u can launch with the startup info set to hidden, just wondering if it is possible to focus to an invisible window)

Thanks for all the help guys!
Jul 26 '07 #6
hariharanmca
1,977 1GB
Thanks for all the replies...

I'm trying to control a second application from my own VB 2005 project... In an ideal world i would like it to happen all in the background.

Basically i create a new instance of an applicaiton, use sendkeys to make it open a new command file, then again to force it to execute the command file, however when it opens it can often lose focus, so i wanted a call (i thought to the api) to ensure that the window is focused.

I originally wrote this in vb for applications and used the appactiviate with success, but unfortunately I can't find a direct replacement for it in 2005.

This is my code to launch and control the application...

Expand|Select|Wrap|Line Numbers
  1.      Private Sub launchApplication()
  2.         Dim Application As Process = New Process()
  3.         Me.WindowState = FormWindowState.Minimized
  4.        Application.StartInfo.FileName = sPath & "\Application.exe"
  5.        Application.StartInfo.WindowStyle = _
  6.            ProcessWindowStyle.Maximized
  7.         Try
  8.             Application.Start()
  9.         Catch ex As Exception
  10.             Me.WindowState = FormWindowState.Normal
  11.             Err.Raise(53)
  12.         End Try
  13.         Application.WaitForInputIdle(4000)
  14.         SendKeys.Send("%F")
  15.         SendKeys.Send("O")
  16.         SendKeys.Flush()
  17.         Application.WaitForInputIdle(100)
  18.         SendKeys.SendWait(sPath & "\" & commandFileName)
  19.         SendKeys.Flush()
  20.         SendKeys.SendWait("~")
  21.         SendKeys.Flush()
  22.         SendKeys.SendWait("%FE")
  23.         Application.WaitForExit(60000)
  24.         If Not Application.HasExited Then
  25.             Application.Kill()
  26.         End If
  27.         Me.WindowState = FormWindowState.Normal
  28.         MsgBox("Done")
  29.     End Sub
  30.  
  31.  
As I said above, in an ideal world I would like the application to never appear (as it requires no user input), my second question is to ask can you launch the application hidden and set the focus to it? (i know u can launch with the startup info set to hidden, just wondering if it is possible to focus to an invisible window)

Thanks for all the help guys!


Hi Martin2007,
1. This will happen if your code does not refer any event handling.
2. If it refers, it won’t be invisible.
3. If the window is accessing in invisible mode then your mouse pointer will not give command to other application.
4. That other application should controlled by your application (That app. should not have event Control).
5. if we consider that it happens, your application will send some key, if you press any key which is going to operate other application.



I am not sure with your code. But just think your code is satisfy my above Query?
Jul 26 '07 #7
Hi Martin2007,
1. This will happen if your code does not refer any event handling.
2. If it refers, it won’t be invisible.
3. If the window is accessing in invisible mode then your mouse pointer will not give command to other application.
4. That other application should controlled by your application (That app. should not have event Control).
5. if we consider that it happens, your application will send some key, if you press any key which is going to operate other application.



I am not sure with your code. But just think your code is satisfy my above Query?
i'm a little new to this, so i'm not sure I understand... but i will try.

my application does not need any event handling from the other application, it just needs to set it running, which i think u say means it can be invisible?

the idea is that the user does not press any keys while the other application is launched, and once it has started to execute the other application will not respond to keypresses anyway. Basically the user presses a button in my application, a message box pops up saying that the other application is running, please wait.. then my application continues on... there is no interface once the other application has been stared.
Jul 26 '07 #8
hariharanmca
1,977 1GB
i'm a little new to this, so i'm not sure I understand... but i will try.

my application does not need any event handling from the other application, it just needs to set it running, which i think u say means it can be invisible?

the idea is that the user does not press any keys while the other application is launched, and once it has started to execute the other application will not respond to keypresses anyway. Basically the user presses a button in my application, a message box pops up saying that the other application is running, please wait.. then my application continues on... there is no interface once the other application has been stared.
the idea is that the user does not press any keys while the other application is launched, and once it has started to execute the other application will not respond to keypresses anyway.
then why you want, your application invisible? (As you told, Other App. will not work (Events), when your App. is started.)

Basically the user presses a button in my application, a message box pops up saying that the other application is running, please wait.. then my application continues on...
If your App. is invisible(User dont know your App. is running). then how can you say other App. is Running(in Message Box).

there is no interface once the other application has been stared.
Then what is the necessary to make all this commands.

Just go through what is your requirement.
I think, you are not aware of what you are going to do.
Jul 26 '07 #9
I dont think you understand me... i will try to be clear.

My application is visible, and allways needs to be...

I need to send commands to a second application to make it do something...

The second application (not my own) needs to be hidden from view, it just needs to run then close (which it will do)...

Basically i need to open the other application, send it a sequency of keystrokes, then leave it alone...

Hope this clarifies...
Jul 26 '07 #10
hariharanmca
1,977 1GB
I dont think you understand me... i will try to be clear.

My application is visible, and allways needs to be...

I need to send commands to a second application to make it do something...

The second application (not my own) needs to be hidden from view, it just needs to run then close (which it will do)...

Basically i need to open the other application, send it a sequency of keystrokes, then leave it alone...

Hope this clarifies...

is your above code is working fine? then you carry On.

As I said above, in an ideal world I would like the application to never appear (as it requires no user input), my second question is to ask can you launch the application hidden and set the focus to it? (i know u can launch with the startup info set to hidden, just wondering if it is possible to focus to an invisible window)
You mention this, there the problem start for us.
Jul 26 '07 #11
Killer42
8,435 Expert 8TB
Martin2007, it looks as though you used the Report button instead of the Reply button. (A mistake I think we've all made from time to time.)

Please send again.
Jul 28 '07 #12
Martin2007, it looks as though you used the Report button instead of the Reply button. (A mistake I think we've all made from time to time.)

Please send again.

Sorry for the report...

The code above does work fine... some of the time... that is the problem which I have been trying to communicate to you.

back to my original question.... can you use the API to force the focus onto an application while it is not visible... that is all i ask... thanks..
Jul 31 '07 #13
Killer42
8,435 Expert 8TB
back to my original question.... can you use the API to force the focus onto an application while it is not visible... that is all i ask... thanks..
My guess would be... minimised, yes. Invisible, probably not.
Don't know, though. This might be a question for the Windows forum, as it's not specific to VB and we don't seem to have the answer.
Jul 31 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alexander | last post by:
I use same assembly in Application and in Web. How can i determine - assembly using in Application or in Web? (Some feautures i cant use in Web)
3
by: nkunapa | last post by:
Hi: I am looking for any ideas/comments/suggestions on how a Windows Service kind of functionality can be acheived from a web application which which will run under IIS or for that matter any...
3
by: Naveen | last post by:
How can we keep track of how many active users are using the application.
1
by: goe | last post by:
Hi, Im my function I need to use focus() and select() based on the "this" keyword. example: onchange="validate(this)" function validate(input) {
0
by: suedasszyy | last post by:
Haiiii.... i'm new for VB.net. can someone help me to solve those question? is it possible to draw a graph such as Sin graph using console application? if not, how can i draw a graph...
0
by: RBM007 | last post by:
Hello, I have created webpages in the atlas standard. To help my users, I automaticaly focus to the first editable control which is in a GridView kind control. Every gridview is placed in a...
5
by: Claudia Fong | last post by:
Hi, I'm using console application to connect to a db and I managed to use the SELECT statement to display records in my DB but I also need to UPDATE some fields in my table but after I run the...
3
by: ramesh.nrk | last post by:
Hi, Is there any way to read the cookies which are in local machine? Can we create cookies using windows Application in the local machine? I am developing a windows application which needs...
5
by: baskarpr | last post by:
Could somebody help me ? I have a left frame and right frame in a page. There is a form in right frame which has number of text box, checkbox, radio button, some static text's. For every input item...
3
by: Oriane | last post by:
Hi there, I would like to open my Asp.Net project as a "Web Application" rather than as a "Web Site" in Visual Studio. But the thing is that I use the System.Web.Profile and the auto-generated...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.