Skip to content
⌘ NSIS Forum Archive

Find windows focus and change focus

13 posts

r2du-soft#

Find windows focus and change focus

hi
how can find windows focus on which application?!
for example i want check if windows focus on notepad.exe then i change focus to cmd.exe,is it is possible?
Anders#
No it is not really possible. All newer versions of Windows have something called the foreground lock so only the active application is allowed to change the focus. This restriction exists to prevent other applications from changing the focus just like you are trying to do because it is very annoying for the user!
r2du-soft#
Originally Posted by Anders View Post
No it is not really possible. All newer versions of Windows have something called the foreground lock so only the active application is allowed to change the focus. This restriction exists to prevent other applications from changing the focus just like you are trying to do because it is very annoying for the user!

thanks for helping foreground code master
foreground code is not very bad:

Focus on notepad:
FindWindow $0 "notepad" ""
IsWindow $0 0 +2
System::Call 'user32::SetForegroundWindow(i r0)'
I agree,it is very annoying for the user,But can used is this code (if p1 run focus p2!) in particular places...
r2du-soft#
i have a code with vb6,this application show windows focus on witch application and show that title in a label

Option Explicit

Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long



Private Sub Timer1_Timer()

Dim foreground_hwnd As Long
Dim txt As String
Dim length As Long

    foreground_hwnd = GetForegroundWindow()

    txt = Space$(1024)
    length = GetWindowText(foreground_hwnd, txt, Len(txt))
    txt = Left$(txt, length)
    lblCaption = txt
    
    
    
End Sub

i how can change this vb6 code to nsis code?
i want check if focus on "New Text Document.txt - Notepad" then messagebox show focus is on you want! else messagebox show focus is on another application in nsis
is possible whit this code with nsis?!
T.Slappy#
You can call Windows API functions (like GetForegroundWindow) from NSIS by using the System plug-in.

Also you can declare variables like structs, integers etc. in NSIS with this plug-in.

I am not a VB coder but it looks like VB is using the same. E.g. this line:

Private Declare Function GetActiveWindow Lib "user32" () As Long looks like it is calling GetActiveWindow from user32.dll library which is pretty the same as NSIS System plug-in does.
r2du-soft#
Originally Posted by T.Slappy View Post
You can call Windows API functions (like GetForegroundWindow) from NSIS by using the System plug-in.

Also you can declare variables like structs, integers etc. in NSIS with this plug-in.

I am not a VB coder but it looks like VB is using the same. E.g. this line:

Private Declare Function GetActiveWindow Lib "user32" () As Long looks like it is calling GetActiveWindow from user32.dll library which is pretty the same as NSIS System plug-in does.

thanks mr T.Slappy

im trying to conver vb code to nsis code but i think my convert is wrong!:

;------------------
Function TrimText
 Exch $R0 ; char
 Exch
 Exch $R1 ; length
 Exch
 Exch 2
 Exch $R2 ; text
 Push $R3
 Push $R4
 
 StrLen $R3 $R2
 IntCmp $R3 $R1 Done Done
 
 StrCpy $R2 $R2 $R1
 
 StrCpy $R3 0
  IntOp $R3 $R3 + 1
  StrCpy $R4 $R2 1 -$R3
  StrCmp $R4 "" Done
  StrCmp $R4 $R0 0 -3
 
  IntOp $R3 $R3 + 1
  StrCpy $R4 $R2 1 -$R3
  StrCmp $R4 "" Done
  StrCmp $R4 $R0 -3
 
  IntOp $R3 $R3 - 1
  StrCpy $R2 $R2 -$R3
  StrCpy $R2 $R2...
 
 Done:
 StrCpy $R0 $R2
 Pop $R4
 Pop $R3
 Pop $R2
 Pop $R1
 Exch $R0 ; output
FunctionEnd
;------------------

;------------------
!macro TrimText Text Length Char Var
Push "${Text}"
Push "${Length}"
Push "${Char}"
 Call TrimText
Pop "${Var}"
!macroend
;------------------

;------------------
!define TrimText "!insertmacro TrimText"
;------------------

;------------------
Var foreground_hwnd
Var txt
Var length
;------------------

#----------------------------
Section
;------------------
System::Call user32::GetForegroundWindow('')i.r0
StrCpy $foreground_hwnd $0
;------------------

;------------------
System::Call 'user32::GetWindowText(i r2, t .r3, i ${NSIS_MAX_STRLEN})'
StrCpy $length $2($foreground_hwnd, $txt, ${NSIS_MAX_STRLEN})
;------------------

