Getting the hWnd of active window

Windows specific questions.
Post Reply
3apa3a
Posts: 13
Joined: Aug 23, 2005 8:49
Location: Helsinki
Contact:

Getting the hWnd of active window

Post by 3apa3a »

Hello, is there a way of getting the currently active window hWnd?
Sample code of finding that?
Hexadecimal Dude!
Posts: 360
Joined: Jun 07, 2005 20:59
Location: england, somewhere around the middle
Contact:

Post by Hexadecimal Dude! »

Here's something someone (....i think DrV) told me on IRC: Use FindWindow

Code: Select all

windowtitle "myuniquewindowtitle"

screenres 10,10,32,0

hWnd = FindWindow(NULL,"myuniquewindowtitle")

print hWnd
i forgot which lib it's in, as my proggy i'm taking this from includes...

Code: Select all

'$include: "\win\shellapi.bi"
'$include: "\win\user32.bi"
good luck

-Hex
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

Code: Select all

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
      (ByVal lpClassName As long, ByVal WindowName As string) As Long

dim hWIN as long
screenres 320,200
windowtitle "...."

hWIN=FindWindow(byval 0&,"....")
print hWIN
sleep
Joshy

EDIT: Hi Hexadecimal Dude! while i reading the WIN API under Linux you answered too ;-)
Hexadecimal Dude!
Posts: 360
Joined: Jun 07, 2005 20:59
Location: england, somewhere around the middle
Contact:

Post by Hexadecimal Dude! »

:)
I was hoping there woud be a cleaner way, though...

*starts to dream* It would be nice if gfxlib had a fuction........

:P


-Hex
Hexadecimal Dude!
Posts: 360
Joined: Jun 07, 2005 20:59
Location: england, somewhere around the middle
Contact:

Post by Hexadecimal Dude! »

oh! you can change the title afterward, incase your unique name is.... embarrassing:

Code: Select all

'$include: "\win\user32.bi"

windowtitle "1337  1337  1337"

screenres 500,500,32,0


hWnd = FindWindow(NULL,"1337  1337  1337")
print hWnd

windowtitle "Pro Database 2005"

print FindWindow(NULL,"Pro Database 2005")

sleep
:P

I suppose you could write a func to make up unique names for you, like:

*untested*

Code: Select all

'$include: "\win\user32.bi"

function WindowHandle(wdth,height,bpp,propername$)
    randomize timer
    i = 1
    winame$ = ""
    for i = 1 to 20
        mynum = int(rnd() * 254 + 1)
        winame$ = winame$ + chr$(mynum)
    next i
    
    windowtitle winame$
    screenres wdth,height,bpp
    hWnd = FindWindow(NULL,winame$)
    windowtitle propername$
    return hWnd
end function

hWnd = WindowHandle(500,500,32,"Propper Window Title")

x = 1
for x = 1 to 200
    SetWindowPos(hWnd,HWND_TOP,x,1,500,500,SWP_NOREPOSITION)
    sleep 5
next x

print "Yay for window number " + str$(hWnd)

sleep
-Hex :)

edit: tested :)
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

Here the real man's way of doing it:

Code: Select all

#include "windows.bi"

#define WINDOW_TITLE_SIZE	128
#define WINDOW_CLASS_PREFIX "fbgfxclass_"

Type BLITTER As Sub(As uByte Ptr, As Integer)

Type Win32Driver
    Version As Integer
    hInstance As HINSTANCE
    WndClass As WNDCLASS
    Wnd As HWND    
    Palette(0 To 255) As PaletteEntry
    Blitter As Blitter Ptr
    Is_Running As Integer
    Is_Palette_Changed As Integer
    Is_Active As Integer
    w As Integer
    h As Integer
    Depth As Integer
    FullScreen As Integer
    Refresh_Rate As Integer
    Window_Title As uByte Ptr
    WindowClass(0 To WINDOW_TITLE_SIZE + Len(WINDOW_CLASS_PREFIX)-1) As uByte
    Init As Function() As Integer
    Exit As Function() As Integer
    Paint As Function() As Integer
    Thread As Sub(ByVal Running_Event As HANDLE)
