|
-
Feb 9th, 2012, 11:55 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] hwnd help?
Code:
Public Class Form1
Public Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Integer, ByVal x As Int32, ByVal y As Int32) As Int32
Private Declare Function GetForegroundWindow Lib "user32" () As Integer
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer
Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Integer, ByVal hdc As Integer) As Integer
Private Sub MSPaint()
Dim HDC As Integer
Dim hWnd As Integer = GetForegroundWindow()
HDC = (GetWindowDC(hWnd))
If GetPixel(HDC, 400, 400) = &HFFFFFF Then
MsgBox("Correct!")
Else
MsgBox("Fail!")
End If
ReleaseDC(hWnd, HDC)
End Sub
Is there anyway to activate a window first, and then perform getpixel on that active window? Also, is there anyway to use the window title as the hwnd?
For instance: Untitled - Paint instead of GetForegroundWindow.
-
Feb 9th, 2012, 12:06 PM
#2
Re: hwnd help?
Here's a pretty good example:
http://www.codeguru.com/forum/showthread.php?p=1757526
The code is C# but it should be pretty easy to follow.
-
Feb 9th, 2012, 01:06 PM
#3
Thread Starter
Hyperactive Member
Re: hwnd help?
Great link, but I'm having alot of problems converting the c# api to vb.net api.
Can someone post an example code of what i'm trying to do?
-
Feb 9th, 2012, 01:15 PM
#4
-
Feb 9th, 2012, 02:46 PM
#5
Thread Starter
Hyperactive Member
Re: hwnd help?
how can i turn:
Code:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByClass( _
ByVal lpClassName As String, _
ByVal zero As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByCaption( _
ByVal zero As IntPtr, _
ByVal lpWindowName As String) As IntPtr
End Function
into a simple and single line of code:
Code:
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Integer) As Integer
-
Feb 9th, 2012, 04:50 PM
#6
Thread Starter
Hyperactive Member
-
Feb 9th, 2012, 06:46 PM
#7
Thread Starter
Hyperactive Member
Re: hwnd help?
Ok well I've found some info, but still need help.
Code:
Dim WindowName As String
Dim Window As Long
WindowName = "Untitled - Notepad"
Window = FindWindow(vbNullString, WindowName)
AppActivate(WindowName)
This code works great other than the fact that it doesn't restore the window if it's minimized. How can I have it restore the minimized window IF IT'S minimized.
-
Feb 9th, 2012, 07:15 PM
#8
Re: hwnd help?
try this:
vb Code:
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
Dim SW_HIDE As Integer = 0
'Hide the window.
Dim SW_MAXIMIZE As Integer = 3
'Maximize the window.
Dim SW_MINIMIZE As Integer = 6
'Minimize the window.
Dim SW_RESTORE As Integer = 9
'Restore the window (not maximized nor minimized).
Dim SW_SHOW As Integer = 5
'Show the window.
Dim SW_SHOWMAXIMIZED As Integer = 3
'Show the window maximized.
Dim SW_SHOWMINIMIZED As Integer = 2
'Show the window minimized.
Dim SW_SHOWMINNOACTIVE As Integer = 7
'Show the window minimized but do not activate it.
Dim SW_SHOWNA As Integer = 8
'Show the window in its current state but do not activate it.
Dim SW_SHOWNOACTIVATE As Integer = 4
'Show the window in its most recent size and position but do not activate it.
Dim SW_SHOWNORMAL As Integer = 1
'Show the window and activate it (as usual).
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 07:33 PM
#9
Thread Starter
Hyperactive Member
Re: hwnd help?
Thanks paul, but how can i Show "Untitled - Notepad"
Dim SW_SHOWNORMAL As Integer = 1 looks like it does any window, but not the hwnd. i need it to only restore notepad if it's minimized and then activate it.
-
Feb 9th, 2012, 07:41 PM
#10
Re: hwnd help?
you need to use findwindow to get the window handle (hWnd) then use something like this:
vb Code:
Dim WindowName As String
Dim Window As intptr
WindowName = "Untitled - Notepad"
Window = FindWindow(vbNullString, WindowName)
ShowWindow(Window, SW_SHOWNORMAL)
AppActivate(WindowName)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 07:51 PM
#11
Thread Starter
Hyperactive Member
Re: hwnd help?
thanks paul. 
now how can i declare the SW_SHOWNORMAL?
-
Feb 9th, 2012, 08:05 PM
#12
Thread Starter
Hyperactive Member
Re: hwnd help?
Ok so I have:
Dim WindowName As String
Dim Window As IntPtr
WindowName = "Untitled - Notepad
Window = FindWindow(vbNullString, WindowName)
ShowWindow(Window, SW_SHOWNORMAL)
AppActivate(WindowName)
And this in my declarations:
Public Const SW_SHOWNORMAL = 1
And I receive error: Arithmetic Operation Resulted in An Overflow
And this : Window = FindWindow(vbNullString, WindowName)
is highlighted as the error I guess.
-
Feb 9th, 2012, 08:24 PM
#13
Re: hwnd help?
there's a discrepancy in your variable and/or api declarations.
you need to check all of your datatypes
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 08:28 PM
#14
Thread Starter
Hyperactive Member
Re: hwnd help?
ok so I cleared everything that had to do with activating the window.
my declarations are :
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
Public Const SW_SHOWNORMAL = 1
My code is still exactly the same as what you posted. I don't know how I can make this work. everything looks like it falls in place, but it doesn't..
thanks for the help.
-
Feb 9th, 2012, 08:51 PM
#15
Re: hwnd help?
you should turn Option Strict on in all of your projects + it'll help you identify datatype discrepancies before they cause a problem:
vb Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As IntPtr) As Integer
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Integer) As Integer
Public Const SW_SHOWNORMAL As Integer = 1
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 09:08 PM
#16
Thread Starter
Hyperactive Member
Re: hwnd help?
thanks paul! code works great.
-
Feb 9th, 2012, 09:10 PM
#17
Re: hwnd help?
 Originally Posted by Ritzky
This code works great other than the fact that it doesn't restore the window if it's minimized. How can I have it restore the minimized window IF IT'S minimized.
One simple way is to use the IsIconic API to test if a window is minimized, theres also the IsZoomed to tell you if the window is maximized, and if its not either then its in a normal windowstate.
Example..., (see pauls code for ShowWindow)
Code:
<DllImport("user32.dll")> _
Private Shared Function IsZoomed(hWnd As IntPtr) As Boolean
End Function
Private Declare Auto Function IsIconic Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
Window = FindWindow(Nothing, WindowName)
' Always make sure you have a vaild window handle before calling APIs!
If Not Window.Equals(IntPtr.Zero) Then
' Restore window if minimized
If IsIconic(Window) Then
ShowWindow(Window, SW_RESTORE)
End If
End If
Last edited by Edgemeal; Feb 9th, 2012 at 09:16 PM.
Reason: check handle
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|