1. How to get info about foreground window in Windows?

Info: handle, class, title, coordinates.

new topic     » topic index » view message » categorize

2. Re: How to get info about foreground window in Windows?

SocIoDim said...

Info: handle, class, title, coordinates.

Here is a sample program...

include std/dll.e 
include std/machine.e 
include std/os.e 
 
atom WINuser32 = 0 
atom WINkernel32 = 0 
 
atom WINGetForegroundWindow = 0 
atom WINGetWindowInfo = 0 
atom WINSendMessage = 0 
atom WINGetLastError = 0 
 
----------------- 
procedure InitWinAPI() 
----------------- 
	WINuser32 = open_dll("user32.dll") 
	WINkernel32 = open_dll("kernel32.dll") 
 
	if WINuser32 != 0 then 
		WINGetForegroundWindow = define_c_func(WINuser32, "GetForegroundWindow", {}, C_POINTER) 
		WINGetWindowInfo       = define_c_func(WINuser32, "GetWindowInfo", {C_POINTER, C_POINTER}, C_INT) 
		WINSendMessage         = define_c_func(WINuser32, "SendMessageA", {C_POINTER, C_INT, C_POINTER, C_POINTER}, C_INT)  
	end if 
	 
	if WINkernel32 != 0 then 
		WINGetLastError        = define_c_func(WINkernel32, "GetLastError", {}, C_POINTER) 
	end if 
end procedure 
 
 
------------------- 
public function GetForegroundWindow() 
------------------- 
	return c_func( WINGetForegroundWindow, {}) 
end function 
 
------------------- 
public function SendMessage(atom pHwd, atom pMsg, atom pwParm, atom plParm) 
------------------- 
	return c_func( WINSendMessage, {pHwd, pMsg, pwParm, plParm}) 
end function 
 
constant WM_GETTEXT = #000D 
 
 
------------------- 
public function GetLastError() 
------------------- 
	return c_func( WINGetLastError, {}) 
end function 
 
 
-- This needs the handle of the window whose information you want. 
-- 
-- It returns a sequence containing ... 
--	 An atom. 0 if the function succeeded otherwise the error code returned by Windows. 
--       (If not zero, it is the only item in the return sequence.) 
--   A sequence containing the Window's pixel co-ords for left, top, right, bottom 
--   A sequence containing the Window client area pixel co-ords for left, top, right, bottom 
--   An atom containing the Style bits 
--   An atom containing the ExStyle bits 
--   An integer. 1 if the window is active, zero otherwise. 
--   An integer. The height of the horizontal borders (in pixels) 
--   An integer. The height of the vertical borders (in pixels) 
--   The Window's class identifier.  
--   The version of Windows of the application that created this window. 
--   The text of the Window's title bar 
 
------------------- 
public function GetWindowInfo(atom pHandle) 
------------------- 
	atom lWinData 
	sequence lWinInfo 
	atom lError 
	 
	lWinData = allocate(62) 
-- typedef struct tagWINDOWINFO { 
--   DWORD cbSize   #  0 
--   RECT  rcWindow #  4 
--   RECT  rcClient # 20 
--   DWORD dwStyle  # 36 
--   DWORD dwExStyle # 40 
--   DWORD dwWindowStatus # 44 
--   UINT  cxWindowBorders # 48 
--   UINT  cyWindowBorders # 52 
--   ATOM  atomWindowType  # 56 
--   WORD  wCreatorVersion # 60 
-- } WINDOWINFO   # 62 
	 
	poke4(lWinData, 0) 
	if c_func( WINGetWindowInfo, {pHandle, lWinData}) then 
		lError = 0 
		lWinInfo = { 
			{peek4s(lWinData + 04), 
			 peek4s(lWinData + 08), 
			 peek4s(lWinData + 12), 
			 peek4s(lWinData + 16)}, 
			 
			{peek4s(lWinData + 20), 
			 peek4s(lWinData + 24), 
			 peek4s(lWinData + 28), 
			 peek4s(lWinData + 32)}, 
 
			peek4u(lWinData + 36), 
			peek4u(lWinData + 40), 
			peek4u(lWinData + 44), 
			peek4s(lWinData + 48), 
			peek4s(lWinData + 52), 
			peek4s(lWinData + 56), 
			peek2s(lWinData + 60) 
			 
		} 
		 
		-- Note: Assumes ANSI Window Text 
		atom lText 
		atom lTextLen 
		 
		lText = allocate(1000) 
		lTextLen = SendMessage(pHandle, WM_GETTEXT, 999, lText) 
	 
		lWinInfo = append(lWinInfo, peek({lText, lTextLen})) 
		free(lText) 
	else 
		lError = GetLastError() 
		lWinInfo = {} 
	end if 
	 
	free(lWinData) 
	 
	return lError & lWinInfo 
end function 
 
 
 
InitWinAPI() 
include std/console.e 
 
sleep(5)  -- gives you a chance to select a window to look at. 
display (GetWindowInfo( GetForegroundWindow() )) 
new topic     » goto parent     » topic index » view message » categorize

3. Re: How to get info about foreground window in Windows?

Thanks! I am a long time poring over. At first it seemed too complicated, but then began to clear. I noticed that the window handle is not checked for NULL. Info from Microsoft:

HWND WINAPI GetForegroundWindow(void);

Parameters

This function has no parameters.

Return value

Type:

Type: HWND

The return value is a handle to the foreground window. The foreground window can be NULL in certain circumstances, such as when a window is losing activation.

new topic     » goto parent     » topic index » view message » categorize

4. Re: How to get info about foreground window in Windows?

SocIoDim said...

I noticed that the window handle is not checked for NULL.

So, deal with that. It was just a sample for you to start your own code from.

new topic     » goto parent     » topic index » view message » categorize

5. Re: How to get info about foreground window in Windows?

Actually, I've left some 'debug' code in the sample. Sorry. Change the line ...

poke4(lWinData, 0) 

to ...

poke4(lWinData, 62) 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu