05-27-2002, 12:03 PM
I'm developing a simple message notification app where its form needs to be always on top (while visible), yet not steal focus from other apps. This to not affect anything else that the user might be doing meanwhile (like writing documents and stuff) yet provide the notification in a non-obstructible way.
The notification will show for just a short while (when a message arrives), and currently when I make the form visible it steals focus.
I haven't found any combination of property settings that does what I need. I suspect it has something to do with EnterFocus etc.
05-27-2002, 12:56 PM
You can set your window to be "always on top" using the SetWindowPos API.
Declare Function SetWindowPos Lib "user32" ( _
ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long _
) As Long
Public Const HWND_TOPMOST = -1
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE
05-27-2002, 01:08 PM
Try showing your window using the ShowWindow API, using the constant SW_SHOWNOACTIVATE
06-02-2002, 09:17 AM
A combination of your hints seems to do the trick:
SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_NOACTIVATE
Form1.Visible = True
For some reason the app stills "steals" focus the very first time the notification banner is shown, but not from that point on. Haven't figured that one out yet.
06-08-2002, 04:05 PM
try using this when calling your form
06-13-2002, 05:34 PM
I think not. That will guarantee that the form grabs focus.
06-14-2002, 02:18 AM
It seems W2000 behaves differently from W98 (or it's a timing issue), so I still haven't solved this. Time to read the WIN32 docs in depth...
A prototype of Notify is now published at
Note the use of a system tray icon and menu.
- Configuration (now just a dialog with no IQ). As Notify uses MAPI there's actually no fundamental configuration that needs to be done.
- The above issue with stealing focus.
- I use the message date/time as trigger for new messages, but the resolution is only 1 minute, so if messages arrive within 1 minute only the first is shown. I'll find a workaround.
- The splash screen disappears too quickly.
- etc etc etc
06-14-2002, 03:39 PM
I seem to have solved the focus issue now, by removing use of Visible and instead relying solely on SetWindowPos. This makes me wonder why VB doesn't include this, and many many other WIN32 calls, as native VB calls or properties.
I made the following call to handle it (note the addition of SHOWWINDOW that to my understanding fully replaces "Visible = True"):
Public Sub SetFormPosition(hWnd As Long, Position As Long)
SetWindowPos hWnd, Position, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW Or SWP_NOACTIVATE
SetFormPosition hWnd, HWND_TOPMOST
The splash screen behaves as it should now as well.