【发布时间】:2019-08-30 14:29:27
【问题描述】:
在将一些项目添加到列表视图后,我需要使用 win32 api 更改列宽,因为垂直滚动条的宽度会导致显示水平滚动条,我想将其删除。
但ListView_SetColumnWidth() 不会改变列宽。
//Column
int CreateColumn(HWND hwndLV, int iCol, char* Text, int iBreite)
{
LVCOLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT ;
lvc.fmt=LVCFMT_LEFT;
lvc.cx = iBreite;
lvc.pszText =(LPWSTR)Text;
lvc.iSubItem = iCol;
return ListView_InsertColumn(hwndLV, iCol, &lvc);
}
//item
int CreateItem(HWND hwndList, char* Text)
{
LVITEM lvi = {0};
lvi.mask = LVIF_TEXT | LVCFMT_LEFT;
lvi.pszText = (LPWSTR)Text;
return ListView_InsertItem(hwndList, &lvi);
}
//Some code ...
hwndList = CreateWindow(WC_LISTVIEW , L"" , WS_VISIBLE | WS_CHILD | LVS_REPORT | WS_BORDER | WS_VSCROLL , 10 , 10 ,300 , 200, hwnd, NULL, GetModuleHandle(NULL), 0);
SendMessage(hwndList,LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT,LVS_EX_FULLROWSELECT);
GetClientRect(hwndList , &rect);
CreateColumn(hwndList , 0 , (char*)L"HEADER" , rect.right );
//Some other codes for adding items here
ListView_SetColumnWidth(hwndList, 0,200); //Does not change the width
如何更改列的宽度?
【问题讨论】:
-
为什么要将 Unicode
const wchar_t[]字符串类型转换为char*只是为了在 API 中将它们转换回wchar_t*?完全摆脱char类型转换。将const wchar_t*用于Text参数,将const_cast<wchar*>(Text)分配给Text时使用pszText -
ListView_SetColumnWidth 对我来说很好用。发布更多代码。你是如何创建 ListView 的?
-
@Anders 我添加了...我真的不明白为什么这不起作用...我要生气了
-
你的字符串代码还是很奇怪,去掉所有
(char*)和(LPWSTR)演员表!仅使用const_cast<LPTSTR>(xyz)。