使用 ImageList 进行图像的面向对象设计
我正在创建一个代表 Image 的类,并且在 Windows 中,您可以将图像存储在 ImageList 控件中。此类将用于轻松地将图像添加到其他控件(例如 ListViews 或 Buttons),或者只是在屏幕上绘制。我的问题是:像这样的东西正确的设计是什么?每个 Image 是否应该拥有自己的一张图像的 ImageList,或者 Image 类是否应该为它的所有实例拥有一个巨大的 ImageList,还是什么?我对 Windows 类的内部了解不多,所以我不知道 ImageList 类有多“重”,但如果它相对便宜,我倾向于“每个实例都有自己的图像列表”。你有什么建议吗?
I am making a class which represents and Image, and with Windows, you store images in ImageList controls. This class will be used to handily add images to other controls like ListViews or Buttons, or simply to draw on the screen. My question is: what is the proper design for something like this? Should each Image have it's own ImageList of one image, or should the Image class have one giant ImageList for all the instances of it, or what? I don't know much about the internals of the Windows classes so I don't know how "heavy" the ImageList class is, but if it's relatively inexpensive, I was leaning toward the "each instance has it's own imagelist." What do you recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,我会推荐介于两者之间的东西。
为每个图像设置不同的图像列表似乎很愚蠢。它们旨在存储图像集合并提高管理这些集合的效率,而不是保存单个图像。
不过,尝试对所有图像使用一个巨大的图像列表也有缺点。即,图像列表中的所有图像必须具有相同的尺寸。如果您的图像不是这样,您需要将它们放置在单独的图像列表中。
但更重要的是,我不太确定为什么您首先要考虑创建一个包含图像列表的图像类。除非您要存储图标/光标或需要担心遮罩,否则为什么不直接封装代表图像的位图的句柄呢?无论如何,这与您要传递到图像列表的“添加”方法中的内容相同。
Generally, I would recommend something in between.
It seems silly to have a different image list for each image. They're designed to store collections of images and make managing these collections more efficient, not so much to hold a single image.
Then again, there are drawbacks to trying to use one giant image list for all of your images. Namely, all of the images in an image list must be of the same size. If this isn't true of your images, you'll need to place them in separate image lists.
But more to the point, I'm not really sure why you're considering creating an image class that contains an image list in the first place. Unless you're going to be storing icons/cursors or need to worry about masks, why not just encapsulate a handle to the bitmap representing the image? That's the same thing you're going to be passing into the image list's "Add" method anyway.