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

因为项目需要, 要更改 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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值