您的位置:首页 > 其它

解决 WinForm 中 TreeView 的 StateImageList 实际显示大小无法改变的问题

2009-07-09 16:00 721 查看
因为项目需要, 要更改 TreeView 的 StateImageList 大小, 试了下, 更改绑定的 StateImageList.ImageSize 没有作用, 显示大小始终是 16x16

在网上搜了搜, 相关资料比较少, 终于在 CodeProject 上找到问题原因:

http://www.codeproject.com/KB/tree/customstatetreeview.aspx?display=PrintAll&fid=313614&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=1519145

附文:

Underlying comctl treeview uses a zero image index, indicating no state image is displayed.

Thus comctl state imagelist must have a dummy as first image.

.NET copies the passed StateImageList to a new NET internal imagelist.

The first image is duplicated, serving as dummy and the copy is passed to comctl.

TreeNode.StateImageIndex values passed to comctl are then increased by 1.

This might have been a nice feature, but WinForms Team blundered using a constant 16 x 16 size for the copy.

If you want different size, use code below and add a dummy as first image.

// in derived TreeView

protected override void WndProc(ref Message m)

{

const int TV_FIRST = 0x1100;

const int TVM_SETIMAGELIST = (TV_FIRST + 9);

const int TVSIL_STATE = 2;

if (m.Msg == TVM_SETIMAGELIST)

{

if (m.WParam.ToInt32() == TVSIL_STATE &&

m.LParam != IntPtr.Zero)

{

// NET assigns a copy of StateImageList

Debug.Assert(StateImageList != null);

// pass comctl the original

m.LParam = StateImageList.Handle;

}

}

base.WndProc(ref m);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