1

私のアプリケーションには、時間の経過とともに変化するテキストで満たされたリストボックスを含むウィンドウがあります。したがって、リストボックスのエントリにはいくつかの長さがあります。

ウィンドウとリストボックスの幅を、リストボックスのエントリの長さ(文字数)に応じて動的に変更したいと思います。

例として、リストボックスに複数のエントリがあり、最大長が30文字の場合、ウィンドウとそのリストボックスの幅を、maixumの長さが20文字である1つのウィンドウよりも大きくしたいとします。

これを行うための最良の方法は何ですか?

4

3 に答える 3

1

次のようなことを試してください:

// find the longest item
CString longest;
for (int i = 0; i < m_list.GetCount(); ++i)
{
    CString temp;
    m_list.GetText(i, temp);
    if (temp.GetLength() > longest.GetLength())
        longest = temp;
}

// get the with of the longest item
CSize size = GetWindowDC()->GetTextExtent(longest);

// you need this to keep the current height
RECT rect;
m_list.GetWindowRect(&rect);

// change only width
int width = size.cx;
int height = rect.bottom - rect.top;
m_list.SetWindowPos(NULL, 0, 0, width, height, SWP_NOZORDER | SWP_NOMOVE);
于 2009-12-11T18:44:22.787 に答える
0

これを試して:

int maxcol = ((CHeaderCtrl*)(listctrl.GetDlgItem(0)))->GetItemCount()-1;
for (int col = 0; col <= maxcol; col++)
{
    listctrl.SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
}
于 2009-12-12T20:10:37.960 に答える
0

どのプログラミングプラットフォームを使用していますか? 私は.NETとVBを推測しています。

リストの内容を調べ、必要に応じてボックスとウィンドウのサイズを変更するメソッドを挿入します。

Dim intMaxLength As Integer = 20
For Each myItem As String In ListBox1.Items
    If Len(myItem) > intMaxLength Then  
       'Number of characters times number of pixels per character  
        ListBox1.Width = Len(myItem) * 10  
        'Me refers back to the form object  
        'Add a few extra pixels to give space around your listbox  
        Me.Width = Len(myItem) * 10 + 30  
    End If  
Next  

これが適切な出発点になることを願っています。

于 2009-12-11T17:25:06.303 に答える