Solved

listbox scrollbar

Posted on 2001-06-18
10
598 Views
Last Modified: 2013-11-20
Hey Guys,
i want to create a list box with Horizontal scroll bar.
how do i do that ?

Shay
0
Comment
Question by:Shay050799
10 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 6202221
go to your listbox's properties and check the "Horizontal scroll" checkbox, simple as that.

good luck
0
 
LVL 3

Author Comment

by:Shay050799
ID: 6202232
have you tried that ?????
if you had...u know that it doesn't work !!!

next ?
Shay
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 6202259
or use m_myList.ModifyStyle(0, WS_HSCROLL);
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6202405
What you're looking for is all here:

http://journal.iftech.com/articles/9905_campbell_clistbox/

please be nicer to sedgwick though -

>have you tried that ?????
>if you had...u know that it doesn't work !!!
>
>next ?

is perhaps a bit rude?
0
 
LVL 4

Accepted Solution

by:
captainkirk earned 50 total points
ID: 6203553
Here's some code in the docs to derive a class from CListBox:


The .h code:


//****************************************************************************
// File:
//
//     HLB.H
//
// Purpose:
//
//     Provides the declaration for the CHorzListBox class.
//
// Development Team:
//
//     James Rhodes
//
// Written by Microsoft Product Support Services, Languages Developer Support
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//****************************************************************************

#ifndef HLB_H
#define HLB_H

#include <afxtempl.h>

typedef CArray<int,int> CIntArray;

/////////////////////////////////////////////////////////////////////////////
// CHorzListBox window

class CHorzListBox : public CListBox
{
// Construction
public:
      CHorzListBox();

// Attributes
protected:
      BOOL m_bLocked;
      CIntArray m_arrExtents;
      int m_nLongestExtent;
      int m_nTabStops;
      int* m_lpTabStops;

// Operations
public:
      void LockHExtentUpdate();
      void UnlockHExtentUpdate();
      void UpdateHExtent();

protected:
      void InsertNewExtent(int nItem, LPCTSTR lpszStr);
      void InsertNewExtent(int nItem, LPCTSTR lpszStr, CDC* pDC);
      void InitTabStops();

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CHorzListBox)
      //}}AFX_VIRTUAL

// Implementation
public:
      virtual ~CHorzListBox();

      // Generated message map functions
protected:
      //{{AFX_MSG(CHorzListBox)
      //}}AFX_MSG
      afx_msg LRESULT OnAddString(WPARAM wParam, LPARAM lParam);
      afx_msg LRESULT OnInsertString(WPARAM wParam, LPARAM lParam);
      afx_msg LRESULT OnDeleteString(WPARAM wParam, LPARAM lParam);
      afx_msg LRESULT OnSetTabStops(WPARAM wParam, LPARAM lParam);
      DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////
#endif



The .cpp code:

//****************************************************************************
// File:
//
//     HLB.CPP
//
// Purpose:
//
//     Implementation file for class CHorzListBox.
//
// Functions:
//
//  CHorzListBox::CHorzListBox          Constructs a CHorzListBox
//  CHorzListBox::~CHorzListBox         Destructor
//  CHorzListBox::LockHExtentUpdate     Pauses automatic updating of the
//                                      horizontal scroll bar
//  CHorzListBox::UnlockHExtentUpdate   Restores automatic updating of
//                                      the horizontal scroll bar
//  CHorzListBox::UpdateHExtent         Updates the horizontal scroll bar
//                                      extent after LockHExtentUpdate has
//                                      been called
//  CHorzListBox::InsertNewExtent       Internal utility function used to
//                                      maintain the extent array
//  CHorzListBox::InitTabStops          Internal tab stop init function
//  CHorzListBox::OnAddString           Intercepts LB_ADDSTRING msg
//  CHorzListBox::OnInsertString        Intercepts LB_INSERTSTRING msg
//  CHorzListBox::OnDeleteString        Intercepts LB_DELETESTRING msg
//  CHorzListBox::OnSetTabStops         Intercepts LB_SETTABSTOPS msg
//
// Development Team:
//
//     James Rhodes
//
// Written by Microsoft Product Support Services, Languages Developer Support
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//****************************************************************************

#include "stdafx.h"
#include "HLISTBOX.h"
#include "HLB.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHorzListBox

