如何阴影添加到一个ListView? [英] How to add a drop shadow to a ListView?
问题描述
有没有一种方法,我可以在C#中(查看= LargeIcon
模式),而无需使用第三方组件添加阴影到ListView项目?
Is there's a way I can add a drop shadow to a ListView item in C# (view = LargeIcon
mode) without using a third party component?
我能想到的唯一方法是通过绘制的图像本身的阴影,但这样一来,当我点击图像,投影也将得到强调(因为它是图像的一部分!)。因此,也许有一种方法,使ListView控件只突出了图像的特定区域,当我点击一个项目?
The only way I can think of is by drawing the drop shadow on the image itself, but this way when I click on the image, the drop shadow will also be highlighted (because it is part of the image!). So maybe there is a way to make the ListView only highlight a specific area of the image when I click on an item?
推荐答案
最简单的方法是创建一个合适的阴影效果的位图,并使用 OwnerDrawing
的 ListView控件
的项目。
The simplest way is to create a suitable dropshadow bitmap and use OwnerDrawing
the ListView
items.
下面是一个例子:
public Form1()
{
InitializeComponent();
shadow = (Bitmap)Image.FromFile(aDropShadowBitmap);
overlay = new Bitmap(64, 64);
using (Graphics G = Graphics.FromImage(overlay))
G.Clear(Color.FromArgb(127, 31, 191, 255));
}
Bitmap shadow;
Bitmap overlay;
private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
ListViewItem item = e.Item;
Point bLoc = new Point(e.Bounds.X + 35, e.Bounds.Y + 10);
Size imgS = imageList1.ImageSize;
e.Graphics.DrawImage(shadow, bLoc);
e.Graphics.DrawImage(imageList1.Images[e.ItemIndex], bLoc);
if ((e.State & ListViewItemStates.Selected) == ListViewItemStates.Selected)
{
e.Graphics.DrawImage(overlay, bLoc);
}
else { }
e.Graphics.DrawString(item.Text, listView1.Font, Brushes.Black,
bLoc.X, bLoc.Y + imgS.Height + 10);
}
它使用图片
在 64×64的ImageList
和适当的80×80位图阴影(在Photoshop中创建)
It uses Images
in a 64x64 ImageList
and a suitable 80x80 bitmap with a drop shadow (created in Photoshop) :
要想像我覆盖在图像与半透明选择位图覆盖
。下面是结果:
To visualize the selection I overlay the image with a semitransparent Bitmap overlay
. Here is the result:
注意:原样,结果有一个微妙的错误:点击文本将不会选择该项目。补救的办法是一个简单的解决方法..:
Note: As is, the result has a subtle bug: Clicking on the texts won't select the items. The remedy is a simple workaround..:
在 ItemHeight
不能直接设置。相反,它是从最大高度得到的分配和适用图像列表
:这里 LargeImageList
和 StateImageList
。
The ItemHeight
can't be set directly. Instead it is derived from the maximum heights of the assigned and applicable ImageLists
: Here LargeImageList
and StateImageList
.
除非我们要添加额外的透明像素为我们所有的图像,我们可以通过添加第二个的ImageList
与 IMAGESIZE 1×
,并将其指定为 StateImageList
。氖需要添加实际图片
!现在,该项目文本也将选择的项目..!
Unless we want to add extra transparent pixels to all our images, we can enlarge the active height of the items by adding a second ImageList
with an ImageSize 1x100
and assign it as the StateImageList
. Ne need to add actual Images
! Now the item texts will also select the items..!
这篇关于如何阴影添加到一个ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!