Display form / text / anything on top of a DirectX application

I am looking to make a program which can display the current time, on top of all windows, but primaryli games, which use DirectX.  (Most games haven't got an ingame clock, and its not possible to check the clock in the systray when playing)

I have a program working, using SetWindowPos, and its working fine when in Windows .. but when running games, i've only yet found one where it works, the rest the display just shows for a splitsecond (i guess a single frame), and then disappears.

Is it at all possible to do this from Visual Basic?

I've seen some other apps that can do this, so I know its possible.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Have you tried using BringWindowToTop api instead?

' in declarations:
Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

' in form_load or simular

BringWindowToTop Form1.hwnd

Im not 100% sure if this will work with DirectX apps.... also, what is the resolution of the games you want the form to be on top of?

'In a module or declared private in a forms declarations
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1

'In a form_activate/load routing
Call SetWindowPos(FormName.hWnd, HWND_TOPMOST, 0&, 0&, 0&, 0&, FLAGS)

Replace FormName with the name of the form.

This will set that form ontop of any other window and keep it there.  The only way anything will cover it up is if another form calls the same type of code to set its position on top which might be your case so you might want a timer or routine to track what form is currently the highest in z-order then set it back to your app.
FlyveHestAuthor Commented:

This is exactly what I already do, and it doesn't work (as I started in the question).  The form only displays for what I guess is a single frame, and then disappears.

It doesn't do this in all games, but out of the four i've tested so far, I can only get it to work in one.  I read that you can open a DirectX applicaion in shared and exclusive mode, and its probably this that causes the program not to work like it was intended.

BUT, a program like PowerStrip can display text on top of a DirectX application, and it stays there no matter what, and its this function that I would like to replicate (I dont know if it hooks into the drivers, uses some special DirectX screen, or something third)
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Basically, the display adapter is reserved for the game while in full-screen mode. When in windowed-mode, only the desktop rectangle of the client (game) window is reserved (until moved) which allows repainting of other windows out-of-the-way of the client window. I'm not too keen on DirectX programming, but the logic of it should be somewhat like that.

The only solution I can see for you to do is attempt some type of kernel-mode driver (2k/XP) or VxD (Win9x) to interact directly with the display adapater. And that most-likely will require using assembly.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
i would think that watching the z-order would fix this problem, im sure that every frame the game sets its z-order back to the top, so if you just watch that it and keep calling the routine to set your form on top it should work, at least in my head it does :)
FlyveHestAuthor Commented:
I tried this (actually before you wrote the comment), and it kind'a works.  The form i display flashes every other frame, and it looks terrible.

Do you know of any OCX, DLL or other that could accomplish this?  (This was more or less what I expected, btw, having to use some third party control to get this to work, as it probably is way to low-level for VB to interact with properly)

What I have done atm is, instead of displaying the time, i've created a bunch of samples, and then I get my program to say it, instead of display it.
Hi, if you have a full-screen Directx application, you cannot force a window on top of it. You are in full screen single application environment and thus only one application at a time can be displayed (the one that created it).

You should minimize the directx Application if runnig or changing to windowed mode.

Or create your DirectX message window.


Think I may have a possible solution for you. If you set the parent window of your application to the game's window then it may be able to display above it. Example below:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Const GWL_HWNDPARENT = (-8)
Private Sub Form_Activate()
    Dim lParentHwnd As Long, lChildHwnd As Long, lFlags As Long
    lParentHwnd = FindWindow(vbNullString, "Sid Meier's Civilization III")
    lChildHwnd = Form1.hwnd
    Call SetWindowLong(lChildHwnd, GWL_HWNDPARENT, lParentHwnd)
End Sub

After that, you can try setting the Z-ORDER of your application to keep it there. I haven't tried it with much (just Civ3). :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.