//***********************************************************************
// Function:
//
//     CHorzListBox::CHorzListBox()
//
// Purpose:
//
//     Constructs a CHorzListBox object
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
CHorzListBox::CHorzListBox()
{
      m_bLocked = FALSE;      // start out in auto mode
      m_nLongestExtent = 0;   // tracks longest extent, initially 0
      m_nTabStops = 0;        // no tab stops
      m_lpTabStops = NULL;    // array of tab stops
}

//***********************************************************************
// Function:
//
//     CHorzListBox::~CHorzListBox()
//
// Purpose:
//
//     Destructs a CHorzListBox object
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
CHorzListBox::~CHorzListBox()
{
      if (m_lpTabStops != NULL)
            delete m_lpTabStops;
}

//***********************************************************************
// Function:
//
//     CHorzListBox::LockHExtentUpdate()
//
// Purpose:
//
//     Stops auto updating of horizontal extent
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
void CHorzListBox::LockHExtentUpdate()
{
      m_bLocked = TRUE;
}

//***********************************************************************
// Function:
//
//     CHorzListBox::UnlockHExtentUpdate()
//
// Purpose:
//
//     Turns auto updating back on
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
void CHorzListBox::UnlockHExtentUpdate()
{
      m_bLocked = FALSE;
}

//***********************************************************************
// Function:
//
//     CHorzListBox::UpdateHExtent()
//
// Purpose:
//
//     Updates horizontal extent when auto updating has been turned off
//     for some period.
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
void CHorzListBox::UpdateHExtent()
{
      m_arrExtents.RemoveAll();
      m_nLongestExtent = 0;
      int nCount = GetCount();

      CDC* pDC = GetDC();
      HFONT hFont = (HFONT)SendMessage(WM_GETFONT);
      CFont *pFont = CFont::FromHandle(hFont);
      ASSERT(pFont);
      CFont* pPrevFont = pDC->SelectObject(pFont);
      CString str;

      for(int i=0; i<nCount; i++)
      {
            GetText(i, str);
            InsertNewExtent(i, str, pDC);
      }

      SetHorizontalExtent(m_nLongestExtent);
      pDC->SelectObject(pPrevFont);
      ReleaseDC(pDC);
}

//***********************************************************************
// Function:
//
//     CHorzListBox::InsertNewExtent() PROTECTED
//
// Purpose:
//
//     Used to update the extent array when a new item is added
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
void CHorzListBox::InsertNewExtent(int nItem, LPCTSTR lpszStr, CDC* pDC)
{
      if (NULL == m_lpTabStops)
            InitTabStops();

    CSize newExtent;
    if (GetStyle() & LBS_USETABSTOPS)
          newExtent = pDC->GetTabbedTextExtent(lpszStr, strlen(lpszStr),
                m_nTabStops, m_lpTabStops);
    else
        newExtent = pDC->GetTextExtent(lpszStr, strlen(lpszStr));

      newExtent.cx += 6;
      m_arrExtents.InsertAt(nItem, newExtent.cx);

      if (newExtent.cx > m_nLongestExtent)
      {
            m_nLongestExtent = newExtent.cx;
      }
}

void CHorzListBox::InsertNewExtent(int nItem, LPCTSTR lpszStr)
{
      if (m_bLocked)
            return;

      CDC* pDC = GetDC();
      HFONT hFont = (HFONT)SendMessage(WM_GETFONT);
      CFont *pFont = CFont::FromHandle(hFont);
      ASSERT(pFont);
      CFont* pPrevFont = pDC->SelectObject(pFont);
      InsertNewExtent(nItem, lpszStr, pDC);
      SetHorizontalExtent(m_nLongestExtent);
      pDC->SelectObject(pPrevFont);
      ReleaseDC(pDC);
}

//***********************************************************************
// Function:
//
//     CHorzListBox::InitTabStops() PROTECTED
//
// Purpose:
//
//     Initializes tab stops
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
void CHorzListBox::InitTabStops()
{
      int nDefault = 2;
      SetTabStops(1, &nDefault);
}

