Problem with HDM_GETITEM

Posted on 2000-05-18
Last Modified: 2008-03-06
The following code returnes 0, why?

Debug.Print SendMessage(hHeader, HDM_GETITEM, 2, HDI)

This is my Type def:
Public Type HD_ITEM
   mask        As Long
   cxy         As Long
   pszText     As String
   hbm         As Long
   cchTextMax  As Long
   fmt         As Long
   lParam      As Long
   iImage      As Long
   iOrder      As Long
End Type

This are my constants:
Public Const HDI_WIDTH        As Long = &H1
Public Const HDI_HEIGHT       As Long = HDI_WIDTH
Public Const HDI_TEXT         As Long = &H2
Public Const HDI_FORMAT       As Long = &H4
Public Const HDI_LPARAM       As Long = &H8
Public Const HDI_BITMAP       As Long = &H10
Public Const HDI_IMAGE        As Long = &H20
Public Const HDI_DI_SETITEM   As Long = &H40
Public Const HDI_ORDER        As Long = &H80

I know that the handle is corect. And my guess is that my type def or my constant is wrong...
Question by:Olli083097
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
LVL 15

Expert Comment

ID: 2821181
>Debug.Print SendMessage(hHeader, HDM_GETITEM, 2, HDI)

You don't need to print return value, but some property of your structure

  HDI.mask = HDI_WIDTH
  ret = SendMessage(hHeader, HDM_GETITEM, 2, HDI)
  Debug.Print HDI.cxy & " " & HDI.lParam

Author Comment

ID: 2821218
I know that but all the properties is 0 and "". And the SendMessage should return True?!?
LVL 15

Expert Comment

ID: 2821287
If your header has 2 columns, the second column has index 1.
  ret = SendMessage(hHeader, HDM_GETITEM, 1, HDI)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 2821294
I know! It has 5 columns...

Author Comment

ID: 2821298
BTW: Have you checked my type def and my constant?
LVL 15

Expert Comment

ID: 2821320
>Have you checked my type def and my constant?
No. I don't use header control, I'm just looking at a similar code, but with other structure.

What is your SendMessage declarations?

Author Comment

ID: 2821336
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd&, ByVal wMsg&, wParam As Any, lParam As Any) As Long

It works when I send othe messages!

Do you know where I can get the type def and the constats?
LVL 15

Accepted Solution

ameba earned 50 total points
ID: 2821611
Found something. I tested and everything works with VB5 Listview.
You have your constants OK, here are some you didn't mention:

Const HDM_FIRST = &H1200
Const LVM_FIRST = &H1000

Private Declare Function SendMessageAny Lib "user32" _
    Alias "SendMessageA" _
    (ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long
Private Declare Function SendMessageLong _
    Lib "user32" Alias "SendMessageA" _
    (ByVal hWnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

Private Sub Form_Click()
    Dim r As Long
    Dim hHeader As Long
    Dim HD As HD_ITEM
    hHeader = SendMessageLong(ListView1.hWnd, LVM_GETHEADER, 0, 0)
    With HD
        .mask = HDI_TEXT
        .cchTextMax = 255
        .pszText = Space(255) ' prepare buffer
        r = SendMessageAny(hHeader, HDM_GETITEM, 1, HD)  ' second column
        ' remove unneeded charavcters
        r = InStr(.pszText, Chr(0))
        MsgBox "Column text = '" & Left(.pszText, r - 1) & "'"
        .mask = HDI_WIDTH
        r = SendMessageAny(hHeader, HDM_GETITEM, 1, HD)  ' second column
        MsgBox "Width = " & .cxy & " pts"
    End With
End Sub

Author Comment

ID: 2824515
I have found the error, thanks to you!
The entire problem was in my SendMessage decleration: "wParam As Any"
it shuld be "As Long"

That's what happen when I get the decleration for other places than the MS textfile that comes with API Viewer...


Author Comment

ID: 2824602
How about the HDM_SETITEM? Can't quite make it work...
LVL 15

Expert Comment

ID: 2825018
Thanks for the points

SendMessage can be used with different types of lParam and wParam parameters, which can be passed ByVal or ByRef. The best practice is to include specific declaration, e.g. SendMessageString, SendMessagePT, SendMessageRect.


    With HD
        .mask = HDI_IMAGE Or HDI_FORMAT
        .fmt = HDF_LEFT Or HDF_STRING
        .pszText = ListView1.ColumnHeaders(colNo + 1).Text
        .iImage = imgIconNo
    End With
    r = SendMessageAny(hHeader, HDM_SETITEM, colNo, HD)

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TT Auto Dashboard 13 101
Help me. 3 70
Fastest way to find and count same items VB6 16 47
odbc driver manager data source name not found and no default driver specified 9 33
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 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