快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

葡京娱乐场娱乐网址:Process Thread Window之关系



许多人包括我自己,对於window与process或Thread间的对应,常感困扰,我就今朝所知者,大年夜概的先容一下:一个Process有自己4GB的位址空间,这个不雅念对照轻易吸收,然则它却不是履行程式的单位,真正履行程式者是Thread,每个Process被Create时,便会有一个主thread 来履行它,我们又可以Create其他的Thread来履行程式,假设现在就有两个thread於这个Process了,那麽,这两个Thread原则上是共用Process的同一个位址空间,例如:背景列印,然则至少有两个Thread在服务,它的好处不用阐明太多大年夜家都可以明白,但它同时也会有其他问题(犹如步、假如一个thread一个不小心当了,那有可能会使全部Process逝世掉落),这些不在评论争论范围。而window呢,它必然有一个thread来履行它,但一个Thread却可以有不光一个的 Windows或根本没有。别的,一个Process有一个的独一的Process ID而每个Thread也有独一的ThreadID,而节制Process 则大年夜多透过Process Handle(hProcess)而非 Process ID,正如透过hWnd来节制Window一样。犹如前面所言,一个window有一个Thread在履行它,那该若何取得呢?请用 Declare Function GetWindowThreadProcessId Lib "user32" _ (ByVal hwnd As Long, lpdwProcessId As葡京娱乐场娱乐网址 Long) As Long 这个function传入一个hwnd而取得Process ID(其第二个叁数),而传回值是 ThreadID。而应用OpenProcess()来取得Process Handle,底下程式的目的是取得 Window ClassName, 与Window Title,我们征采window时,常应用FindWindow() 但这个function传入的叁数第一个就是Window ClassName,但我们常常不知道 ClassName透过它,我们可随意马虎来得知,进而做许多的节制。 程式应用要领: 1.先将想查询的window叫出,放在桌面上 2.履行本程式,把不需要的window关掉落,直到想查询的window看得见(不必整个) 3.在form1的Command1上按下mouse的右键不要放掉落,这时会看到mouse变问号加箭头 而且form1不见了,代表您可以疲塌mouse了。 4.移到想查询的window上面,放掉落mouse,而後form1再度呈现,上有所要的资讯

一个form , 一个ommandBox, 2 labels

Private Type POINTAPI

X As Long

Y As Long

End Type

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, _

ByVal hWndInsertAfter As Long,  al X As Long, ByVal Y As Long, _

ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, _

ByVal yPoint As Long) As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _

(ByVal hwnd As Long, ByVal lpClassName As String, _

ByVal nMaxCount As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _

(ByVal hwnd As Long, lpdwProcessId As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" _

(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _

ByVal dwProcessId As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _

ByVal uExitCode 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

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _

(B葡京娱乐场娱乐网址yVal lpClassName As String, ByVal lpWindowName As String) As Long

Const HWND_TOP = 0

Const HWND_BOTTOM = 1

Const SWP_NOSIZE = &H1

Const SWP_NOMOVE = &H2

Const PROCESS_QUERY_INFORMATION = &H400

Private TitleName As String

Private ClassN葡京娱乐场娱乐网址ame As String

Private hProcess As Long

Private Candisplay As Boolean

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer,

X As Single, Y As Single)

Dim aa As Long

If Button = vbRightButton Then

将window设定成在底层

aa = SetWindowPos(Me.hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)

Me.MousePointer = vbArrowQuestion

End If

End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single,

Y As Single)

Dim aa As Long

Dim pt As POINTAPI

Dim hwnd5 As 葡京娱乐场娱乐网址Long

Dim str5 As String

Dim len5 As Long

Dim thrid As Long, pid As Long

If Button = vbRightButton Then

设定window到上层

aa = SetWindowPos(Me.hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE)

Me.MousePointer = vbDefault

取得mouse今朝的座标,不能用mouseup Event的x,y因那是相对於form的座标

aa = GetCursorPos(pt)

取得pt所在座标,是落在那一个window上面

hwnd5 = WindowFromPoint(pt.X, pt.Y)

len5 = 256

ClassName = String(255, 0)

取得该window的Class name

aa = GetClassName(hwnd5, ClassName, len5)

ClassName = Left(ClassName, aa)

len5 = 256

TitleName 葡京娱乐场娱乐网址= String(255, 0)

取得该window的title

aa = GetWindowText(hwnd5, TitleName, len5)

TitleName = Left(TitleName, aa)

依hwnd取得相对应的threadID(thrid), ProcessID(pid)

thrid = GetWindowThreadProcessId(hwnd5, pid)

取得Process Handle

hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid)

有了hProcess那可服务可多啦,例如:可侦测 它何时停止,或强制它停止等等

Call TerminateProcess(hProcess, 38) 最好别这麽做,否则,嘿嘿,你自己试

Label1.Caption = " Title = " + TitleName

Label2.Caption = "Calss Name = " + ClassName

End If

End Sub

您可能还会对下面的文章感兴趣: