FindWindow这个函式检索处理顶级视窗的类名和视窗名称匹配指定的字元串。这个函式不搜寻子视窗。
基本介绍
- 中文名:FindWindow
- 外文名:FindWindow
- 类型:函式
- 特点:不搜寻子视窗
简述
lpClassName参数指向类名,lpWindowName指向视窗名,如果有指定的类名和视窗的名字则表示成功返回一个视窗的句柄。否则返回零。
FindWindow,
LPCTSTR lpClassName, // pointer to class name
LPCTSTR lpWindowName // pointer to window name
);
HWND hWnd, // handle of window
LPDWORD lpdwProcessId // address of variable for process identifier
);
DWORD dwDesiredAccess, // access flag
BOOL bInheritHandle, // handle inheritance flag
DWORD dwProcessId // process identifier
);
HANDLE hProcess, // handle to process whose memory is written to
LPVOID lpBaseAddress, // address to start writing to
LPVOID lpBuffer, // pointer to buffer to write data to
DWORD nSize, // number of bytes to write
LPDWORD lpNumberOfBytesWritten // actual number of bytes written
);
const
ResourceOffset: dword = $004219F4;
resource: dword = 3113226621;
ResourceOffset1: dword = $004219F8;
resource1: dword = 1940000000;
ResourceOffset2: dword = $0043FA50;
resource2: dword = 1280185;
ResourceOffset3: dword = $0043FA54;
resource3: dword = 3163064576;
ResourceOffset4: dword = $0043FA58;
resource4: dword = 2298478592;
var
hw: HWND;
pid: dword;
h: THandle;
tt: Cardinal;
begin
hw := FindWindow('XX', nil);
if hw = 0 then
Exit;
GetWindowThreadProcessId(hw, @pid);
h := OpenProcess(PROCESS_ALL_ACCESS, false, pid);
if h = 0 then
Exit;
if flatcheckbox1.Checked=true then
begin
WriteProcessMemory(h, Pointer(ResourceOffset), @Resource, sizeof(Resource), tt);
WriteProcessMemory(h, Pointer(ResourceOffset1), @Resource1, sizeof(Resource1), tt);
end;
if flatcheckbox2.Checked=true then
begin
WriteProcessMemory(h, Pointer(ResourceOffset2), @Resource2, sizeof(Resource2), tt);
WriteProcessMemory(h, Pointer(ResourceOffset3), @Resource3, sizeof(Resource3), tt);
WriteProcessMemory(h, Pointer(ResourceOffset4), @Resource4, sizeof(Resource4), tt);
end;
MessageBeep(0);
CloseHandle(h);
close
函式原型
HWND FindWindow(LPCSTR lpClassName,LPCSTR lpWindowName);
参数表
lpClassName
指向一个以NULL字元结尾的、用来指定类名的字元串或一个可以确定类名字元串的原子。如果这个参数是一个原子,那幺它必须是一个在调用此函式前已经通过GlobalAddAtom函式创建好的全局原子。这个原子(一个16bit的值),必须被放置在lpClassName的低位位元组中,lpClassName的高位位元组置零。
如果该参数为null时,将会寻找任何与lpWindowName参数匹配的视窗。
lpWindowName
指向一个以NULL字元结尾的、用来指定视窗名(即视窗标题)的字元串。如果此参数为NULL,则匹配所有视窗名。
返回值
如果函式执行成功,则返回值是拥有指定视窗类名或视窗名的视窗的句柄。
如果函式执行失败,则返回值为 NULL 。可以通过调用GetLastError函式获得更加详细的错误信息。
C#中
导入库:user32.lib
头档案:winuser.h
***.Net 中运用
命名空间 using System.Runtime.InteropServices;
导入库 [DllImport("user32.dll")]
函式原型 public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
参数说明 lpClassName String,指向包含了视窗类名的空中止(C语言)字串的指针;或设为零,表示接收任何类
lpWindowName String,指向包含了视窗文本(或标籤)的空中止(C语言)字串的指针;或设为零,表示接收 任何视窗标题
返回值 :句柄
C++中
头档案:afxwin.h
例子:
//Activate an application with a window with as pecific class nameBOOLCMyApp::FirstInstance(){CWnd*pWndPrev,*pWndChild;//Determine if a window with the class name exists...pWndPrev=CWnd::FindWindow(_T("MyNewClass"),NULL);if(NULL!=pWndPrev){//If so, does it have any popups?pWndChild=pWndPrev->GetLastActivePopup();//If iconic, restore the main windowif(pWndPrev->IsIconic())pWndPrev->ShowWindow(SW_RESTORE);//Bring the main window or its popup to the foregroundpWndChild->SetForegroundWindow();//And you are done activating theother applicationreturnFALSE;}returnTRUE;}
声明
VB6.0中的声明:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
FINDWINDOWSDelphi简单例子:
var
t:thandle;
begin
t:=findwindow(nil,'计算器'); //运行windows计算器,获取计算器的句柄
showmessage(inttostr(t));
end;
易语言中的声明:
.版本 2
.DLL命令 FindWindow, 整数型, "user32.dll", "FindWindowA"
.参数 类名, 文本型
.参数 标题, 文本型