How to use GetWindowLong properly in VB.NET.

  • Thread starter Thread starter Joe Johnston
  • Start date Start date
J

Joe Johnston

If there is actually a framework answer to this I am all
ears...

I have the window handle I am getting the consts from my
c++ header winuser.h. No matter what window I check I
_always_ get the following;
1 max true
2 min False
3 HasBorder False
4 titlebar False

I have included the declaration and the functions below.

Private Declare Function GetWindowLong Lib "user32"
Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex
As Long) As Long
Private Const WS_BORDER = &H800000
Private Const WS_CAPTION = &HC00000
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const GWL_STYLE = (-16)

Friend Function HasMax(ByVal hwnd As Long) As
Boolean
Dim Style As Long
Style = GetWindowLong(hwnd, GWL_STYLE)
If ((Style And WS_MAXIMIZEBOX) = WS_MAXIMIZEBOX)
= True Then
HasMax = True
Else
HasMax = False
End If
End Function

Friend Function HasMin(ByVal hwnd As Long) As Boolean
Dim Style As Long
Style = GetWindowLong(hwnd, GWL_STYLE)
If ((Style And WS_MINIMIZEBOX) = WS_MINIMIZEBOX)
= True Then
HasMin = True
Else
HasMin = False
End If
End Function

Friend Function HasTitleBar(ByVal hwnd As Long) As
Boolean
Dim Style As Long
Style = GetWindowLong(hwnd, GWL_STYLE)
If ((Style And WS_DLGFRAME) = WS_DLGFRAME = True)
Then
HasTitleBar = True
Else
HasTitleBar = False
End If
End Function

Friend Function HasBorder(ByVal hwnd As Long) As
Boolean
Dim Style As Long
Style = GetWindowLong(hwnd, GWL_STYLE)
If ((Style And WS_BORDER) = WS_BORDER = True) Then
HasBorder = True
Else
HasBorder = False
End If
End Function
 
Private Declare Function GetWindowLong Lib "user32"
Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex
As Long) As Long


<DllImport("User32.dll", EntryPoint:="GetWindowLong")> _
Private Function GetWindowLong(ByVal HWND As IntPtr, ByVal Index as Integer) as Integer
End Function


--Kresimir
http://www.inet.hr/~kkrecak
 
Thanks Kresimir, This is a superior declaration and I
have implimented the InteropServices.

However, The call still yields the same results which
lead me to believe the I am calling the function wrong or
there is some detail I am leaving out.

My thanks in advance,
Joe Johnston


-----Original Message-----



<DllImport("User32.dll", EntryPoint:="GetWindowLong")> _
Private Function GetWindowLong(ByVal HWND As IntPtr,
ByVal Index as Integer) as Integer
 
Thanks Kresimir, This is a superior declaration and I
have implimented the InteropServices.

However, The call still yields the same results which
lead me to believe the I am calling the function wrong or
there is some detail I am leaving out.


this seems to work:


Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Environment

Public Class Form1
Inherits Form

Private components As IContainer
Private WS_OVERLAPPED As Integer = &H0
Private WS_POPUP As Integer = &H80000000
Private WS_CHILD As Integer = &H40000000
Private WS_MINIMIZE As Integer = &H20000000
Private WS_VISIBLE As Integer = &H10000000
Private WS_DISABLED As Integer = &H8000000
Private WS_CLIPSIBLINGS As Integer = &H4000000
Private WS_CLIPCHILDREN As Integer = &H2000000
Private WS_MAXIMIZE As Integer = &H1000000
Private WS_CAPTION As Integer = &HC00000 '/* WS_BORDER | WS_DLGFRAME */
Private WS_BORDER As Integer = &H800000
Private WS_DLGFRAME As Integer = &H400000
Private WS_VSCROLL As Integer = &H200000
Private WS_HSCROLL As Integer = &H100000
Private WS_SYSMENU As Integer = &H80000
Private WS_THICKFRAME As Integer = &H40000
Private WS_GROUP As Integer = &H20000
Private WS_TABSTOP As Integer = &H10000
Private WS_MINIMIZEBOX As Integer = &H20000
Private WS_MAXIMIZEBOX As Integer = &H10000

Public Sub New()
MyBase.New()
InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

<DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
End Sub

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
Dim ret As Integer = User32DLL.GetWindowLong(Me.Handle, -16)
Dim s As String = String.Empty
If (ret And WS_BORDER) Then s &= "WS_BORDER" & NewLine
If (ret And WS_CAPTION) Then s &= "WS_CAPTION" & NewLine
If (ret And WS_CHILD) Then s &= "WS_CHILD" & NewLine
If (ret And WS_CLIPCHILDREN) Then s &= "WS_CLIPCHILDREN" & NewLine
If (ret And WS_CLIPSIBLINGS) Then s &= "WS_CLIPSIBLINGS" & NewLine
If (ret And WS_DISABLED) Then s &= "WS_DISABLED" & NewLine
If (ret And WS_DLGFRAME) Then s &= "WS_DLGFRAME" & NewLine
If (ret And WS_GROUP) Then s &= "WS_GROUP" & NewLine
If (ret And WS_HSCROLL) Then s &= "WS_HSCROLL" & NewLine
If (ret And WS_MAXIMIZE) Then s &= "WS_MAXIMIZE" & NewLine
If (ret And WS_MAXIMIZEBOX) Then s &= "WS_MAXIMIZEBOX" & NewLine
If (ret And WS_MINIMIZE) Then s &= "WS_MINIMIZE" & NewLine
If (ret And WS_MINIMIZEBOX) Then s &= "WS_MINIMIZEBOX" & NewLine
If (ret And WS_OVERLAPPED) Then s &= "WS_OVERLAPPED" & NewLine
If (ret And WS_POPUP) Then s &= "WS_POPUP" & NewLine
If (ret And WS_SYSMENU) Then s &= "WS_SYSMENU" & NewLine
If (ret And WS_TABSTOP) Then s &= "WS_TABSTOP" & NewLine
If (ret And WS_THICKFRAME) Then s &= "WS_THICKFRAME" & NewLine
If (ret And WS_VISIBLE) Then s &= "WS_VISIBLE" & NewLine
If (ret And WS_VSCROLL) Then s &= "WS_VSCROLL" & NewLine
MessageBox.Show(s, "Styles")
End Sub

