• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 813
  • Last Modified:

Telling Column to Resize

I've searched and found HDN_DIVIDERDBLCLICK, which is exactly what I want to do, but I don't want to have to dbl-click on the column divider in the listctrl myself.  I want it to resize itself on inserting an item.

I have a Dialog, with a listctrl in it, with 3 columns.

How do I post the message (HDN_DIVIDERDBLCLICK);

I've done the following :

  m_pHeaderCtrl = m_ListScript.GetHeaderCtrl();
  int iNumHeaderItems = m_pHeaderCtrl->GetItemCount();
  HDITEM m_HdItem;
  NMHEADER m_NmHeader;
  NMHDR m_NmHdr;
  for(int i=0; i<iNumHeaderItems; i++)
    TRACE("iNumHeaderItems = %d\n",iNumHeaderItems);

    m_pHeaderCtrl->GetItem(i, &m_HdItem);

    m_NmHdr.hwndFrom = GetSafeHwnd();
    m_NmHdr.idFrom   = GetDlgCtrlID();
    m_NmHdr.code     = HDN_DIVIDERDBLCLICK;
    m_NmHeader.hdr.hwndFrom = GetSafeHwnd();
    m_NmHeader.hdr.idFrom   = GetDlgCtrlID();
    m_NmHeader.hdr.code     = HDN_DIVIDERDBLCLICK;
    m_NmHeader.iItem = i;
    m_NmHeader.iButton = 0; // Left Button
    m_NmHeader.pitem = &m_HdItem;

    m_ListScript.SendMessage(WM_NOTIFY, GetDlgCtrlID(), (long)&m_NmHeader);

How do I tell each column to resize itself??? ??? ???
  • 2
  • 2
1 Solution
Can't you simply us SetItem to set a column's width

HDITEM hdi ;
memset( hdi, 0, sizeof hdi ) ;
hdi.mask = HDI_WIDTH
hdi.cxy = 100 ; // new width
pHeaderCtrl->SetItem( i, &hdi ) ;  // i = column to size
jubjubAuthor Commented:
Nope..I don't want to be able to calculate what width I want it to be...
For example, if I enter a string of 1 character, then the column would be really thin, else if I enter a 100 char string, the column will resize, because the new string is bigger, and will be exactly the right size to fit that string.

The only reason i want this, is because I can see that it does it anyway..so how do i tell it to resize, without having to dbl-click on the divider??
Okay I understand the question now.  

The bad news is I don't know the exact answer

Your basic approach you have won't work (HDN_ messages are notifications sent to you to tell you something has happened, sending them to the control will not trigger the action).

If you want to trigger the action, you need to fake a double click message on the divider of the control  (e.g. WM_LBUTTONDBLCLK).  As MS don't give me source for Windows, I don't know exactly which message the header control responds to, but I guess it's WM_LBUTTONDBLCLK.

Here's what I suggest you do.

(a) Try sending WM_LBUTTONDBLCLK to the control

if this doesn't work

(b) get out spy++ and spy on a headercontrol and see what messages are going on when you double click.  Maybe one of the other messages sent to the control triggers the double click behaviour, in which case SendMessage that.

Let me know if this is the answer, or you need more clues on how to figure this out.
Tommy HuiEngineerCommented:
If you're using a CListCtrl, why not use

  CListCtrl::SetColumnWidth(col, LVSCW_AUTOSIZE);

A header control by itself doesn't do it because it does not know what is in each column. A list control does.

jubjubAuthor Commented:
Bravo...that's exactly what I want..


Answer2000 Thanx for your help too, you gave some pointers which I may use later..
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now