Solved

VB.net How to normalize other minimized applications ( I enumerate all open windows)

Posted on 2004-08-09
5
485 Views
Last Modified: 2011-09-20
How to normalize other minimized applications ( I enumerate all open windows)
0
Comment
Question by:Vartana
  • 4
5 Comments
 
LVL 4

Accepted Solution

by:
Daniellus83 earned 500 total points
Comment Utility
You can do it the (very...) hard way (winAPI), but there is a quit simple way using keystrokes:

> You said you enumerate all open windows, I assume you know how may windows are open, lets say NumberX windows are open, then your code will loke like this:

--------------------------------------------------------
Dim n as integer

for n=1 to Number_of_Windows
     
      SendKeys.Send("%{TAB}")

Next
--------------------------------------------------------

(Put this under a button_click....)

Sub6, Daniël

0
 
LVL 4

Expert Comment

by:Daniellus83
Comment Utility

Btw.: you might need you app to wait in between the keystrokes. This is because the keystroke need some time to execute. Use this module like this:

wait(300)   'wait 300 msec


So you code would be:
--------------------------------------------------------
Dim n as integer

for n=1 to Number_of_Windows
     
      SendKeys.Send("%{TAB}")
      Wait(300)

Next
--------------------------------------------------------



The module you have to add to your project:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Module mWait

    Private Declare Function CreateWaitableTimer Lib "kernel32" Alias "CreateWaitableTimerA" (ByVal lpSemaphoreAttributes As Integer, ByVal bManualReset As Integer, ByVal lpName As String) As Integer
    Private Declare Function OpenWaitableTimer Lib "kernel32" Alias "OpenWaitableTimerA" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal lpName As String) As Integer
    'UPGRADE_WARNING: Structure FILETIME may require marshalling attributes to be passed as an argument in this Declare statement. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1050"'
    Private Declare Function SetWaitableTimer Lib "kernel32" (ByVal hTimer As Integer, ByRef lpDueTime As FILETIME, ByVal lPeriod As Integer, ByVal pfnCompletionRoutine As Integer, ByVal lpArgToCompletionRoutine As Integer, ByVal fResume As Integer) As Integer
    Private Declare Function CancelWaitableTimer Lib "kernel32" (ByVal hTimer As Integer) As Object
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Integer, ByVal dwMilliseconds As Integer) As Integer
    Private Declare Function MsgWaitForMultipleObjects Lib "user32" (ByVal nCount As Integer, ByRef pHandles As Integer, ByVal fWaitAll As Integer, ByVal dwMilliseconds As Integer, ByVal dwWakeMask As Integer) As Integer
    ' Wait Module code

    Private Structure FILETIME
        Dim dwLowDateTime As Integer
        Dim dwHighDateTime As Integer
    End Structure

    Private Const WAIT_ABANDONED As Integer = &H80
    Private Const WAIT_ABANDONED_0 As Integer = &H80
    Private Const WAIT_FAILED As Integer = -1
    Private Const WAIT_IO_COMPLETION As Integer = &HC0
    Private Const WAIT_OBJECT_0 As Integer = 0
    Private Const WAIT_OBJECT_1 As Integer = 1
    Private Const WAIT_TIMEOUT As Integer = &H102

    Private Const INFINITE As Short = &HFFFFS
    Private Const ERROR_ALREADY_EXISTS As Short = 183

    Private Const QS_HOTKEY As Integer = &H80S
    Private Const QS_KEY As Integer = &H1S
    Private Const QS_MOUSEBUTTON As Integer = &H4S
    Private Const QS_MOUSEMOVE As Integer = &H2S
    Private Const QS_PAINT As Integer = &H20S
    Private Const QS_POSTMESSAGE As Integer = &H8S
    Private Const QS_SENDMESSAGE As Integer = &H40S
    Private Const QS_TIMER As Integer = &H10S
    Private Const QS_MOUSE As Integer = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
    Private Const QS_INPUT As Integer = (QS_MOUSE Or QS_KEY)
    Private Const QS_ALLEVENTS As Integer = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)
    Private Const QS_ALLINPUT As Integer = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)

    Public Sub Wait(ByRef lNumberOfSeconds As Integer)
        Dim ft As FILETIME
        Dim lBusy As Integer
        Dim lRet As Integer
        Dim dblDelay As Double
        Dim dblDelayLow As Double
        Dim dblUnits As Double
        Dim hTimer As Integer

        hTimer = CreateWaitableTimer(0, True, VB6.GetExeName() & "Timer")

        If Err.LastDllError = ERROR_ALREADY_EXISTS Then
            ' If the timer already exists, it does not hurt to open it
            ' as long as the person who is trying to open it has the
            ' proper access rights.
        Else
            ft.dwLowDateTime = -1
            ft.dwHighDateTime = -1
            lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, 0)
        End If

        ' Convert the Units to nanoseconds.
        dblUnits = CDbl(&H10000) * CDbl(&H10000)
        dblDelay = CDbl(lNumberOfSeconds) * 1000 * 10   '3 Nulletjes weggehaald voor het aantal 1000dersten seconden

        ' By setting the high/low time to a negative number, it tells
        ' the Wait (in SetWaitableTimer) to use an offset time as
        ' opposed to a hardcoded time. If it were positive, it would
        ' try to convert the value to GMT.
        ft.dwHighDateTime = -CInt(dblDelay / dblUnits) - 1
        dblDelayLow = -dblUnits * (dblDelay / dblUnits - Fix(dblDelay / dblUnits))

        If dblDelayLow < CDbl(&H80000000) Then
            ' &H80000000 is MAX_LONG, so you are just making sure
            ' that you don't overflow when you try to stick it into
            ' the FILETIME structure.
            dblDelayLow = dblUnits + dblDelayLow
        End If

        ft.dwLowDateTime = CInt(dblDelayLow)
        lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, False)

        Do
            ' QS_ALLINPUT means that MsgWaitForMultipleObjects will
            ' return every time the thread in which it is running gets
            ' a message. If you wanted to handle messages in here you could,
            ' but by calling Doevents you are letting DefWindowProc
            ' do its normal windows message handling---Like DDE, etc.
            lBusy = MsgWaitForMultipleObjects(1, hTimer, False, INFINITE, QS_ALLINPUT)
            System.Windows.Forms.Application.DoEvents()
        Loop Until lBusy = WAIT_OBJECT_0

        ' Close the handles when you are done with them.
        CloseHandle(hTimer)

    End Sub

