请问如何设定CListCtrl的行高?不用图标。

七伤拳 2001-11-25 07:31:47
我知道在CListBox中可以通过在OnMeasureItem中来设置行高,查MSDN说MeasureItem要自绘风格的才能接受到,但我又不想自绘,请问该如何设定行高?
不是用设定图标高度然后加入的方法实现.
...全文
718 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wglacier 2001-12-06
  • 打赏
  • 举报
回复
发出去了,祝你好运
七伤拳 2001-12-06
  • 打赏
  • 举报
回复
TO ghz(浪子):
我照你说的那样做了,可是还是不行,搞不懂错在哪里.
你可不可以给我发个例程,麻烦你了!
jxlilin@sina.com
wglacier 2001-12-01
  • 打赏
  • 举报
回复
其实不用SetFont也行:
1、添加:
protected:
//{{AFX_MSG(CMyListCtrl)
afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
//}}AFX_MSG

2、BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
//{{AFX_MSG_MAP(CMyListCtrl)
ON_WM_MEASUREITEM()
//}}AFX_MSG_MAP
ON_WM_MEASUREITEM_REFLECT( )

END_MESSAGE_MAP()

3、void CMyListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = 50;//改成你想要的行高
}

4、别忘了给它发个消息 m_list.SendMessage(WM_WINDOWPOSCHANGED,0,0);
(注:2000下可不太好使)


wglacier 2001-12-01
  • 打赏
  • 举报
回复
那个SendMessage(WM_WINDOWPOSCHANGED,0,0)方法也要Owner Draw风格。
用SetFont也是,
要求必须是Owner Draw风格,这是Codeguru上的一篇文章:

Changing row height in owner drawn control

--------------------------------------------------------------------------------
When you change the font of a list view control, the control or its parent window does not get a chance to respecify the height of the rows. No WM_MEASUREITEM message is sent to the controls parent window. The net effect is that when the font is changed for the control, the row height is no longer valid.
Here's a work around that forces the WM_MEASUREITEM message to be generated.

Step 1: Add handler for WM_SETFONT
The WM_MEASUREITEM message is sent when the control is created. Fortunately this message is also sent whenever the control is resized. Since we want the row height to be adjusted when the font changes, what better place to do thin than in the WM_SETFONT handler. In OnSetFont() we fool Windows into thinking that the window size of the control has changed. We do this by sending the WM_WINDOWPOSCHANGED message to the control which then gets handled by the default window procedure for the control. Note that we haven't specified the SWP_NOSIZE flag and we set the width and height field in the WINDOWPOS structure equal to the existing dimension.
For some reason, the Class Wizard did not have the WM_SETFONT message in its list of window messages. You will have to add the message map entry yourself. It is important to place the entries outside of the block used by the Class Wizard, otherwise the next you make any change with the wizard, our manual changes will be lost.

// In the header file
//{{AFX_MSG(CMyListCtrl)
:
:
//}}AFX_MSG
afx_msg LRESULT OnSetFont(WPARAM wParam, LPARAM);
afx_msg void MeasureItem ( LPMEASUREITEMSTRUCT lpMeasureItemStruct );
DECLARE_MESSAGE_MAP()


//////////////////////////////////////////////////////////////////////
// In the cpp file
BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
//{{AFX_MSG_MAP(CMyListCtrl)
:
:
//}}AFX_MSG_MAP
ON_MESSAGE(WM_SETFONT, OnSetFont)
ON_WM_MEASUREITEM_REFLECT( )
END_MESSAGE_MAP()


LRESULT CMyListCtrl::OnSetFont(WPARAM wParam, LPARAM)
{
LRESULT res = Default();

CRect rc;
GetWindowRect( &rc );

WINDOWPOS wp;
wp.hwnd = m_hWnd;
wp.cx = rc.Width();
wp.cy = rc.Height();
wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
SendMessage( WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp );

return res;
}

Step 2: Add handler for WM_MEASUREITEM
Since we want our CListCtrl derived class to be modular, we will handle the WM_MEASUREITEM message within this class. The message however, is sent to the parent window, so we use message reflection. Again, the Class Wizard is not much help. We have to manually add the entry in the message map and update the header file. See the code snippet in step one for this.
void CMyListCtrl::MeasureItem ( LPMEASUREITEMSTRUCT lpMeasureItemStruct )
{
LOGFONT lf;
GetFont()->GetLogFont( &lf );

if( lf.lfHeight < 0 )
lpMeasureItemStruct->itemHeight = -lf.lfHeight;
else
lpMeasureItemStruct->itemHeight = lf.lfHeight;
}


eaglerock 2001-12-01
  • 打赏
  • 举报
回复
改变字体可以的
七伤拳 2001-12-01
  • 打赏
  • 举报
回复
设置字体的方法可行,不过不符合要求,我只是想改变行的高度,并不想改变字体。
TO ghz(浪子) :
我试过了,不行,不知道是不是我的代码不对,你能给出具体的代码吗?谢谢了
firehorizon 2001-11-25
  • 打赏
  • 举报
回复
konfyt(--- 以下信息不对外公开 ---)的方法可行,但是列表的头的高度也变了,而且头的字体不变!
wglacier 2001-11-25
  • 打赏
  • 举报
回复
重载OnMeasureItem,设定行高,但是这时并不调用它,
要在程序初始化时调用SendMessage(WM_WINDOWPOSCHANGED,0,0)时才调用OnMeasureItem,
具体的例子以前在Codeguru上见过,你可以去查查http://www.codeguru.com/
蒋晟 2001-11-25
  • 打赏
  • 举报
回复
好像只有自绘啊,哪位大哥知道其他的办法说一声
konfyt 2001-11-25
  • 打赏
  • 举报
回复
iLdf
设置字体绝对能做到,先构造一个 CFont对象,然后用 相关ListCtrl控件的成员变量或者控件指针 调用SetFont() 就OK了

你说不能可能是你那一小步不小心搞错了
firehorizon 2001-11-25
  • 打赏
  • 举报
回复
设置字体好像也不行,等待!
ZengYongChun 2001-11-25
  • 打赏
  • 举报
回复
设置字体可否?
ExitWindows 2001-11-25
  • 打赏
  • 举报
回复
正好我也有这个问题,UP。

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