如何动态制作图片盒? [英] How to dynamically produce pictureboxes ?

查看:106
本文介绍了如何动态制作图片盒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(1)

我正在使用Windows窗体应用程序。



我想在点击图片框时创建另一个图片框。

这是我的代码

(1)
I am working on windows form application.

I want to create another picturebox when click in a picturebox.
Here is my code

            PictureBox p = (PictureBox)sender; // Picturebox in which user clicks
            PictureBox pb = new PictureBox();

// Set the properties of new picturebox as similar to that of clicked one
            pb.Image = p.Image;
            pb.Location = new Point(100,100);
            pb.Size = p.Size;



但它没有在指定位置显示新的图片框。



(2)

此外,有没有办法创建半透明的图片框?



(3)如何在下一个图片框中使图片框透明?将图片框的背景颜色设置为透明不起作用?

推荐答案

那么,你的UI应如何知道这张图片盒子甚至存在? :-)

要显示控件,还要设置其父级。比如,你有一些控制父;可以是任何容器控件: Panel TabPage 表单。然后

Well, and how your UI is supposed to "know" that this picture box even exists? :-)
To show a control, also set its parent. Say, you have some Control parent; which can be any container control: Panel, TabPage, Form. Then
p.Parent = parent; // adds this control to parent
// same as:
parent.Control.Add(p);





-SA


1)将新的图片框添加到控制列表 [ ^ ]表格(或原始图片的父母)

2-3)半透明并不简单,但可以做到: http:// bobpowell .net / transcontrols.aspx [ ^ ]
1) Add the new picturebox to the control list[^] of the form (or of the original pictures's parent)
2-3) Translucency is not simple, but can be done: http://bobpowell.net/transcontrols.aspx[^]


PictureBox p = (PictureBox)sender; // Picturebox in which user clicks
PictureBox pb = new PictureBox();

// Set the properties of new picturebox as similar to that of clicked one
pb.Image = p.Image;

// [1]
p.Parent.Controls.Add(pb);

pb.Location = new Point(100,100);
pb.Size = p.Size;

[1] 假设你想要新的PictureBox从源PictureBox复制的属性出现在源PictureBox所在的坐标空间中的位置100,100处:您需要将新PictureBox添加到源PictureBox所在的父控件的Control Collection中。执行此操作后,您可以说源PictureBox和新PictureBox都具有相同的Parent。



将控件的父属性设置为Container Control具有相同的功能使用'容器控件的'添加方法'控件属性(这是一个ControlCollection对象)的效果。



如果你在运行时创建多个PictureBox,我建议制作一个可以用作基础:的UserControl,这样,每次创建一个新的视觉外观属性都会自动相同。或者,您可以创建一个继承自PictureBox的自定义控件。



如果您使用这样的工厂方法,并跟踪最后创建的控件,那么,如果你愿意,你可以自动定位下一个创建的相对于之前创建的那个。



Windows Forms中的透明度是一项棘手的业务,它本身就是有限;如果你真的想使用透明度,我建议使用WPF。

[1] Assuming you want to the new PictureBox whose Properties you copy from the source PictureBox to appear at the location 100,100 in the co-ordinate space in which the source PictureBox is located: you need to add the new PictureBox to the Control Collection of the Parent Control in which the source PictureBox exists. After you do that, you can say that both the source and new PictureBoxes have the same Parent.

Setting the Parent Property of a Control to a Container Control has the same effect as using the 'Add method of a Container Control's 'Controls Property (which is a ControlCollection object).

If you are creating multiple PictureBoxes at run-time, I'd suggest making a UserControl that you could use as a "base:" that way, all the visual-appearance properties would be automatically identical every time you created a new one. Or, you could make a Custom Control that inherited from PictureBox.

If you use such a "factory method," and keep track of the last Control created, then, if you wish, you can automatically position the next one created relative to where the previously created one is.

Transparency in Windows Forms is a tricky business, and it is inherently limited; if you really want to work with transparency, I suggest WPF.


这篇关于如何动态制作图片盒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