End Class

Friend Class User32DLL
<DllImport("User32.dll", EntryPoint:="GetWindowLong")> _
Friend Shared Function GetWindowLong(ByVal HWND As IntPtr, ByVal Index As Integer) As Integer
End Function
End Class


--Kresimir
http://www.inet.hr/~kkrecak
 
This is solved now. The problem was my cast of long
in .NET I needed to convert my window handle which came
back as a long (bad in .NET integer better) to a intPtr.
Me.Handle is already good but a .NET Long that = 126507
would throw a wrench into the bitmask. Thank you
sincerly for your efforts.

Joe Johnston

-----Original Message-----



this seems to work:


Imports System.Runtime.InteropServices
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Environment

Public Class Form1
Inherits Form

Private components As IContainer
Private WS_OVERLAPPED As Integer = &H0
Private WS_POPUP As Integer = &H80000000
Private WS_CHILD As Integer = &H40000000
Private WS_MINIMIZE As Integer = &H20000000
Private WS_VISIBLE As Integer = &H10000000
Private WS_DISABLED As Integer = &H8000000
Private WS_CLIPSIBLINGS As Integer = &H4000000
Private WS_CLIPCHILDREN As Integer = &H2000000
Private WS_MAXIMIZE As Integer = &H1000000
Private WS_CAPTION As Integer =
&HC00000 '/* WS_BORDER | WS_DLGFRAME */
Private WS_BORDER As Integer = &H800000
Private WS_DLGFRAME As Integer = &H400000
Private WS_VSCROLL As Integer = &H200000
Private WS_HSCROLL As Integer = &H100000
Private WS_SYSMENU As Integer = &H80000
Private WS_THICKFRAME As Integer = &H40000
Private WS_GROUP As Integer = &H20000
Private WS_TABSTOP As Integer = &H10000
Private WS_MINIMIZEBOX As Integer = &H20000
Private WS_MAXIMIZEBOX As Integer = &H10000

Public Sub New()
MyBase.New()
InitializeComponent()
End Sub

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

<DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
Me.Text = "Form1"
End Sub

Private Sub Form1_Click(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.Click
Dim ret As Integer = User32DLL.GetWindowLong (Me.Handle, -16)
Dim s As String = String.Empty
If (ret And WS_BORDER) Then s &= "WS_BORDER" & NewLine
If (ret And WS_CAPTION) Then s &= "WS_CAPTION" & NewLine
If (ret And WS_CHILD) Then s &= "WS_CHILD" & NewLine
If (ret And WS_CLIPCHILDREN) Then s &= "WS_CLIPCHILDREN" & NewLine
If (ret And WS_CLIPSIBLINGS) Then s &= "WS_CLIPSIBLINGS" & NewLine
If (ret And WS_DISABLED) Then s &= "WS_DISABLED" & NewLine
If (ret And WS_DLGFRAME) Then s &= "WS_DLGFRAME" & NewLine
If (ret And WS_GROUP) Then s &= "WS_GROUP" & NewLine
If (ret And WS_HSCROLL) Then s &= "WS_HSCROLL" & NewLine
If (ret And WS_MAXIMIZE) Then s &= "WS_MAXIMIZE" & NewLine
If (ret And WS_MAXIMIZEBOX) Then s &= "WS_MAXIMIZEBOX" & NewLine
If (ret And WS_MINIMIZE) Then s &= "WS_MINIMIZE" & NewLine
If (ret And WS_MINIMIZEBOX) Then s &= "WS_MINIMIZEBOX" & NewLine
If (ret And WS_OVERLAPPED) Then s &= "WS_OVERLAPPED" & NewLine
If (ret And WS_POPUP) Then s &= "WS_POPUP" & NewLine
If (ret And WS_SYSMENU) Then s &= "WS_SYSMENU" & NewLine
If (ret And WS_TABSTOP) Then s &= "WS_TABSTOP" & NewLine
If (ret And WS_THICKFRAME) Then s &= "WS_THICKFRAME" & NewLine
If (ret And WS_VISIBLE) Then s &= "WS_VISIBLE" & NewLine
If (ret And WS_VSCROLL) Then s &= "WS_VSCROLL" & NewLine
MessageBox.Show(s, "Styles")
End Sub

End Class

Friend Class User32DLL
<DllImport("User32.dll", EntryPoint:="GetWindowLong")
_
Friend Shared Function GetWindowLong(ByVal HWND As
IntPtr, ByVal Index As Integer) As Integer
 
Back
Top