End Type

Extern FB_Win32 Alias "fb_win32" As Win32Driver

Screen 13

Dim WindowTextLength As Integer
Dim WindowText As String

WindowTitle "Hello"

WindowTextLength = GetWindowTextLength(FB_Win32.Wnd)
WindowText = Space(WindowTextLength)
GetWindowText FB_Win32.Wnd, StrPtr(WindowText), WindowTextLength + 1

Print WindowText

Sleep
Some of the C code in that structure was a little confusing, so lemme know if I made any mistakes in translating the struct. It's in src/gfxlib2/win32/fb_gfx_win32.h in the source.
Hexadecimal Dude!
Posts: 360
Joined: Jun 07, 2005 20:59
Location: england, somewhere around the middle
Contact:

Post by Hexadecimal Dude! »

where's windows.bi (it says it can't find it)
jofers wrote:Here the real man's way of doing it:
yes, i fully argree, yesterday I allowed my friend to tie my hair with a bow. I wore it like that al the way through music. My teacher didn't realise it had happened just moments prior to her arrival for a joke, she thought i'd made a... lifestyle change :)

......wait this is the same teacher who reffered to me as "she" on last years report...... maybe i'm loosing my masculinity 8O.....

If anyone needs me i'll be outside, exercising, drinking beer and fixing things.

+also (sorry to use your thread 3apa3a) do you have any fool's examples of how i might go about swalowing keystrokes after i'm finished with them?

-Hex
Last edited by Hexadecimal Dude! on Sep 13, 2005 21:11, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

hello jofers,
i have put your example to my mode struc hack example.
(link removed)

I hope it is ok for you.

Joshy
Last edited by D.J.Peters on Oct 06, 2007 20:19, edited 1 time in total.
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

DJ Peters: I don't care if you use it. There's another example using RTLIB & GFXLIB externs where someone was asking on how to hook PRINT to a custom function around the "Programming" thread, you may want to check that one out as well.

Hexadecimal Dude!: The example uses the "new" windows headers, which you'll have to pull from the CVS.

For the headers you have:
#include "win/user32.bi"

If you get any errors on any parameters passed in these new functions, just pass them like this

New headers: DoFunction @myvar, myvar2
Old headers: DoFunction ByVal @myvar, myvar2

Pointers have to be passed like that becuase the old headers have a lot of ByRef's.

The new headers will be used from releases 0.15b on.
3apa3a
Posts: 13
Joined: Aug 23, 2005 8:49
Location: Helsinki
Contact:

Post by 3apa3a »

Thanks to all of you that replied, but this is not what i wanted o_O
I want to get the hWnd of the window that was active before own program starts...
Like, when i press a hot key i have to locate the active window.
Sorry for confusion.
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

Code: Select all

#include "windows.bi"

Dim hWnd As HWND
hWnd = GetForegroundWindow

Dim WindowTextLength As Integer
Dim WindowText As String

WindowTextLength = GetWindowTextLength(hWnd)
WindowText = Space(WindowTextLength)
GetWindowText hWnd, StrPtr(WindowText), WindowTextLength + 1

MessageBox 0, StrPtr(WindowText), "hey", 0
Remember to compile with -s gui or the foreground window will be the console. May I ask why you want to do this?
3apa3a
Posts: 13
Joined: Aug 23, 2005 8:49
Location: Helsinki
Contact:

Post by 3apa3a »

jofers wrote: Remember to compile with -s gui or the foreground window will be the console. May I ask why you want to do this?
Thank you, jofers. This code worked... I just couldnt find the API function..
Well, i actually needed for my small VB6 program. I have been asked for a program that shows an empty window at specified coordinates and can be toggled visible by a hot key. And that window shouldn't be active O_O

It works fine in Form_Load

Code: Select all

PrevhWnd = GetForegroundWindow
Me.Show
SetForegroundWindow PrevhWnd
Post Reply