Add a smart horizontal scroll bar to CListBox

Source: Internet
Author: User

 

  

InIn MFC, It is very convenient to use a list box (CListBox) to display multiple strings. But Missing

The horizontal scroll bar of the provincial list box is not smart enough-the meaning of intelligence here is: when it should appear

Now, it should not disappear when it appears, and it should be able to automatically adjust its own size. Examples

Identify existing problems and solutions.

 

---- 1. Problem demonstration

 

---- Use the Visual Studio Application Wizard to create the CustomCListBox project. This is based on

The default value is used for all optional parameters provided by the wizard.

 

---- In the resource editor, set the font of the main text box to 12 12, insert a CListBox control, and set

ItsID: IDC_LLISTTEST; Size: 125X84. Check the vertical scroll bar and horizontal roll of the list box.

The dynamic bar is valid and Its sorting style is canceled.

 

---- Start Class Wizard, select the Member Variables tab, and add the corresponding

Member variablesM_lListTest, and select Control in Category.

 

---- Select ClassView In the Workspace pane to expand the CCustomCListBoxDlg class and

Double-clickOnInitDialog (), find the comment line "TODO: Add extra initialization here" in the editing pane ",

Add the following content to the row:

 

M_lListTest.AddString (_ T ("One "));

M_lListTest.AddString (_ T ("Two "));

M_lListTest.AddString (_ T ("Three "));

M_lListTest.AddString (_ T ("Four "));

M_lListTest.AddString (_ T ("Five "));

M_lListTest.AddString (_ T ("Six "));

M_lListTest.AddString (_ T ("North China scenery, thousands of miles of ice, thousands of miles of snow. "));

M_lListTest.AddString (_ T ("Eight "));

M_lListTest.AddString (_ T ("Nine "));

M_lListTest.AddString (_ T ("Ten "));

 

 

---- Compile and run this project. You can find that the list box correctly displays all the content.

 

---- If you add a row after m_lListText.AddString (_ T "Ten:

 

M_lListTest.AddString (_ T ("Eleven "));

 

---- Recompile and run the project. A vertical scroll bar is displayed. Vertical scroll bar

The horizontal display width of the list box is reduced, and the content of the seventh row is cut and cannot be completely displayed.

. However, the horizontal scroll bar does not appear automatically at this time, and the seventh row cannot be seen after it is cut.

 

---- If we delete the last added statement, extend the number of Chinese characters in the seventh line to a value that exceeds the display width of the list box.

You can also find that the horizontal scroll bar does not appear automatically. The cut part is still invisible.

 

---- It can be seen that the horizontal scroll bar of CListBox is not as "smart" as the vertical scroll bar: vertical

The scroll bar can always appear automatically when it is needed, and can automatically adjust its own size and horizontally

The scroll bar cannot.

 

---- 2. Solve the Problem

 

---- To improve code reusability, you can create a CListBox derived class and implement it in the derived class.

"Smart" horizontal scroll bar. The main issues to be considered include: Tracking the maximum string width (

Font changes in different occasions). If necessary, the width of the vertical scroll bar is calculated to automatically display and adjust the level.

The size of the scroll bar.

 

---- Select Insert/New Class from the menu and set the name of the newly created Class to CDJListBox. The base Class is

CListBox. The default value is used for other options. Click OK to automatically generate Visual Studio

DJListBox. cpp and DJListBox. h files.

 

---- Change the list box in the Main Dialog box to the CDJListBox type, that is, expand

CCustomListBoxDlg class and double-click the m_lListTest member. In the editing pane, modify

 

CListBoxM_lListTest;

 

 

:

 

CDJListBoxM_lListTest;

 

 

---- Then, before the class declares the code, insert

<

Related Article

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.