Among the WIN32 standard controls, the list control (ListBox) is not provided with the list view (ListView), so if the length of the list exceeds the width of the list, then the exceeded portion will not be displayed. In this article, I will explain how to use SDK to solve this problem with a simple example. In this example, I will add 100 lines of the following format for a list control: this is a very very very very very very long desentence - Line 1 this is a very very very very very long service - line 2 ... This adding text code is: Case WM_INITDIALOG: {INT I; TCHAR STR [100]; for (i = 0; i <100; i ) ) {WSPRINTF (STR, "this is a very very very very very long sentence - line% d", i 1); senddlgitemmessage (HDLG, IDC_LIST, LB_ADDSTSTRING, 0, (LPARAM) STR);}} Break; Of course, Before adding a horizontal scroll bar, it is the following effects:
Below I come to add a horizontal scroll bar for this list control, first you need to set a horizontal scroll bar for this list control in the design, and then you can add a level scroll bar to it by sending a LB_SethorizontALEXTENT message to the list control. In the additional parameters of this message, the WPARAM parameter is the length of the horizontal scroll bar in pixels, and LParam is not used. So, you can set a sufficient length (assuming to 500) for this scroll bar. Case WM_INITDIALOG: {HDC HDC; INT I; TCHAR STR [100]; for (i = 0; i <100; i ) ) {WSPRINTF (STR, "This Is a Very Very Very Very Long Sentence - LINE% D", I 1); SenddlgiteMmessage (HDLG, IDC_LIST, LB_ADDSTSTRING, 0, (LPARAM) STR);} SenddlgiteMmessage (HDLG, IDC_LIST , LB_SETHORIZONTALEXTENT, 500, 0); // Setting the horizontal scroll bar of the length of 500 pixels} Break; this code is executed, the effect is as follows:
As you can see, we have successfully added a horizontal scroll bar for the list control. However, the beauty is not enough, and the length of 500 seems too long, very unrestrained. Then what we need is a way, we can use this method to convert the length of the string into an appropriate pixel length, so that this interface is beautiful. Fortunately, the win32 API does have such a function: BOOL GettextExtentPoint32 (HDC HDC, // Related Equipment Handle LPCTSTSTR LPSTRING, // String INT CBSTRING, // String Character number (ie) LPSIZE LPSIZE //// Used to receive the size of the string); maybe you will be very wondering: Why is this function to talk to HDC? In fact, it is very simple: Windows is a graphic operating system, so all texts are also "painted" to the interface by the Windows system. Then, the length of the string naturally and the size of the font and the size of the font, the pixel width of the string must also be calculated. My code is as follows: Case WM_INITDIALOG: {HDC HDC; SIZE S; INT I; TCHAR STR [100]; HDC = Getdc (HDLG); // Get related device handle for (i = 0; i <100; i ) { WSPrintf (STR, "this is a very very very very very very long sentence - line% d", i 1); GetTextExtentPoint32 (HDC, STR, LSTRLEN (STR), & S); // Get the pixel size of the string //// If the new string width is greater than the previous horizontal scroll width, reset the scroll bar width IF (S.cx> (long) SenddlgiteMMessage (HDLG, IDC_LIST, LB_GETHORIZONTALEXTENT, 0, 0)) SenddlgiteMmessage (HDLG, IDC_LIST, LB_SETHORIZONTALEXTENT, (WParam) S.cx, 0); Senddlgitemmessage (HDLG, IDC_LIST, LB_ADDSTRING, 0, (LPARAM) STR);} ReleaseDC (HDLG, HDC);} Break; now you can easily call out a drown, look at the effect Bar: You may ask, why is there a little blank on the right? For this problem, the note of GettextExtentPoint 32 is explained: "Due to the character's tightening character, the range of characters in a string may not be equal to the range of the string, calculating the character width considering the character interval by the SetTextCharacterextra device. "At the end of this article, I suddenly discovered the LB_Gettextlen message in the list control message in the MSDN, which can be used to get the character length of the list item. In this way, the pixel length of the list item may also be converted through the length of the character, but I didn't find this method.