我的问题是:
System.ComponentModel.Win32Exception:'创建窗口句柄时出错'。
我知道我可以用Dispose()
解决这个问题,但是当我在程序中使用它时,我显示另一个错误:
System.ObjectDisposedException:'无法访问已处置的对象。对象名称:'PictureBox'。 “
我使用以下代码:
private void SetUpPuzzle_Click(int parts)
{
Panel P = new Panel
{
Size = new Size(200, 200),
Location = new Point(394, 62),
};
Controls.Add(P);
Control board = P;
int total = parts * parts;
var PB = new PictureBox[total];
var imgarray = new Image[total];
var img = User_Image.Image;
int W = img.Width / (int.Parse(Math.Sqrt(double.Parse(parts.ToString())).ToString()));
int H = img.Height / (int.Parse(Math.Sqrt(double.Parse(parts.ToString())).ToString()));
int size = 200 / (int.Parse(Math.Sqrt(double.Parse(parts.ToString())).ToString()));
for (int x = 0; x < parts; x++)
{
for (int y = 0; y < parts; y++)
{
var index = x * parts + y;
imgarray[index] = new Bitmap(W, H);
using (Graphics graphics = Graphics.FromImage(imgarray[index]))
graphics.DrawImage(img, new Rectangle(0, 0, W, H),
new Rectangle(x * W, y * H, W, H), GraphicsUnit.Pixel);
PB[index] = new PictureBox
{
Name = "P" + index,
Size = new Size(size, size),
Location = new Point(x * size, y * size),
Image = imgarray[index],
SizeMode = PictureBoxSizeMode.StretchImage
};
PB[index].MouseEnter += Images_M_E;
PB[index].MouseLeave += Images_M_L;
PB[index].MouseClick += Form_MouseClick;
*PB[index].Dispose();
*board.Controls.Add(PB[index]);
}
}
}
当我想创建10,000个对象时
显示此错误。
我的问题是:
System.ComponentModel.Win32Exception:'创建窗口句柄时出错'。
确实。您正在为Winforms
应用程序创建太多控件。
处理它们并没有多大帮助,因为你不能再使用处理过的物体了。
要拥有这种大型拼图(10k件),你需要改变使用PictureBoxes
(或任何其他Controls
)来展示拼图的不同方法。这已经在original question中被提出但是你只想拥有100件,还记得吗?
最常见的方法是:保留一个图像列表(当它们<= 256x256像素时将它们放入ImageList
!)并在电路板的Paint
事件中绘制它们。这将摆脱PictureBoxes
所涉及的所有开销。
(旁白:人们可能认为这不会对所有DrawImage
调用都有效。但是所有那些PictureBoxes
也需要在所有表面上绘制所有像素,所以这不是问题。但是它们也必须承担存在的开销(在引擎盖下)功能齐全的windows
(参见错误信息!),这就是系统只能有限数量的原因;总是尽量保持控件的数量<1k!)
您必须将放置逻辑移动到电路板的Paint
事件,并且还必须更改事件模型..:
而不是让每个PictureBox
回应自己的事件,你将不得不找到一种方法来完成董事会活动中的所有工作。根据事件的不同,这必须是不同的。
由于我们不知道你有哪些事件,他们做了什么以及他们的工作需要哪些数据,因此很难提供所有必要的细节,所以我只想指出一些事情......:
Enter
或Leave
活动。相反,您需要通过在MouseMove事件中测试它来检测输入片段的区域。如果您保留List<Rectangle>
,您可以使用Rectangle.Contains(e.Location)
进行此测试。所有其他事件都可以使用类似的想法;有些很简单,有些需要一些计算,但它们都很快且很容易实现。
要优化性能,请尝试使图像尺寸合适,并使用Format32bppPArgb作为像素格式,因为它的显示速度更快。
另一种选择是使用您现在用于创建它们的相同计算从Paint
事件中的原始图像中直接拉出像素数据。 (有一个DrawImage
叠加层使用两个Rectangles
,一个用于确定目标,另一个用于源区域。)这样可以节省GDI
手柄,至少如果你不能使用ImageList
。
始终计划增长!为了更好的实现,请创建一个Piece
类。它应该在Rectangle
的ImageList
集合中持有Images
和整数索引。它也可能有一个方法Switch(Piece otherPiece)
,可以切换Rectangles
或索引。
祝好运 :-)
我遇到了这个异常,因为无限循环创建了新的UI控件并设置了它的属性。在循环多次之后,当变更控制可见属性时抛出此excption。我发现用户对象和GDI对象(来自任务管理器)都非常大。
我想你的问题是类似的原因,系统资源被那些UI控件耗尽。
我评论PB[index].Dispose();
和它的工作。
private void SetUpPuzzle(int parts)
{
// Comment ***********
//Panel P = new Panel
//{
// Size = new Size(200, 200),
// Location = new Point(394, 62),
//};
//Controls.Add(P);
//Control board = P; ***********
int total = parts * parts;
var PB = new PictureBox[total];
var imgarray = new Image[total];
var img = User_Image.Image;
int W =Convert.ToInt32(img.Width / Math.Sqrt(parts));
int H = Convert.ToInt32(img.Height / Math.Sqrt(parts));
int size = Convert.ToInt32(200 / Math.Sqrt(parts));
for (int x = 0; x < parts; x++)
{
for (int y = 0; y < parts; y++)
{
var index = x * parts + y;
imgarray[index] = new Bitmap(W, H);
using (Graphics graphics = Graphics.FromImage(imgarray[index]))
graphics.DrawImage(img, new Rectangle(0, 0, W, H),
new Rectangle(x * W, y * H, W, H), GraphicsUnit.Pixel);
PB[index] = new PictureBox
{
Name = "P" + index,
Size = new Size(size, size),
Location = new Point(x * size, y * size),
Image = imgarray[index],
SizeMode = PictureBoxSizeMode.StretchImage
};
PB[index].MouseEnter += Form1_MouseEnter;
PB[index].MouseLeave += Form1_MouseLeave;
PB[index].MouseClick += Form1_MouseClick;
//Comment
//PB[index].Dispose(); < -----------------
// Add PB in Panel in form
panel1.Controls.Add(PB[index]);
}
}
// after add all refresh panel
panel1.Refresh();
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
throw new NotImplementedException();
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
throw new NotImplementedException();
}
private void Form1_MouseEnter(object sender, EventArgs e)
{
throw new NotImplementedException();
}
然后在按钮中调用SetUpPuzzle
方法,如:
private void button1_Click(object sender, EventArgs e)
{
SetUpPuzzle(10);
}