gpt4 book ai didi

c# - 动态显示数百张图片

转载 作者:行者123 更新时间:2023-11-30 19:50:40 24 4
gpt4 key购买 nike

我必须创建一个能够显示电影院(不知道确切的词)模式的表单。本质上,我必须显示大量(由另一个来源提供)独立的类似椅子的图像,这些图像可以在点击时改变颜色(状态)。

我在网上冲浪寻找解决方案,但我真的不知道如何解决这个问题。有人可以帮助我吗?

最佳答案

如果您需要绘制那么多图像,最好的办法是使用面板控件并通过处理 OnPaint 事件或更好的方式自己处理绘图:创建一个继承自 Panel 控件并覆盖 Paint 的自定义控件方法。在线查找有关如何在 .NET 中创建自定义绘制控件的示例。

不要尝试使用 Image 控件或其他此类控件创建数百个图像,因为这会增加很多开销。

在Paint方法中,可以使用DrawImage根据不同状态(即选中或未选中)绘制椅子的函数。您可以将椅子的状态存储在内存中的一维或二维数组中,然后在 Paint 方法中循环绘制每把椅子,根据其索引计算椅子在屏幕上的位置:

for(int chairIndex = 0; chairIndex < chairsCount; chairIndex++)
{
// compute the on-screen position of each chair
chairX = (chairIndex % chairsPerLine) * chairWidh;
chairY = (chairIndex / chairsPerLine) * chairHeight;

// and read the current state from the array
chairState = chairsArray[chairIndex];

// then draw the chair image in the graphics context
switch(chairState)
{
case /* SELECTED */
.. DrawImage(selectedImage, new Point(chairX, chairY));
case /* NOT-SELECTED */
.. DrawImage(nonSelectedImage, new Point(chairX, chairY));
}
}

当用户单击椅子以切换其在内存中的状态时,您还必须处理鼠标事件以“点击测试”。

// compute chairIndex based on mouse position (for hit-test)
chairIndex = mouseX / chairWidth + (mouseY / chairHeight) * chairsPerLine;
// then toggle state accordingly

上面的代码片段假设您之前已经定义了一些变量,您已经将不同的椅子图像加载到两个或更多变量中,并且您正在使用一维数组来存储椅子状态。

关于c# - 动态显示数百张图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1999792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com