Old 28th March 2016, 08:18   #1
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 342
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?
r2du-soft is offline   Reply With Quote
Old 28th March 2016, 08:38   #2
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,280
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!

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 28th March 2016, 20:26   #3
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 342
Quote:
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:
HTML Code:
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 is offline   Reply With Quote
Old 28th March 2016, 20:54   #4
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 342
i have a code with vb6,this application show windows focus on witch application and show that title in a label

HTML Code:
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?!
r2du-soft is offline   Reply With Quote
Old 29th March 2016, 07:06   #5
T.Slappy
Major Dude
 
T.Slappy's Avatar
 
Join Date: Jan 2006
Location: Slovakia
Posts: 543
Send a message via ICQ to 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.

Cool looking installers with custom design: www.graphical-installer.com
Design NSIS setup Pages easily: www.install-designer.com
Build NSIS installers in Visual Studio 2005-2019: www.visual-installer.com
or RAD Studio 2009, 2010, XE-10.3 Rio: www.rad-installer.com
T.Slappy is offline   Reply With Quote
Old 1st April 2016, 20:15   #6
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 342
Quote:
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!:

HTML Code:

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

HTML Code:
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 is offline   Reply With Quote
Old 13th April 2016, 19:37   #7
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 342
try again

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

HTML 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....
r2du-soft is offline   Reply With Quote
Old 13th April 2016, 20:04   #8
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,280
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...

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 13th April 2016, 20:34   #9
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 342
Quote:
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:
HTML Code:
 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
HTML Code:
GetWindowText(lngOldHwnd, strBuff, Len(strBuff))


but i don't know whay my code callback to me value 0!
HTML Code:
;------------------
;P3:
System::Call 'user32::GetWindowText($foreground_hwnd, $txt, StrLen $length2 $txt)i .r3'
MessageBox MB_OK|MB_ICONINFORMATION "$3"
;------------------
r2du-soft is offline   Reply With Quote
Old 13th April 2016, 20:55   #10
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,280
The System plug-in readme even has a example that uses the function you are trying to call!

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 16th April 2016, 22:43   #11
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 342
Find the handle that windows focus is on it

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

HTML 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?
r2du-soft is offline   Reply With Quote
Old 16th April 2016, 22:54   #12
Anders
Moderator
 
Anders's Avatar
 
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,280
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.

IntOp $PostCount $PostCount + 1
Anders is offline   Reply With Quote
Old 19th April 2016, 17:52   #13
r2du-soft
Senior Member
 
r2du-soft's Avatar
 
Join Date: Nov 2013
Location: Iran
Posts: 342
Find the (handle|ClassName|Title) that windows focus is on it

Quote:
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:
PHP Code:
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
r2du-soft is offline   Reply With Quote
Reply
Go Back   Winamp & Shoutcast Forums > Developer Center > NSIS Discussion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump