zoukankan      html  css  js  c++  java
  • VC CListCtrl 第一列列宽自适应

    今天用VC写工具的时候用到CListView,并且ListCtrl的第一列需要自动拉伸,以占满空白区域

    Delphi做这个设置是很容易的,只要将Column的AutoSize设置为True就可以了

    不过VC/SDK的AutoSize其实是根据Item的文本长度来自动设置Column Width,跟我的要求不符,只好自己实现了

    其实很简单,就是觉得麻烦...

    void CMyView::AdjustColumnWidth()
    {
        RECT rc;
        CListCtrl
    & list = GetListCtrl();
        CHeaderCtrl
    * pHeader = list.GetHeaderCtrl();
        
    if (!pHeader)
            
    return;

        
    // 客户区域
        list.GetClientRect(&rc);
        
    int nColCount = pHeader->GetItemCount();
        
    // 去掉其他列占用的宽度
        for (int i = 1; i < nColCount; i++)
        {
            rc.right 
    -= list.GetColumnWidth(i);
        }

        
    // 去掉滚动条占用的宽度
        SCROLLBARINFO sbi;
        
    if (list.GetScrollBarInfo(OBJID_VSCROLL, &sbi))
        {
            rc.right 
    -= sbi.rcScrollBar.right - sbi.rcScrollBar.left;
        }

        
    // 修改列宽
        list.SetColumnWidth(0, rc.right > rc.left ? rc.right - rc.left : LVSCW_AUTOSIZE_USEHEADER);
    }

    使用方法:在OnSize消息中调用一下AdjustColumnWidth就好了

    void CMyView::OnSize(UINT nType, int cx, int cy)
    {
        CListView::OnSize(nType, cx, cy);

        
    if (GetListCtrl().GetSafeHwnd())
            AdjustColumnWidth();
    }



    看清这世界的美丽与残酷



    NAILY Soft
    Sephil on CNBlogs
  • 相关阅读:
    vim tab 和4个空格
    python 入门
    pyenv 以及 virtualenv
    Redis Cluster 理论知识
    使用Redis SETNX 命令实现分布式锁
    go runtime scheduler
    LeetCode Valid Parentheses
    LeetCode Rotate Image
    leetcode
    HDU 3657 Game(取数 最小割)经典
  • 原文地址:https://www.cnblogs.com/sephil/p/2004384.html
Copyright © 2011-2022 走看看