End Module
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

0
 
LVL 1

Author Comment

by:Vartana
Comment Utility
     SendKeys.Send("%{TAB}") how do i say which window or number  to goto ?
0
 
LVL 4

Expert Comment

by:Daniellus83
Comment Utility
Hello Vartana,

Let us say you have 8 windows open, and the 1rst is your own program, the code to normalize all windows is in this case:

(Here you return to your own window):

--------------------------------------------------------
        Dim n As Integer
        Dim tabs As String

        For n = 2 To 9
            tabs = tabs & "{tab}"
        Next

        SendKeys.Send("%(" & tabs & ")")        '>>> In this case the command is:  SendKeys.Send("%({tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab})")
--------------------------------------------------------

NOTE: Let n=2 !!  Since our own program is always window 1, you can better start off with n=2.  The 9 means: 9-8=1 You return to window 1.


>> IF you want to go to window 2:

--------------------------------------------------------
        Dim n As Integer
        Dim tabs As String

        For n = 2 To 2
            tabs = tabs & "{tab}"
        Next

        SendKeys.Send("%(" & tabs & ")")
--------------------------------------------------------

This means you execute the ALT-TAB commando once.


>> AND if you want to go to window 6:

--------------------------------------------------------
        Dim n As Integer
        Dim tabs As String

        For n = 2 To 6
            tabs = tabs & "{tab}"
        Next

        SendKeys.Send("%(" & tabs & ")")
--------------------------------------------------------

But if you want to go to window 6, also this code works:

>> AND if you want to go to window 6:

--------------------------------------------------------
        Dim n As Integer
        Dim tabs As String

        For n = 2 To 14
            tabs = tabs & "{tab}"
        Next

        SendKeys.Send("%(" & tabs & ")")
--------------------------------------------------------

Since: 14-8=6

0
 
LVL 4

Expert Comment

by:Daniellus83
Comment Utility
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now