//when button is clicked
this.BackgroundImage = new Bitmap(@"file_location");
无论如何,我可以创建一个Base类并更改
的文件位置 this.BackgroundImage = new Bitmap(@"file_location");
在我将要打开的表格上,以便根据选择的图像,所有表格将共享相同的背景图像
答案 0 :(得分:2)
如果要在按钮单击后更改应用中所有表单的背景图像,请尝试以下操作:
public class BaseForm: Form
{
private static Bitmap _skin = new Bitmap(@"default");
// base Form stores static image which should be set as background for derived forms
public static Bitmap Skin
{
get { return _skin; }
set { _skin = value; OnSkinChanged(EventArgs.Empty); }
}
// when current image changes, event is raised
static event EventHandler SkinChanged;
static void OnSkinChanged(EventArgs e)
{
if (SkinChanged!=null)
SkinChanged(null, e);
}
public BaseForm()
{
InitializeComponent();
// all derived forms are subscribed to event of changing background image
// they update own background image
BaseForm.SkinChanged += SetSkinHandler;
// set background when form created
if (!DesignMode) // do not change in design mode (optional check)
SetSkin();
}
private void SetSkinHandler(object sender, EventArgs e)
{
SetSkin();
}
private void SetSkin()
{
this.BackgroundImage = BaseForm.Skin;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
BaseForm.SkinChanged -= SetSkinHandler;
}
// default
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
在按钮中设置图像单击处理程序(某些来自设置菜单)
private void SkinButtonClick(object sender, EventArgs e)
{
BaseForm.Skin = new Bitmap(@"file_location");
}
答案 1 :(得分:0)
可能的解决方案:以某种值保存图像的颜色或路径。
当打开一些新表单时,将此值作为参数传递给它的构造函数。并在init设置BackgroundImage
。
以第一种形式:
var backGround = String.Empty;
private void SelectBackGround(){
//some stuff
backGround = imgPath;
}
protected void btn_click(...){
var form = new Form2(backGround);
form.Show();
}
在Form2中:
public Form2(string img){
//init controls
this.BackgroundImage = new Bitmap(img);
}
像这样......