BEGIN_MESSAGE_MAP(CHorzListBox, CListBox)
      //{{AFX_MSG_MAP(CHorzListBox)
      //}}AFX_MSG_MAP
      ON_MESSAGE( LB_ADDSTRING, OnAddString )
      ON_MESSAGE( LB_INSERTSTRING, OnInsertString )
      ON_MESSAGE( LB_DELETESTRING, OnDeleteString )
      ON_MESSAGE( LB_SETTABSTOPS, OnSetTabStops )
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHorzListBox message handlers

//***********************************************************************
// Function:
//
//     CHorzListBox::OnAddString()
//
// Purpose:
//
//     Intercepts the LB_ADDSTRING message to update the horizontal extent
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
LRESULT CHorzListBox::OnAddString(WPARAM, LPARAM lParam)
{
      LRESULT lResult = Default();
      if (LB_ERR == lResult || LB_ERRSPACE == lResult) // check for error first!
            return lResult;

      InsertNewExtent(lResult, (LPCTSTR)lParam);
      return lResult;
}

//***********************************************************************
// Function:
//
//     CHorzListBox::OnInsertString()
//
// Purpose:
//
//     Intercepts the LB_INSERTSTRING message to update the horizontal extent
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
LRESULT CHorzListBox::OnInsertString(WPARAM, LPARAM lParam)
{
      LRESULT lResult = Default();
      if (LB_ERR == lResult || LB_ERRSPACE == lResult) // check for error first!
            return lResult;

      InsertNewExtent(lResult, (LPCTSTR)lParam);
      return lResult;
}

//***********************************************************************
// Function:
//
//     CHorzListBox::OnDeleteString()
//
// Purpose:
//
//     Intercepts the LB_DELETESTRING message to update the horizontal extent
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
LRESULT CHorzListBox::OnDeleteString(WPARAM wParam, LPARAM)
{
      LRESULT lResult = Default();
      if (LB_ERR == lResult) // check for error first!
            return lResult;

      if (m_bLocked) // don't do anything if locked
            return lResult;

      int nExtent = m_arrExtents[wParam];
      m_arrExtents.RemoveAt(wParam);
      if (nExtent >= m_nLongestExtent)
      {
            m_nLongestExtent = 0;
            for(int i = 0; i<lResult; i++)
            {
                  if (m_arrExtents[i] > m_nLongestExtent)
                        m_nLongestExtent = m_arrExtents[i];
            }
      }
      SetHorizontalExtent(m_nLongestExtent);

      return lResult;
}

