调整列表视图大小时,如何设置标题宽度等于其列表视图控件宽度? [英] How to set header width equal to its list view control width when list view is resized?
问题描述
我想在使用win32 api调整大小时将列表视图中标头的宽度设置为其列表视图控件的宽度,因此我使用ListView_SetColumnWidth()
设置其宽度等于其控件的宽度,但它不起作用.
I want to set width of header in a list view to width of its list view control when it is resized using win32 api.So i use ListView_SetColumnWidth()
to set its width equal to its control's width but it does not work.
这是WinMain()
中的代码:
InitCommonControls();
hwndList1 = CreateWindow(WC_LISTVIEW , L"" , WS_VISIBLE | WS_CHILD | LVS_REPORT | WS_BORDER | WS_VSCROLL, 10 , 10 , width , height, hwnd, NULL, GetModuleHandle(NULL), 0);
//Sub classing the list control
SetWindowSubclass(hwndList1 ,ListProc,0 ,NULL);
SendMessage(hwndList1,LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_FULLROWSELECT,LVS_EX_FULLROWSELECT);
hHeader1=ListView_GetHeader(hwndList1);
GetClientRect(hwndList1 , &rect1);
CreateColumn(hwndList1 , 0 , (char*)L"MASTER" , rect1.right );
//enable arrows
EnableScrollBar(hwndList1 , SB_VERT , ESB_ENABLE_BOTH);
//scroll down
SendMessage(hwndList1, WM_VSCROLL, SB_BOTTOM, 0L);
这是ListProc()
:
//the list proc
LRESULT CALLBACK ListProc( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,UINT_PTR, DWORD_PTR ){
switch(msg)
{
case WM_NOTIFY :
if (((LPNMHDR) lp)->code == NM_CUSTOMDRAW)
{
LPNMCUSTOMDRAW lpcd = (LPNMCUSTOMDRAW)lp;
switch(lpcd->dwDrawStage)
{
case CDDS_PREPAINT :
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
{
SetBkColor(lpcd->hdc, RGB(0, 135, 234));
SetTextColor(lpcd->hdc, RGB(255, 255, 245));
return CDRF_NEWFONT;
}
break;
}
}
break;
case WM_NCPAINT:
{
RECT rc;
GetWindowRect(hwnd, &rc);
OffsetRect(&rc, -rc.left, -rc.top);
auto hdc = GetWindowDC(hwnd);
auto hpen = CreatePen(PS_SOLID, 1, RGB(201, 201, 201));
auto oldpen = SelectObject(hdc, hpen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
SelectObject(hdc, oldpen);
DeleteObject(oldpen);
ReleaseDC(hwnd, hdc);
return 0;
}
case WM_NCDESTROY:
RemoveWindowSubclass(hwnd, ListProc, 0);
break;
}
return DefSubclassProc(hwnd, msg, wp, lp);
}
对于父窗口过程,以下代码为WndProc()
:
And the following code is WndProc()
for parent window procedure:
//The window procedure
LRESULT CALLBACK WndProc( HWND hwnd , UINT msg , WPARAM wParam , LPARAM lParam){
switch(msg){
case WM_SIZE:{
int nHeight , nWidth;
width =(int)((nWidth /2) * 0.8);
height =(int)((nHeight/2) * 0.7);
if( wParam == SIZE_RESTORED ){
SetWindowPos(hwndList1, 0 , 10, 10 , width, height,SWP_NOZORDER|SWP_NOMOVE);
RECT Rc;
GetClientRect(hwndList1, &Rc);
ListView_SetColumnWidth(hwndList1, 0, Rc.right - Rc.left);
}
else if ( wParam == SIZE_MAXIMIZED )
{
SetWindowPos(hwndList1, 0 , 20, 20, width, height,0);
RECT Rc;
GetClientRect(hwndList1, &Rc);
ListView_SetColumnWidth(hwndList1, 0, Rc.right - Rc.left);//
}
}
break;
case WM_NOTIFY:
if(((LPNMHDR)lParam)->code == NM_CUSTOMDRAW) {
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
switch(lplvcd->nmcd.dwDrawStage) {
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
if (((int)lplvcd->nmcd.dwItemSpec%2)==0) {
lplvcd->clrText = RGB(0,0,0);
lplvcd->clrTextBk = RGB(255, 255, 255);
} else {
lplvcd->clrText = RGB(0,0,0);
lplvcd->clrTextBk = RGB(255,255,255);
}
return CDRF_NEWFONT;
}
}
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam)){
case ID_FILE_EXIT:
PostMessage(hwnd, WM_CLOSE , 0 , 0);
break;
case ID_ABOUT:
{
int ret=DialogBox( GetModuleHandle(NULL) , MAKEINTRESOURCE(ID_ABOUT) , hwnd , AboutDlgProc );
}
break;
}
break;
case WM_CLOSE:
DestroyWindow( hwnd );
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc( hwnd , msg , wParam , lParam );
}
return 0;
}
我想念什么?还有另一种方法吗?
What am i missing? Is there another way to do it?
谢谢!
推荐答案
使用GetClientRect
查找控件的内部矩形.示例:
Use GetClientRect
to find the inner rectangle for the control. Example:
case WM_SIZE:
{
//resize the listview control first
//calculate width/height
SetWindowPos(hwndList1, NULL, 0, 0, width, height, SWP_NOZORDER|SWP_NOMOVE);
RECT rc;
GetClientRect(hwndList1, &rc);
ListView_SetColumnWidth(hwndList1, 0, rc.right - rc.left);//rc.left is zero
break;
}
客户端矩形可以比窗口矩形小一些.
The client rectangle can be a bit smaller than the window rectangle.
您可以使用width/height使用SetWindowPos
或MoveWindow
设置列表视图控件的大小.这将对应于GetWindowRect
.但是您希望使用客户端矩形作为列宽.
You can use width/height to set the size of listview control using SetWindowPos
or MoveWindow
. This will correspond to GetWindowRect
. But you want the client rectangle for column width.
您还可以将listview控件子类化,并在listview_proc
中响应WM_SIZE
.
You can also subclass the listview control and respond to WM_SIZE
in listview_proc
.
WM_SIZE
.第一次打开窗口时,默认情况下不会触发它.第一次初始化窗口时,您可能必须调用ListView_SetColumnWidth
.
WM_SIZE
is sent when the user resizes the main window. It is not triggered by default when the window is first opened. You may have to call ListView_SetColumnWidth
when the window is first initialized.
另请注意,您不能在
ListProc
中处理自定义绘图.您必须从ListProc
中删除WM_NOTIFY
部分,并将其仅添加到WndProc
.
Also note, you cannot handle custom draw inside
ListProc
. You must remove WM_NOTIFY
section from ListProc
, add it to WndProc
only.
建议的修改:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_SIZE:
{
int nWidth = LOWORD(lParam);
int nHeight = HIWORD(lParam);
int width = (int)((nWidth / 2) * 0.8);
int height = (int)((nHeight / 2) * 0.7);
SetWindowPos(hwndList1, 0, 20, 20, width, height, SWP_NOZORDER);
RECT rc;
GetClientRect(hwndList1, &rc);
ListView_SetColumnWidth(hwndList1, 0, rc.right - rc.left);
}
break;
case WM_NOTIFY:
if(((LPNMHDR)lParam)->code == NM_CUSTOMDRAW)
{
LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
switch(lplvcd->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
return CDRF_NOTIFYITEMDRAW;
case CDDS_ITEMPREPAINT:
lplvcd->clrText = RGB(255, 0, 0);
lplvcd->clrTextBk = RGB(255, 255, 0);
return CDRF_NEWFONT;
}
}
break;
...
}
这篇关于调整列表视图大小时,如何设置标题宽度等于其列表视图控件宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!