|
![]() |
#1 |
Senior Member
Join Date: Nov 2013
Location: Iran
Posts: 343
|
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? |
![]() |
![]() |
![]() |
#2 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,356
|
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 |
![]() |
![]() |
![]() |
#3 | |
Senior Member
Join Date: Nov 2013
Location: Iran
Posts: 343
|
Quote:
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)' |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Join Date: Nov 2013
Location: Iran
Posts: 343
|
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?! |
![]() |
![]() |
![]() |
#5 |
Major Dude
|
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 Create Setup Pages easily: www.install-designer.com Build installers in Visual Studio 2005-2019: www.visual-installer.com or RAD Studio 2009, 2010, XE-10.4 Sydney: www.rad-installer.com |
![]() |
![]() |
![]() |
#6 | |
Senior Member
Join Date: Nov 2013
Location: Iran
Posts: 343
|
Quote:
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 #---------------------------- 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 |
|
![]() |
![]() |
![]() |
#7 |
Senior Member
Join Date: Nov 2013
Location: Iran
Posts: 343
|
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.... |
![]() |
![]() |
![]() |
#8 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,356
|
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 |
![]() |
![]() |
![]() |
#9 | |
Senior Member
Join Date: Nov 2013
Location: Iran
Posts: 343
|
Quote:
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" ;------------------ |
|
![]() |
![]() |
![]() |
#10 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,356
|
The System plug-in readme even has a example that uses the function you are trying to call!
IntOp $PostCount $PostCount + 1 |
![]() |
![]() |
![]() |
#11 |
Senior Member
Join Date: Nov 2013
Location: Iran
Posts: 343
|
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" "" 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? |
![]() |
![]() |
![]() |
#12 |
Moderator
Join Date: Jun 2002
Location: ${NSISDIR}
Posts: 5,356
|
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 |
![]() |
![]() |
![]() |
#13 | |
Senior Member
Join Date: Nov 2013
Location: Iran
Posts: 343
|
Find the (handle|ClassName|Title) that windows focus is on it
Quote:
I found it: PHP Code:
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 |
|
![]() |
![]() |
![]() |
|
Thread Tools | Search this Thread |
Display Modes | |
|
|