Add a horizontal scroll bar for a ListBox in MFC

Source: Internet
Author: User

We know that the horizontal scrollbar in MFC is not as smart as a vertical scroll bar. When the text exceeds the width of the listbox, the horizontal scroll bar does not appear on its own, and we need to manually call the function sethorizontalextent in CListBox to set the width in pixels.

We can add our own smart horizontal scroll bar, and now we'll start by creating a ListBox control and setting its Horizontalscrollbar property to True, as follows:

In this way, the creation of the control is complete, and then you need to add code to implement the smart horizontal scroll bar.

First, we create a class, where I'm named Cihlistbox, which needs to inherit the CListBox class in order to add a horizontal scroll bar.

Then we need to overwrite the addstring and insertstring interfaces of the CListBox class to add a horizontal scroll bar.

Finally, naturally, our main method of calculating intelligent horizontal scroll bars is named Refushhorizontalscrollbar.

The declaration of the entire class is as follows:

#ifndef _ihlistbox_h_
#define _ihlistbox_h_

Class Cihlistbox:public CListBox
{
Public
Cihlistbox (void);
~cihlistbox (void);

Override this method to add a horizontal scroll bar
int addstring (LPCTSTR lpszitem);
int insertstring (int nIndex, LPCTSTR lpszitem);

Calculate Horizontal ScrollBar Width
void Refushhorizontalscrollbar (void);
};

#endif

First, there is no suspense between addstring and Insertstring, which is to recalculate the width of the horizontal scrollbar after calling the method of the base class, and the code is as follows:

int cihlistbox::addstring (LPCTSTR lpszitem)
{
int nresult = clistbox::addstring (Lpszitem);

Refushhorizontalscrollbar ();

return nresult;
}

int cihlistbox::insertstring (int nIndex, LPCTSTR lpszitem)
{
int nresult = clistbox::insertstring (NIndex, Lpszitem);

Refushhorizontalscrollbar ();

return nresult;
}

Then there is the Refushhorizontalscrollbar method, the essence of which is to calculate the width of each item in the ListBox, and then set the maximum width to the horizontal width. The implementation code is as follows:

void Cihlistbox::refushhorizontalscrollbar (void)
{
CDC *PDC = This->getdc ();
if (NULL = = PDC)
{
Return
}

int ncount = This->getcount ();
if (Ncount < 1)
{
This->sethorizontalextent (0);
Return
}

int nmaxextent = 0;
CString Sztext;
for (int i = 0; i < ncount; ++i)
{
This->gettext (i, sztext);
CSize &cs = pdc->gettextextent (Sztext);
if (Cs.cx > Nmaxextent)
{
Nmaxextent = cs.cx;
}
}

This->sethorizontalextent (nmaxextent);
}

Then, when we get the listbox control, we just need to use the subclass method to implement the smart horizontal scroll bar.

My attempt code is as follows:

#define Dlg_list_test ((cihlistbox*) (GetDlgItem (idc_listtest)))

Dlg_list_test->addstring (TEXT ("This is Lenth tes"));
Dlg_list_test->addstring (TEXT ("This is lenth test test test");
Dlg_list_test->addstring (TEXT ("This is lenth test test, test Test test11111"));

The results are as follows:

The above is for your reference only, thank you all ^-^! ~

Add a horizontal scroll bar for a ListBox in MFC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.