June 25th, 2012, 09:17 AM
Backward button to go back to form from opened .exe program.
I am currently working on a program that has several buttons in it lined up. Each button goes to different applications (.exe programs). The program I am making is suppose to be a fullscreen program and won't let you see any of Windows when it's running. Therefore I want to make a "back" button that always stays on top of any application and when I push it it will take me back to my WindowsForm (startpage of the program). Any ideas?
I am also wondering if you can make it so all .exe programs only can run ONCE. So if I press one of the buttons saying "open notepad" I want it to work so it can't open Notepad once again making it two notepads open. Just go back to the first Notepad opened. But if I close notepad I wanna be able to open it again trough one of my buttons.
Thank you for your time! // Kevin.
June 25th, 2012, 11:27 AM
You're going to need a lot of API to pull off all your requirements. I frankly don't know where to begin telling you to begin, except suggesting that a lower level language (C based) would be the better tool for this job.
“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
June 25th, 2012, 12:35 PM
I am not sure what you mean by the first part, but to ensure that an application only starts once, I use the following:
Code above is for one form (frmTest) and one module (module1). Notepad is not a very good example, because each Notepad window can have a different title depending on the file it is editing. But the example above will activate an "Untitled - Notepad" window and switch focus to it. If it already exits, focus is switched to it. If it is minimized, it is restored.
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Declare Function GetForegroundWindow Lib "user32" () As Long
Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Private Sub cmdNotePad_Click()
Dim Title$, A$
Dim TaskID As Long
Dim ErrorCode As Long
On Error GoTo NotePadActErr
Title$ = "Untitled - Notepad"
TaskID = CheckUnique(Title$, 0)
If TaskID = 0 Then
' FlashBox.Show 0
Screen.MousePointer = 11
A$ = "\windows\system32" + "\NOTEPAD.EXE"
If Len(Command$) > 0 Then
A$ = A$ + " " + Command$
TaskID = Shell(A$, 1)
Screen.MousePointer = 0
ElseIf TaskID < 0 Then
MsgBox "Program " + Title$ + "is active but not able to take focus.", 64
ErrorCode = Err
If ErrorCode = 53 Then
MsgBox "NotePad.exe Program could not be found!", 16
Screen.MousePointer = 0
' Unload FlashBox
' Call LogError("^" + Str$(Err) + " Activate Acct")
' Call FatalError(ErrorCode)
Function CheckUnique(ByVal FormName As String, hIgnore As Long) As Long
'FormName is the caption of the desired form, hIgnore is the
'window handle of the parent form to be ignored if already running.
Dim hWnd As Long
Dim ShowW As Long, SetF As Long, Pid As Long
CheckUnique = 0
hWnd = FindWindow(vbNullString, FormName)
If hWnd = 0 Then
ShowW = ShowWindow(hWnd, 9) 'Restore it in case it is minimized
ShowW = hWnd 'Save original handle
SetF = GetForegroundWindow() 'Does not always return current app
If hIgnore = frmTest.hWnd Then
SetF = SetForegroundWindow(hWnd)
SetF = GetCurrentProcessId()
SetF = WaitForInputIdle(SetF, 10000)
hWnd = GetForegroundWindow()
hWnd = SetF
SetF = GetOwnedWindow(hWnd) 'Get owned top level window
If SetF = 0 Then 'No owned Windows found
CheckUnique = hWnd
ShowW = SetForegroundWindow(ShowW) 'SetFocus to FormName
CheckUnique = -SetF 'return neg handle for owned window
SetF = GetWindowThreadProcessId(SetF, Pid)
SetF = AttachThreadInput(SetF, GetCurrentThreadId(), Pid)
Private Function GetOwnedWindow(hWnd As Long) As Long
Dim OwnedHandle As Long
OwnedHandle = GetDesktopWindow() 'get the desktop handle
OwnedHandle = GetWindow(OwnedHandle, 5) ' get first top level window
If GetParent(OwnedHandle) = hWnd Then
GetOwnedWindow = OwnedHandle
OwnedHandle = GetWindow(OwnedHandle, 2) 'get next top level window
Loop Until OwnedHandle = 0
October 10th, 2012, 04:32 AM
For a non internal windows program
Ok! Thx man this is great really. But how would you do this example if there was a program called: trombmobil.exe and was located in C://Program Files/Tromb 3.1/trombmobil.exe
Originally Posted by couttsj
This also a program that I only want to be able to open once, I should have this fixed, but there is something I am doing wrong, any help would be appreciated. Thx in advance.
October 10th, 2012, 10:36 AM
The name of the executable doesn't enter into it; it is the name in the title bar or form Caption that is used to determine if a particular window is loaded or not.
Originally Posted by Kevax
October 12th, 2012, 04:28 AM
Ok, now I get it! Thank you so much for your time
Originally Posted by couttsj