Change the width of the list box in ComboBox

xiaoxiao2021-03-06  41

We often use the combo box, and the combo box is a-drop and a list. In general, the width and selection box of the list box is as wide, but we really need to make the width of the list box, one, let us see what we want to choose.

In order to have this feature, I wrote the following function. First add a message handle of WM_CTLCOLOR in your dialog, or use CCOMBOBOX inheritance class, and add the following code:

Hbrush TVisualcombo :: ONCTLCOLOR (CDC * PDC, CWND * PWND, UINT NCTLCOLOR)

{

Hbrush Hbr = CCOMBOBOX :: ONCTLCOLOR (PDC, PWND, NCTLCOLOR);

Switch (nctlcolor) {

Case CTLCOLOR_EDIT:

Break;

Case CTLCOLOR_LISTBOX:

IF (ListWidth> 0) {

// new width of a listbox is defined

CRECT RECT;

PWND-> GetWindowRect (& Rect);

IF (Rect.width ()! = Listwidth) {

Rect.right = Rect.Left Listwidth;

PWND-> MoveWindow (& Rect);

}

}

Break;

}

// Todo: Return A Different Brush if The Default is not design

Return Hbr;

}

This is not completely good, you have to refresh the list box, so you can change the width of the list box according to the length of the text in the list box, you have to do this, you have to scan the list box. The entry has to calculate the length of the text (via PDC), so if you refresh the list box, you can display it longer to display.

The above method is implemented by the WM_CTLColor message, and later, knowing that there is a function in the MFC's CCOMBOBOX class, it is:

CCOMBOBOX :: SetDroppedwidth (int width);

With this function, you can set the width into your favorite value, and its actual width is the maximum of 2 values ​​below:

1. You set the value (that is, the value set above)

2. The value of the list box

转载请注明原文地址:https://www.9cbs.com/read-56456.html

New Post(0)