| |

VerySource

 Forgot password?
 Register
Search
View: 852|Reply: 2

I want to develop a program with the function of deriving the names of all currently running windows.

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-6 11:20:01
| Show all posts |Read mode
consult
Reply

Use magic Report

2

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-6 14:33:01
| Show all posts
Available API functions: Get Windows (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Among them, hWnd is the current window handle, wCmd is a constant related to hWnd, and its meaning is as follows:
wCmd value
meaning
GW-GHILD
First child window
GW-HWNDFIRST
Child window's first sibling window, its first top-level window
GW-HWNDLAST
The last sibling window of the child window, or the last top-level window
GW-HWNDNEXT
Successor window
GW-HWNDPRCV
Previous window
GW-OWNER
Window owner
The return value of this function is the handle of the window pointed to by wCmd.
We use this handle and then use the function Get WindowsText (ByVal hWnd As Long, ByVal
Ipstring As String, ByVal cch As Long) As Long, will handle the window title specified by hWnd
Put into a string variable Ipstring, cch refers to the maximum number of characters put into Ipstring. This function
Returns the string length on success, or zero if the window has no title.
Before using the Get WindowsText function, you must also use the function Get WindowsTextLength (
ByVal hWnd As Long) As Long gets the length of the window title specified by hWnd and puts it into cch.
Write a procedure FindTitle () to find all titles running in the system, first get the first
The top-level window handle currwnd, and then use the While ... Wend loop structure, when currwnd is not zero and the label
When the length of the title text is not zero, save the obtained title into the list box Combo1, and then find the handle of the subsequent window.
When the handle currwnd = 0, it means that there is no subsequent window and exit the loop. This will put all windows in the system
Find out the handle and title.
However, during debugging, it is found that the window titles found by this method are very many, which indicates that the Windows system is running.
There are many hidden windows, and these windows are not needed by us, and they are also activated by AppActivate
An error occurred.
So we write a process Sift () to find the active window. The method is to use AppActivate
Activate all the windows one by one, and throw them away by mistake. Keep the active window title and put it in the list box Combo2.
First create a new form Form1, Caption = "Get window title", and create two tags on the form Form1
Check, Label1, Caption = "All window titles", Label2.Caption = "Activable window titles";
Create two drop-down list boxes, Combo1 stores all the title names in the system, Combo2 stores the active tags
Title name; create two more command buttons, Command1.Caption = "Activate Form",
For testing, the command button Command2.Caption = "Refresh", click it to find all the systems in the system again.
The name of the form in. Use this button when you run a new program after the program has run.
Select Add Module in Project from the VB system menu, and enter the following API functions and some constants.

'Module module

Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd
As Long) As Long
Declare Function GetWindowText Lib "user32" Aias "GetWindowTextA" (ByVal
hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowText Length Lib "user32" Aias "
GetWindowTextLengthA "(ByVal hwnd As Long) As Long
Public Const GW-HWNDFIRST = 0
Public Const GW-HWNDLAST = 1
Public Const GW-HWNDNEXT = 2
Public Const GW-HWNDPREV = 3
Public Const GW-OWNER = 4

Create two subroutines:

Sub FindTitle ()
'Find all window titles on the desktop
Dim currwnd As Integer
Combo1.Clear
Currwnd = GetWindow (hwnd, GW-HWNDFIRST)
While currwnd <> 0
Length = GetWindow TextLength (currwnd)
listitem $ = Space $ (length +1)
length = GetWindow Text (currwnd, listitem $, length + 1)
if length> 0 Then
Combo1.Addltem listitem $
End if

currwnd = GetWindow (currwnd, GW-HWNDNEXT)

if Combl1.ListCount> 0 Then
Combo1.Text = Combol.List (0)
Combo1.Listindex = 0
Else
MsgBox "No active window found", 16, "Active"
End if
Wend
End Sub

Sub Sift ()

'Test window is active

i = 0
Combo2.Clear
Do
On Local Error Resume Next
AppActivate Combo1.List (i)
If Err = 0 Then
Combo2.Additem Combo1.List (i)
End if
i = i + 1
Loop Unti 1 i = Combo1.ListCount-1
AppActivate Form1.Caption
If Combo2.ListCount> 0 then
Combo2.Text = Combo2.List (0)
Combo2.Listindex = 0
Else
MsgBox "No active window found", 16, "Active"
End if
End Sub
Private Sub Form-Load ()
Form1.Show 'first displays this form, otherwise the title of the found form does not have itself
MsgBox "Start Finding Window Title"
Call FindTitle
Call sift
End Sub
Private Sub Command1-Click ()
F $ = Combo2.Text
On Local Error Resume Next
AppActivate F $
End Sub

Private Sub Command2-Click ()
Call FindTitle
Call sift
End Sub
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-8 07:09:01
| Show all posts
The solution is on the first floor.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list