Question[Request] Checking a windows state.[Solved]

Posts 18 of 8 · Page 1 of 1
[Request] Checking a windows state.[Solved]
I want to check a process out side of visual basic's window state such as Minimized , Maximized , etc.
Hang on, i am lost. can you re-phrase for me?
say I am making a combat arms spammer , that I only want to work when combat arms is opened to its maximized state.
O, this beats me. wait for nextgen1 he will know the answer. he has never not
The code is rather extensive and requires handle , api, etc.

You use P/Invoke Window Placement

MSDN Library offers the code

Quote Originally Posted by msdn
Imports System.Runtime.InteropServices
Imports System.Environment
Imports System.IO
Imports System.ComponentModel

Public Class Form1

Private WithEvents b As New Button

Sub New()
InitializeComponent()
Process.Start(Path.Combine(GetFolderPath(SpecialFo lder.System), "notepad"))
Me.Controls.Add(b)
End Sub

Private Sub b_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles b.Click
' FindWindow is going to find the first instance...
Me.Text = "Notepad is " & NativeMethods.GetWindowState("Untitled - NotePad").ToString
End Sub

End Class


Public Class NativeMethods

<Flags()> _
Private Enum PlacementFlags As UInteger
WPF_SETMINPOSITION = 1
WPF_RESTORETOMAXIMIZED = 2
WPF_ASYNCWINDOWPLACEMENT = 4
End Enum

Private Enum ShowState As UInteger
SW_HIDE = 0 '
SW_SHOWNORMAL = 1 '
SW_SHOWMINIMIZED = 2 '
SW_SHOWMAXIMIZED = 3 '
SW_MAXIMIZE = 3 '
SW_SHOWNOACTIVATE = 4 '
SW_SHOW = 5 '
SW_MINIMIZE = 6 '
SW_SHOWMINNOACTIVE = 7 '
SW_SHOWNA = 8 '
SW_RESTORE = 9 '
End Enum

' friendly ones, just for the common ones...
Public Enum WindowState
Normal
Maximized
Minimized
End Enum

<DllImport("user32.dll")> _
Private Shared Function GetWindowPlacement( _
ByVal hWnd As IntPtr, _
ByRef windowPlacement As WindowPlacement) As Boolean
End Function

<DllImport("user32.dll", EntryPoint:="FindWindowA", CharSet:=CharSet.Ansi)> _
Public Shared Function FindWindow( _
ByVal className As String, _
ByVal WindowName As String) As IntPtr
End Function

Private Structure WindowPlacement
Public Length As UInteger
Public Flags As PlacementFlags
Public ShowCmd As ShowState
Public MinPosition As Point
Public MaxPosition As Point
Public NormalPosition As Rectangle
End Structure

Private Structure NativePoint
Public X As Integer
Public Y As Integer
End Structure

Private Structure NativeRectangle
Public X As Integer
Public Y As Integer
Public Width As Integer
Public Height As Integer
End Structure

Public Shared Function GetWindowState(ByVal WindowCaption As String) As WindowState
' So get the window handle with findwindow. I'm using the caption...
Dim hwnd As IntPtr = FindWindow(Nothing, WindowCaption)
If hwnd = IntPtr.Zero Then Throw New Exception("Window not found")
' Create the structure we pass to the api function.
Dim wp As New WindowPlacement
' Set its size parameter
wp.Length = Convert.ToUInt32(Marshal.SizeOf(GetType(WindowPlac ement)))
' Send it off
If GetWindowPlacement(hwnd, wp) = False Then Throw New Win32Exception
' It has other useful information...
Select Case wp.ShowCmd
Case ShowState.SW_SHOWMAXIMIZED
Return WindowState.Maximized
Case ShowState.SW_SHOWMINIMIZED
Return WindowState.Minimized
Case ShowState.SW_SHOWNORMAL
Return WindowState.Normal
Case Else
' this is just a demo...
Throw New Exception("wasn't a friendly one, windows state is: " & wp.ShowCmd.ToString)
End Select
Return CType(wp.ShowCmd, WindowState)
End Function

End Class
Wow, see never. i told you nextgen would know. he is just a freakin beast at VB
Quote Originally Posted by CoderNever View Post
I want to check a process out side of visual basic's window state such as Minimized , Maximized , etc.
Not as in depth as NextGen1's code but:

Code:
Private Function GetActiveWindowTitle() As String
        Dim MyStr As String
        MyStr = New String(Chr(0), 100)
        GetWindowText(GetForegroundWindow, MyStr, 100)
        MyStr = MyStr.Substring(0, InStr(MyStr, Chr(0)) - 1)
        Return MyStr
    End Function
Use:
Code:
If GetActiveWindowTitle() = "Untitled - Notepad" Then
            'Whatever you want to get done. Auto spam?
        End If
This will make your spammer only work when Combat Arms is the active window. If Combat Arms is maximized, but not Active (or foreground) then the spammer will not work either. So this code works as well.
naww stick to NextGen's code. And /bump.
Posts 18 of 8 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?