Getting the hWnd of active window
Getting the hWnd of active window
Hello, is there a way of getting the currently active window hWnd?
Sample code of finding that?
Sample code of finding that?
-
- Posts: 360
- Joined: Jun 07, 2005 20:59
- Location: england, somewhere around the middle
- Contact:
Here's something someone (....i think DrV) told me on IRC: Use FindWindow
i forgot which lib it's in, as my proggy i'm taking this from includes...
good luck
-Hex
Code: Select all
windowtitle "myuniquewindowtitle"
screenres 10,10,32,0
hWnd = FindWindow(NULL,"myuniquewindowtitle")
print hWnd
Code: Select all
'$include: "\win\shellapi.bi"
'$include: "\win\user32.bi"
-Hex
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
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
EDIT: Hi Hexadecimal Dude! while i reading the WIN API under Linux you answered too ;-)
-
- Posts: 360
- Joined: Jun 07, 2005 20:59
- Location: england, somewhere around the middle
- Contact:
-
- Posts: 360
- Joined: Jun 07, 2005 20:59
- Location: england, somewhere around the middle
- Contact:
oh! you can change the title afterward, incase your unique name is.... embarrassing:
:P
I suppose you could write a func to make up unique names for you, like:
*untested*
-Hex :)
edit: tested :)
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
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
edit: tested :)
Here the real man's way of doing it:
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.
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
-
- Posts: 360
- Joined: Jun 07, 2005 20:59
- Location: england, somewhere around the middle
- Contact:
where's windows.bi (it says it can't find it)
......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
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 :)jofers wrote:Here the real man's way of doing it:
......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.
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
hello jofers,
i have put your example to my mode struc hack example.
(link removed)
I hope it is ok for you.
Joshy
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.
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.
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.
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
Thank you, jofers. This code worked... I just couldnt find the API function..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?
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