RE: Application Foreground "Timeout"

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: shapij (shapij_at_discussions.microsoft.com)
Date: 09/24/04


Date: Fri, 24 Sep 2004 02:09:01 -0700

Well, it's been a long hard slog and I believe I've formulated a solution to
this problem.

First, I tried sending a message using a timer and it had the partial effect
of doing what pressing one of my programmed HotKeys did. That is:

"I'm sure the app is still running because pressing a programmed HotKey
causes the top portion of my app to appear over the title bar. And then
tapping that exposed area of my app's screen restores the rest of the app to
full view." (Quoted from my initial post of this thread.)

However, this was exactly the same! as my desperation try of using
Me.BringToFront in my new timer routine. (Which was one line of code vs.
approximately 30 lines.)

Here's what DID work. I finally figured out that an API call using "user32"
wouldn't work. I needed to change that to "CoreDLL.dll".

FIRST THE CLASS
============================================
    Public Class WinMgt
        'Windows Positioning & Control Support.
        '...API Declarations.
        Private Declare Function ShowWindow Lib "CoreDll.dll" (ByVal hWnd As
Integer, ByVal nCmdShow As Integer) As Integer
        Private Declare Function SetWindowPos Lib "CoreDll.dll" (ByVal hWnd
As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As
Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer)
As Integer
        Private Declare Function BringWindowToTop Lib "CoreDll.dll" (ByVal
hWnd As Integer) As Integer
        Private Declare Function SetForegroundWindow Lib "CoreDll.dll"
(ByVal hWnd As Integer) As Boolean
        '...Constant Declarations.
        'ShowWindow
        Public Enum WindowState
            SW_SHOWNORMAL = 1
            SW_SHOWMINIMIZED = 2
            SW_SHOWMAXIMIZED = 3
        End Enum
        'SetWindowPos
        Private Const SWP_NOSIZE As Int32 = &H1
        Private Const SWP_NOMOVE As Int32 = &H2
        Private Const SWP_HIDEWINDOW As Int32 = &H80
        Private Const SWP_SHOWWINDOW As Int32 = &H40
        Private Const SWP_NOACTIVATE As Int32 = &H10
        Public Enum WindowPos
            vbTopMost = -1&
            vbNotTopMost = -2&
        End Enum
        Public Sub BringToTop(ByVal hWnd As Integer, Optional ByVal Position
As WindowPos = WindowPos.vbNotTopMost)
            On Error Resume Next
            SetWinPosition(hWnd, WindowPos.vbTopMost)
            BringWindowToTop(hWnd)
            SetWinPosition(hWnd, Position)
            ShowWindow(hWnd, WindowState.SW_SHOWMAXIMIZED)
            SetForegroundWindow(hWnd)
        End Sub

        Private Sub SetWinPosition(ByVal hWnd As Integer, ByVal Position As
WindowPos, Optional ByVal fHideWindow As Boolean = False)
            Const wFlagsShow As Integer = SWP_NOMOVE Or SWP_NOSIZE Or
SWP_SHOWWINDOW Or SWP_NOACTIVATE
            Const wFlagsHide As Integer = SWP_NOMOVE Or SWP_NOSIZE Or
SWP_HIDEWINDOW Or SWP_NOACTIVATE
            If Position = WindowPos.vbTopMost Or Position =
WindowPos.vbNotTopMost Then
                If fHideWindow Then
                    SetWindowPos(hWnd, Position, 0, 0, 0, 0, wFlagsHide)
                Else
                    SetWindowPos(hWnd, Position, 0, 0, 0, 0, wFlagsShow)
                End If
            End If
        End Sub

    End Class 'WinMgt
====================================

NOW THE FORM CODE
====================================
    Private win As New Corware.WinMgt
    Private time As New Timer

    Private Sub InitializeTimer()
        AddHandler time.Tick, AddressOf TimeTick
        time.Interval = 1
        time.Enabled() = True
    End Sub

    Private Sub TimeTick(ByVal sender As Object, ByVal e As EventArgs)
        win.BringToTop(Me.hWnd.ToInt32, Corware.WinMgt.WindowPos.vbTopMost)
    End Sub

    Private ReadOnly Property hWnd() As IntPtr
        Get
            Dim hwd As IntPtr
            Capture = True
            hwd = GetCapture()
            Capture = False
            Return hwd
        End Get
    End Property

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        ' Take over the screen.
        Me.WindowState = FormWindowState.Maximized ' Hide the Taskbar.
        InitializeTimer()
    End Sub
====================================

I had to remark out my HotKeys programming to get another -- native -- app
to the top and leave my app running. That's how I could tell that my
form/window was being forced to the top when the timer fired. Now it's time
to do the actual test of powering off the device overnight and see if the
power-on results in the Pocket PC's Start screen quickly (1 ms) disappearing
from view with my app on top. I'm definitely crossing my fingers!



Relevant Pages

  • Re: msflexgrid with ctrl-pgup & ctrl-pgdn
    ... It is still possible for your VB app to ... Timer event, which occurs about ten milliseconds after the key was ... Private Sub Form_Unload ... Private Declare Function SetWindowsHookEx Lib "user32" _ ...
    (comp.lang.basic.visual.misc)
  • Do-Nothing WinForm App Using 4 Threads?
    ... refactoring a multi-threaded GUI app at work and want to be sure it's ... I then added a Windows Timer control to the form and updated a label ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: ldb file
    ... have a timer event set to check the value of the field in the back end to see ... If it indicates the app should be closed, then give the user a 5 ... Is there a way that I can I kick out the ...
    (microsoft.public.access.gettingstarted)
  • RE: ldb file
    ... have a timer event set to check the value of the field in the back end to see ... If it indicates the app should be closed, then give the user a 5 ... quit the application. ...
    (microsoft.public.access.gettingstarted)
  • Re: Disk thrashing with paging file
    ... Your Timer suggestion is a very good idea which I'll try before ... to stop Windows paging out memory. ... user interface app and SQL Server. ... SetProcessWorkingSetSize or VirtualLock APIs make a difference? ...
    (microsoft.public.vb.winapi)