簡體   English   中英

調整列表視圖大小時,如何設置標題寬度等於其列表視圖控件寬度?

[英]How to set header width equal to its list view control width when list view is resized?

我想在使用win32 api調整大小時將列表視圖中標頭的寬度設置為其列表視圖控件的寬度,因此我使用ListView_SetColumnWidth()將其寬度設置為其控件的寬度,但它不起作用。

這是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()

//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;
 }

我想念什么? 還有另一種方法嗎?

謝謝!

使用GetClientRect查找控件的內部矩形。 例:

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;
}

客戶矩形可以比窗口矩形小一些。

您可以使用width / height通過SetWindowPosMoveWindow設置listview控件的大小。 這將對應於GetWindowRect 但是,您希望使用客戶端矩形作為列寬。

您還可以子類化listview控件,並在listview_proc響應WM_SIZE

當用戶調整主窗口大小時,發送WM_SIZE 第一次打開窗口時,默認情況下不會觸發它。 第一次初始化窗口時,可能必須調用ListView_SetColumnWidth


還要注意,您不能在ListProc處理自定義繪制。 您必須從ListProc刪除WM_NOTIFY部分, ListProc將其添加到WndProc

建議編輯:

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;
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM