# Stop A Program From Losing Focus



## Focus2 (Nov 30, 2011)

I have a problem with my computer losing focus on a certain screen. So im trying make in program that always running, in if the SCREEN lose focus an a program will bring it back up automatic. Are prevent it fom losong focus.

Thanks


----------



## Special2God (Oct 5, 2009)

First question, what language are you trying to program this in? 
What OS are you working with? 
You can google "Windows keep window focus" for various answers in C#, C++, etc... 

Open task manager (right click on taskbar > task manager), go to options and select "always on top", watch the resulting behavior of the task manager window, is that the result you want?


----------



## Focus2 (Nov 30, 2011)

I working with vb, in I looking for something simple.


----------



## Special2God (Oct 5, 2009)

Try reading this microsoft page about creating forms that are always on top of other windows.


----------



## Focus2 (Nov 30, 2011)

Thanks, but I couldnt get it to work. I have two program that will be running at the same time. But for some reason, the program thats minimize keep pop over the program thats needs to always be on display. 

I have in code below, can some help me, on what Im doing wrong to focus on a process in task manager. In order for the program can stay in display.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim objWMIService, colProcesses
Dim Process As String
　
Process = "OUTLOOK.exe"
If Process = "OUTLOOK.exe" Then
Process = Focus() 
' MessageBox.Show("test")
Else
End If
objWMIService = GetObject("winmgmts:")
colProcesses = objWMIService.ExecQuery("Select * from Win32_Process where name='" & Process & "'")
If colProcesses.Count Then
colProcesses = vbMaximizedFocus
MsgBox("Process is Running")
Else
MsgBox("Process is Not Running")
End If
If Process = "OUTLOOK.EXE" Then
End If
End Sub
End Class


----------



## Special2God (Oct 5, 2009)

Since I'm not familiar with VB, I would recommend following the "step-by-step example" in the "more information" section of the link I posted and setup a *new project* and copy-paste the example code into the new project. 

Get an exact working copy of the example code in the new project and then try to copy the necessary parts of the example into your project.


----------



## TLF_KoBE (Jan 23, 2012)

Here is an example for you in C# it should be easy enough to convert to VB.Net:
pinvoke.net: SetForegroundWindow (user32)


----------



## Focus2 (Nov 30, 2011)

Thanks for you help everyone, But i have finally figure it out. Such a small code, but it gets the job done. Im posting the code, just in case someone may need it oneday..

Happy Coding:whistling:


Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = Format(TimeOfDay, "h:mm:ss tt")
AppActivate("Today Test.txt")
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Enabled = True
End Sub
End Class


----------



## Special2God (Oct 5, 2009)

Thanks for telling us Focus2, glad you were able to solve the problem. 
Happy coding :wave:


----------

