SendMessage in VB.NET

S

Scott Gunn

Hi, I'm trying to find out what order the columns in a listview have been
reordered into.

I found some examples on the Internet that use csharp and I tried to convert
them into vb but it always returns false and pCol is empty

Where am I going wrong?

[Declares]

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef
lParam As LV_COLUMN) As Integer

Public Structure LV_COLUMN

Dim mask As UInt32

Dim fmt As Integer

Dim cx As Integer

Dim pszText As String

Dim cchTextMax As Integer

Dim iSubItem As Integer

Dim iImage As Integer

Dim iOrder As Integer

End Structure

Public Const LVM_FIRST = &H1000

Public Const LVM_GETCOLUMN = (LVM_FIRST + 25)



[ Code to get the column info for column 0]

Call SendMessage(lvListView.Handle.ToInt32, LVM_GETCOLUMN, 0, pCol)



Regards

Scott.
 
T

Tom Shelton

Hi, I'm trying to find out what order the columns in a listview have been
reordered into.

I found some examples on the Internet that use csharp and I tried to convert
them into vb but it always returns false and pCol is empty

Where am I going wrong?

[Declares]

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef
lParam As LV_COLUMN) As Integer

Option Strict On ' always a good idea
Option Explicit On ' again, always a good idea

Imports System.Runtime.InteropServices

Public Declare Auto Function SendMessage Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByRef lParam As As LVCOLUMN) As Boolean

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure LVCOLUMN
Pulbic mask As Integer
Public fmt As Integer
Public cx As Integer
Public pszText As String
Public chTextMax As Integer
Public iSubItem As Integer
Public iImage As Integer
Public iOrder As Integer
End Structure

Public Const LVM_FIRST As Integer = &H1000
Public Const LVM_GETCOLUMN As Integer = (LVM_FIRST + 25)
[ Code to get the column info for column 0]

Call SendMessage(lvListView.Handle.ToInt32, LVM_GETCOLUMN, 0, pCol)

Try

If SendMessage (ListView.Handle, LVM_GETCOLUMN, 0, pCol) Then
' DO STUFF
Else
Throw New Win32Exception (Marshal.GetLastWin32Error ())
End If

Catch ex As Win32Exception
MessageBox.Show (ex.Message)
End Try
 
S

Scott Gunn

Hello

If SendMessage (ListView.Handle, LVM_GETCOLUMN, 0, pCol) Then

Returns true but I don't get any information on the column in pCol

Regards
Scott.

Tom Shelton said:
Hi, I'm trying to find out what order the columns in a listview have been
reordered into.

I found some examples on the Internet that use csharp and I tried to convert
them into vb but it always returns false and pCol is empty

Where am I going wrong?

[Declares]

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal
hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef
lParam As LV_COLUMN) As Integer

Option Strict On ' always a good idea
Option Explicit On ' again, always a good idea

Imports System.Runtime.InteropServices

Public Declare Auto Function SendMessage Lib "user32" _
(ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByRef lParam As As LVCOLUMN) As Boolean

<StructLayout (LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure LVCOLUMN
Pulbic mask As Integer
Public fmt As Integer
Public cx As Integer
Public pszText As String
Public chTextMax As Integer
Public iSubItem As Integer
Public iImage As Integer
Public iOrder As Integer
End Structure

Public Const LVM_FIRST As Integer = &H1000
Public Const LVM_GETCOLUMN As Integer = (LVM_FIRST + 25)
[ Code to get the column info for column 0]

Call SendMessage(lvListView.Handle.ToInt32, LVM_GETCOLUMN, 0, pCol)

Try

If SendMessage (ListView.Handle, LVM_GETCOLUMN, 0, pCol) Then
' DO STUFF
Else
Throw New Win32Exception (Marshal.GetLastWin32Error ())
End If

Catch ex As Win32Exception
MessageBox.Show (ex.Message)
End Try


--
Tom Shelton [MVP]
OS Name: Microsoft Windows XP Professional
OS Version: 5.1.2600 Service Pack 2 Build 2600
System Up Time: 1 Days, 23 Hours, 24 Minutes, 18 Seconds
 
T

Tom Shelton

Hello

If SendMessage (ListView.Handle, LVM_GETCOLUMN, 0, pCol) Then

Returns true but I don't get any information on the column in pCol

Regards
Scott.

Scott... Could you post the actuall declarations and code your using to
call this function? The problem is either in your code or my
declaration of the pCol structure... Which is possible :) That's what
you get for converting msdn documentation on the fly and not testing it
:)
 
G

Guest

The

Public Const LVM_GETCOLUMN = (LVM_FIRST + 25)

declaration is incorrect - it should be LVM_FIRST + 95.

You probably want to use the Alias SendMessageW for the SendMessage function
declaration (personally, I would have used the DllImport declaration instead
of the Public Declare ... syntax - but that's just 'cause I'm more used to
that)

Also, what mask are you setting in your LV_COLUMN struct before you pass it
to SendMessage?

- Stenis
 
T

Tom Shelton

The

Public Const LVM_GETCOLUMN = (LVM_FIRST + 25)

declaration is incorrect - it should be LVM_FIRST + 95.

You probably want to use the Alias SendMessageW for the SendMessage function
declaration (personally, I would have used the DllImport declaration instead
of the Public Declare ... syntax - but that's just 'cause I'm more used to
that)

You don't want, or need to alias anything. That's what Auto does for
you, and makes it so that your strings work on both 9x and NT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top