;------------------
;StrCpy $txt ${TrimText} "$txt" ${NSIS_MAX_STRLEN} " " $R0
${TrimText} "$txt" ${NSIS_MAX_STRLEN} " " $R0
;------------------

;------------------
StrCpy $txt $R0
MessageBox MB_OK|MB_ICONINFORMATION "$txt"
;------------------
SectionEND
#----------------------------
is possible help in convert that code from vb to nsis?


in vb code GetActiveWindow is no needing for work vb code.i nust remove that.

just i want convert this code to nsis:

Option Explicit

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long



Private Sub Timer1_Timer()

Dim foreground_hwnd As Long
Dim txt As String
Dim length As Long

    foreground_hwnd = GetForegroundWindow()

    txt = Space$(1024)
    length = GetWindowText(foreground_hwnd, txt, Len(txt))
    txt = Left$(txt, length)
    lblCaption = txt
    
End Sub
r2du-soft#
try again

hi
i try again to convert vb code to nsis code:

;------------------
Var foreground_hwnd
Var txt
Var length
Var length2
;------------------


Section

;------------------
;P1:
Strcpy $txt "                                                                                                                            "
MessageBox MB_OK|MB_ICONINFORMATION "$txt"
;------------------

;------------------
;P2:
System::Call 'user32::GetForegroundWindow()i .r0'
StrCpy $foreground_hwnd $0
MessageBox MB_OK|MB_ICONINFORMATION "$foreground_hwnd"
;------------------

;------------------
;P3:
System::Call 'user32::GetWindowText($foreground_hwnd, $txt, StrLen $length2 $txt)i .r3'
MessageBox MB_OK|MB_ICONINFORMATION "$3"
;------------------

;------------------
;P4:
;Then be completed
;------------------

SectionEND


i can't completing P3
Again not work!!!
I need help....
Anders#
You need to specify the parameter types in the call to GetWindowText. First look them up on MSDN and then find the matching type in the System plug-in readme...
r2du-soft#
Originally Posted by Anders View Post
You need to specify the parameter types in the call to GetWindowText. First look them up on MSDN and then find the matching type in the System plug-in readme...
hi,thanks
i see msdn and see this codes:
 Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Public Function GetParentFormCaption(ByVal lngHwnd As Long) As String

  Dim strBuff As String * 255

  Dim lngOldHwnd As Long, lngResult As Long

 

  lngOldHwnd = GetParent(lngHwnd)

  lngResult = GetWindowText(lngOldHwnd, strBuff, Len(strBuff))

 

  GetParentFormCaption = Trim(strBuff)

End Function

GetWindowText have 3 parameter and i use 3 parameter for that in convert
GetWindowText(lngOldHwnd, strBuff, Len(strBuff))


but i don't know whay my code callback to me value 0!
;------------------
;P3:
System::Call 'user32::GetWindowText($foreground_hwnd, $txt, StrLen $length2 $txt)i .r3'
MessageBox MB_OK|MB_ICONINFORMATION "$3"
;------------------
r2du-soft#
Find the handle that windows focus is on it

Hi
I know can find handle focus a process with this code:

FindWindow $0 "notepad" ""
Now how can find the handle that windows focus is on it?

For example now windows focus is on notepad
But suppose we dont know focus is on notepad!
i want when my application start after one minunts show a handle that windows focus is on it?


And after find that handle focus i want give notepad focus and after i want compare thats focuses and if thats handles equal then show message 'now windows focus is on notepad'

is possible?
Anders#
Stop asking these silly questions, you already have the System plugin code that calls GetForegroundWindow and you cannot change the focus unless your application already has focus.
r2du-soft#
Find the (handle|ClassName|Title) that windows focus is on it

Originally Posted by Anders View Post
Stop asking these silly questions, you already have the System plugin code that calls GetForegroundWindow and you cannot change the focus unless your application already has focus.

I found it:
Sleep 6666
System::Call user32::GetForegroundWindow()i.r1
System::Call 'USER32::GetClassName(ir1,t.r2,i ${NSIS_MAX_STRLEN})'
System::Call 'USER32::GetWindowText(ir1,t.r3,i ${NSIS_MAX_STRLEN})'
MessageBox mb_ok "Windows Focus Handle= ($1)$\r$\nWindows Focus Class Name= ($2)$\r$\nWindows Focus Title Name= ($3)" 
Now can:
1-Find Handle programs windows focus on its (Find Handle)
2-Find ClassName programs windows focus on its (Find ClassName)
3-Find Title programs windows focus on its (Find Title)

Thanks Mr Anders