如何捕获控件中的所有项目? [英] How to capture all the items inside a control?
问题描述
假设面板上有一个picturexox在图片框上有一个datagridview.
Suppose there is a picturexox on the panel and there is a datagridview on the picturebox.
我使用以下代码捕获面板:
I use the following code to capture the panel:
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(panel1.ClientRectangle.Width, panel1.ClientRectangle.Height);
panel1.DrawToBitmap(bmp, panel1.ClientRectangle);
bmp.Save(@"test.jpg");
但是似乎只有图片框已被捕获,缺少它的datagridview.
But it seems that only the picturebox has been captured, the datagridview on it is missing.
如何与datagridview一起捕获图片框?
How can capture the picturebox together with the datagridview?
推荐答案
DrawToBitmap
方法将捕获在 Paint
事件和所有嵌套控件中绘制的图形.如果缺少,则不会直接嵌套或间接嵌套.这可能是这样发生的:
The DrawToBitmap
method will capture the graphics drawn in the Paint
event and all nested controls. If one is missing it isn't nested either directly of indirectly. Here is how this probably happened:
面板
和 PictureBoxes
的设计者行为不同.
Panels
(例如 GroupBoxes
, TabPages
)以及其他一些容器控件.
Panels
, like GroupBoxes
, TabPages
and a few more are container controls.
这意味着您用鼠标拖动到它们上的任何控件都会在其中嵌套.
This means that any control you drag onto them with your mouse gets nested in them.
PictureBox
不是容器,例如 Button
或 Label
..
要嵌套您在 PictureBox
中的 DataGridView
,您可以选择
To nest your DataGridView
in the PictureBox
you have a choice of
- 使用键盘(!) 将其移动到此处
- 将其嵌套在代码中(
yourDGV.Parent = yourPBox1;
)
通过移动 PictureBox
来查看 DGV
是否随键盘一起移动,以测试键盘方式是否成功.
Do test the success of the keyboard way by moving the PictureBox
to see if the DGV
moves with it..
更新:
-
如果将
DGV
嵌套在本身嵌套在Panel
中的PictureBox
中,则一切都会好起来,并且结果看起来就像您看到的一样.
If you nest the
DGV
in thePictureBox
, which itself is nested in thePanel
, all will be well and the result will looks just like what you see.
但是,如果您将两者和 DGV
和 PictureBox
嵌套在 Panel
中,则只允许它们重叠,将会出现一个奇怪的错误:反向中绘制了多个重叠控件,请参见已记录
However, if you nest both DGV
and PictureBox
in the Panel
and merely let them overlap, a strange bug will appear: Multiple overlapping controls are drawn in reverse, See here for a post and a link about this issue, which is documented
容器内的控件以相反的顺序呈现.
Controls inside containers are rendered in reverse order.
但显然仍然是一个错误.
but obviously still a bug.
这篇关于如何捕获控件中的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!