【发布时间】:2012-02-29 04:26:08
【问题描述】:
我正在使用 WinForms 和 .Net 2.0,我正在使用 Staffdotnet.collapsiblepanel dll 创建一个可折叠面板,我想在面板标题中添加一个背面图像。 我已经可以在面板标题中更改背景颜色,但我不知道如何使用图像来做到这一点。
【问题讨论】:
标签: c# .net winforms panel collapse
我正在使用 WinForms 和 .Net 2.0,我正在使用 Staffdotnet.collapsiblepanel dll 创建一个可折叠面板,我想在面板标题中添加一个背面图像。 我已经可以在面板标题中更改背景颜色,但我不知道如何使用图像来做到这一点。
【问题讨论】:
标签: c# .net winforms panel collapse
如果不对StaffDotNot.CollapsiblePanel 库本身(找到here)进行细微更改,我认为您无法做到这一点。
在CollapsiblePanel.Designer.cs 中,您将在partial class 声明的末尾看到以下声明:
private System.Windows.Forms.Panel titlePanel;
private System.Windows.Forms.PictureBox togglingImage;
private System.Windows.Forms.ImageList collapsiblePanelImageList;
private System.Windows.Forms.Label lblPanelTitle;
您需要将声明 private System.Windows.Forms.Panel titlePanel; 修改为 public System.Windows.Forms.Panel titlePanel;。这将允许您从库下载中包含的测试项目执行以下代码:
namespace StaffDotNet.CollapsiblePanel.Test
{
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
this.collapsiblePanel1.titlePanel.BackgroundImage = Image.FromFile(@"GreenBubbles.jpg");
}
}
}
使用此示例(替换您自己的图像),产生以下输出:
但是,这可能不是您想要进行的最佳更改(将整个 titlePanel 对象暴露给您的类)。相反,将property 添加到获取和设置背景图像的 CollapsiblePanel 类定义中可能更有意义(同时将titlePanel 成员保留为private)
//CollapsiblePanel.cs
#region Properties
...
/// <summary>
/// Gets or sets the the background image used in the panel title
/// </summary>
[Category("Collapsible Panel")]
[Description("Gets or sets the background image used in the panel title")]
[DisplayName("Panel Title Background Image")]
public Image PanelBackgroundImage
{
get { return titlePanel.BackgroundImage; }
set { titlePanel.BackgroundImage = value; }
}
#endregion
//frmTest.cs
namespace StaffDotNet.CollapsiblePanel.Test
{
public partial class frmTest : Form
{
public frmTest()
{
InitializeComponent();
this.collapsiblePanel1.PanelBackgroundImage = Image.FromFile(@"GreenBubbles.jpg");
}
}
}
【讨论】: