Mfc 列表框宽度大小取决于文本长度

Mfc 列表框宽度大小取决于文本长度,mfc,width,clistbox,Mfc,Width,Clistbox,我的应用程序有一个窗口,里面有一个列表框,里面充满了随时间变化的文本,因此列表框条目可以有几个长度 我想让窗口和列表框的宽度根据列表框条目的长度(以字符数为单位)动态变化 例如,如果我的列表框有多个条目,并且最大长度为30个字符,那么我希望使窗口及其列表框的宽度大于一个窗口(maixum Length为20个字符) 最好的方法是什么?您使用的是什么编程平台?我猜是.NET和VB 输入检查列表内容的方法,并根据需要更改框和窗口的大小: Dim intMaxLength As Integer = 2

我的应用程序有一个窗口,里面有一个列表框,里面充满了随时间变化的文本,因此列表框条目可以有几个长度

我想让窗口和列表框的宽度根据列表框条目的长度(以字符数为单位)动态变化

例如,如果我的列表框有多个条目,并且最大长度为30个字符,那么我希望使窗口及其列表框的宽度大于一个窗口(maixum Length为20个字符)


最好的方法是什么?

您使用的是什么编程平台?我猜是.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  

希望这能给您一个不错的起点。

您使用的是什么编程平台?我猜是.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  

希望这能给你一个不错的起点。

试试这样的方法:

// 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);
//查找最长的项目
CString最长;
对于(int i=0;ilongest.GetLength())
最长=温度;
}
//获取最长项目的名称
CSize size=GetWindowDC()->GetTextExtent(最长);
//你需要这个来保持当前的高度
RECT-RECT;
m_list.GetWindowRect(&rect);
//仅更改宽度
int width=size.cx;
int height=rect.bottom-rect.top;
m_list.SetWindowPos(NULL、0、0、宽度、高度、SWP_NOZORDER | SWP_NOMOVE);

尝试以下方法:

// 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);
//查找最长的项目
CString最长;
对于(int i=0;ilongest.GetLength())
最长=温度;
}
//获取最长项目的名称
CSize size=GetWindowDC()->GetTextExtent(最长);
//你需要这个来保持当前的高度
RECT-RECT;
m_list.GetWindowRect(&rect);
//仅更改宽度
int width=size.cx;
int height=rect.bottom-rect.top;
m_list.SetWindowPos(NULL、0、0、宽度、高度、SWP_NOZORDER | SWP_NOMOVE);
试试这个:

int maxcol = ((CHeaderCtrl*)(listctrl.GetDlgItem(0)))->GetItemCount()-1;
for (int col = 0; col <= maxcol; col++)
{
    listctrl.SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
}
intmaxcol=((CHeaderCtrl*)(listctrl.GetDlgItem(0))->GetItemCount()-1;
对于(int col=0;col请尝试以下方法:

int maxcol = ((CHeaderCtrl*)(listctrl.GetDlgItem(0)))->GetItemCount()-1;
for (int col = 0; col <= maxcol; col++)
{
    listctrl.SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
}
intmaxcol=((CHeaderCtrl*)(listctrl.GetDlgItem(0))->GetItemCount()-1;

(int=0;COL改变了一切。我不是C++的家伙。也许其他人可以帮助你。)这改变了一切。我不是C++的家伙。也许其他人可以帮助你。这似乎是一个很好的解决办法。我一到家就会尝试。您可能需要对每个字符串调用GetTextExtent。使用比例字体时,短字符串完全可能比长字符串宽。您需要确保使用列表框的设备上下文,即“m_list.GetWindowDC()”而不仅仅是“GetWindowDC()”。另外,要使用列表框的当前字体而不是默认的系统字体测量文本,您还需要“m_list.GetWindowDC()。SelectObject(m_list.GetFont())”。谢谢。这似乎是一个很好的解决办法。我一到家就会尝试。您可能需要对每个字符串调用GetTextExtent。使用比例字体时,短字符串完全可能比长字符串宽。您需要确保使用列表框的设备上下文,即“m_list.GetWindowDC()”而不仅仅是“GetWindowDC()”。此外,要使用列表框的当前字体而不是默认的系统字体来测量文本,还需要“m_list.GetWindowDC()。SelectObject(m_list.GetFont())”。