//***********************************************************************
// Function:
//
//     CHorzListBox::OnSetTabStops()
//
// Purpose:
//
//     Intercepts the LB_SETTABSTOPS message to update tab stop array
//
// History:
//
//   Date   Comment                                           Initials
// ======== ================================================= ========
//  1/31/96 Created                                             JMR
//***********************************************************************
LRESULT CHorzListBox::OnSetTabStops(WPARAM wParam, LPARAM lParam)
{
      LRESULT lResult = Default();
      if (!lResult)
            return lResult;

      m_nTabStops = wParam;

      if (NULL != m_lpTabStops)
      {
            delete [] m_lpTabStops;
            m_lpTabStops = NULL;
      }
      
      if (m_nTabStops > 0)
      {
            m_lpTabStops = new int[m_nTabStops];
            memcpy(m_lpTabStops, (void*)lParam, m_nTabStops * sizeof(int));

            CDC* pDC = GetDC();
            HFONT hFont = (HFONT)SendMessage(WM_GETFONT);
            CFont *pFont = CFont::FromHandle(hFont);
            ASSERT(pFont);
            CFont* pPrevFont = pDC->SelectObject(pFont);
            CSize size;

            GetTextExtentPoint32(pDC->GetSafeHdc(),
                  _T("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
                  52, &size);

            pDC->SelectObject(pPrevFont);
            ReleaseDC(pDC);

            int aveCharWidth = (size.cx/26 +1)/2;

            for(int i=0; i<m_nTabStops; i++)
                  m_lpTabStops[i] = (m_lpTabStops[i] * aveCharWidth + 2)/4;
      }

      if (!m_bLocked)
            UpdateHExtent(); // tabs changed, recalc everything!

      return lResult;
}
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Expert Comment

by:BVadim
ID: 6203554
There is part of article from MSDN about
horizontal scroll bar in the list box.

*********************************************************
Microsoft Windows version 3.0 is the first version that recognizes the WS_HSCROLL window style for list boxes. This style adds a horizontal scroll bar to the list box. However, the scroll bar does not appear automatically when a string that is too long to display in a list box is added to that list box. Similarly, when the last string longer than the list box is removed, Windows will not hide the scroll bar. This behavior is different from that of the vertical scroll bar of a list box, which is added and removed as needed.

An application must maintain the width, in pixels, of each string in the list box. The LB_SETHORIZONTALEXTENT list box message controls both the scrolling range and the visibility of a horizontal scroll bar. In this article, the term "extent" is defined to be the width of an object in screen pixels. Each string has an extent as does the list box itself.
***********************************************************
0
 

Expert Comment

by:FlipFlop
ID: 6511912
Hi Shay,

Yep this one cought me out too, so this should save you
falling into the same pit as I did!

Its a two step procedure.

Step 1:
Go to your listbox's properties and check the "Horizontal scroll" checkbox. (Yes you do need this!)

Step 2:
In you ListBox class add the following to one of your
member functions, I usually put it in my OnInitDialog()

// Set up horizontal scroll bar
CString          str;
CSize          sz;
int          dx = 0;

// Find the widest string in your listbox
// Where m_pListBox is the member variable
// attached to you ListBox.  Alternatively
// you can replace it with a pointer like
// extern CListBox* pmyListBox and use
// that instead
for (i=0;i<m_pListBox.GetCount();i++)
{
     m_pNum.GetText(i, str);
     sz = m_pListBox.GetDC()->GetTextExtent(str);
     if (sz.cx > dx)
          dx = sz.cx;
}

// Set the horizontal scroll range, this is where
// all the action is
m_pNum.SetHorizontalExtent(dx);


If your listbox items are always of  the same width, and
you know this value beforehand you can just make a call
to SetHorizontalExtent(nWidth) to set the scroll range.


Hope this helps, FlipFlop








0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6799522
Shay----->  You have asked 95 questions at this site, and only awarded 53 of them....  this is totally against the Guidelines of this site.  Please return to them and update/finalize them this week.  The list is below.  Of these 53 awarded, there are 7 questions that are -1 user id problems which we will need to manually correct.  Please accept the first correct response in each of them and award the experts who helped you with the deserved "A" grade, or comments if less than an A as to why.  If you need our help to special handle any of these, let us know that as well via comment here.  If these are not finalized this week, I will contact Administration for further action.  Please click the HELP DESK link on the left for the rules of engagement here (Guidelines, Member Agreement and Question/Answer process).

I also noticed that a number of your questions are less than 50 points, which is the recommended value for EASY questions, with 300 points the maximum.  To draw additional attention, you can always increase their values without our assistance.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20229485
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20123800
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20121605
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20120473
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20110164
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20109749
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=20077855
http://www.experts-exchange.com/jsp/qShow.jsp?ta=wince&qid=12019739 November, 2000, zero comments added, 5 points.
http://www.experts-exchange.com/jsp/qShow.jsp?ta=winprog&qid=11169491 November, 2000 ZERO points, comments added
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20135373
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20133207
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20115081
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20103624
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20102120
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20100568
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20088761
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20076668
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=11536619 October, 2000 - 15 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=12014539  November, 2000
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11990378      "
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=11863978      "
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20223052
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20220148
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20149200  July, 2001 - 5 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20148518    "      - 10 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20138848
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20137234
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20132473
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20132221
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20122930  May, 2001 5 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20121466  May, 2001 0 points
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20094887
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20094102
http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=20067934

I have asked Administration to Email you in these regards.  I also noticed several other things I'd like to bring to you attention.  There are some that you did award with "C" grades, and no comment as to why.  Also, some of your questions have had zero response, and that is likely for two reasons:  Poor closure on open items and poor grades.  I think you'll agree, epecially given the number of questions you've asked here, that experts here take great pride in their work, and deserve "A" grades and responses to all help they've given you.

EXPERTS -->  In the event any of these items remain open at the end of this week, I will finalize them and look to you for guidance on the fair results.  As always, we very much appreciate your willingness to help and the excellent support you all give.

Thanks,

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6799706
Wow!  That's a comprehensive list you've got there, keep up the good work!

(intended both ironically for Shay and literally for Moondancer)
0
 
LVL 5

Expert Comment

by:Netminder
ID: 6822850
Admin notified of user neglect. Force/accepted by

Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now