我正在用 C# 创建一个 WinForm 程序,我想使用 PNG 或其他图像文件自定义它的皮肤或界面。我不仅关心按钮,还关心它的背景、进度条和其他一些控件。我从我的大部分研究中意识到,TransparentKey 用于不规则形状,然后面板支持 PNG 透明度。
我的问题:我想知道是否有更好的方法,实际和推荐的方法来为我的程序实现自定义接口。
我当前的代码使用 BackgroundImage 属性来替换图像。然而,我确实相信这不是我应该实施的方式。
这是我当前的代码:
public List<Image> BaseImage = new List<Image>();
public List<Image> ClickedImage = new List<Image>();
public List<object> ControlAction = new List<object>();
public List<Image> HoverImage = new List<Image>();
private int counter = 0;
public Control CreatePanel(Image BaseImage, Image HoverImage, Image ClickedImage,
string Name, int x, int y, ButtonAction action)
{
this.BaseImage.Add(BaseImage);
this.HoverImage.Add(HoverImage);
this.ClickedImage.Add(ClickedImage);
int width = BaseImage.Width;
int height = BaseImage.Height;
Actions Action = new Actions();
EventHandler SelectedAction = null;
switch (action)
{
case ButtonAction.Cancel:
SelectedAction = Action.Cancel;
break;
case ButtonAction.Exit:
SelectedAction = Action.Exit;
break;
case ButtonAction.Minimize:
SelectedAction = Action.Minimize;
break;
case ButtonAction.Open:
SelectedAction = Action.Open;
break;
case ButtonAction.Pause:
SelectedAction = Action.Pause;
break;
case ButtonAction.Start:
SelectedAction = Action.Start;
break;
}
Simplify.Invoke(() =>
{
this.newPanel.SuspendLayout();
this.newPanel.Location = new System.Drawing.Point(x, y);
this.newPanel.Name = Name;
this.newPanel.TabIndex = counter;
this.newPanel.Size = new System.Drawing.Size(width, height);
this.newPanel.BackColor = Color.FromArgb(0, 255, 255, 255);
this.newPanel.BackgroundImage = this.BaseImage[counter];
this.newPanel.Click += new EventHandler(SelectedAction);
this.newPanel.MouseEnter += new EventHandler(PanelHover);
this.newPanel.MouseDown += new MouseEventHandler(PanelClicked);
this.newPanel.MouseLeave += new EventHandler(PanelLeave);
this.newPanel.ResumeLayout(false);
});
ControlAction.Add(action);
counter += 1;
return newPanel;
}
private void PanelClicked(object sender, MouseEventArgs e)
{
this.newPanel.SuspendLayout();
this.newPanel.BackColor = Color.FromArgb(0, 255, 255, 255);
this.newPanel.BackgroundImage = ClickedImage[newPanel.TabIndex];
this.newPanel.Size = new Size(ClickedImage[newPanel.TabIndex].Width, ClickedImage[newPanel.TabIndex].Height);
this.newPanel.ResumeLayout(false);
}
private void PanelHover(object sender, EventArgs e)
{
this.newPanel.SuspendLayout();
this.newPanel.BackColor = Color.FromArgb(0, 255, 255, 255);
this.newPanel.BackgroundImage = HoverImage[newPanel.TabIndex];
this.newPanel.Size = new Size(HoverImage[newPanel.TabIndex].Width, HoverImage[newPanel.TabIndex].Height);
this.newPanel.ResumeLayout(false);
}
private void PanelLeave(object sender, EventArgs e)
{
this.newPanel.SuspendLayout();
this.newPanel.BackColor = Color.FromArgb(0, 255, 255, 255);
this.newPanel.BackgroundImage = BaseImage[newPanel.TabIndex];
this.newPanel.Size = new Size(BaseImage[newPanel.TabIndex].Width, BaseImage[newPanel.TabIndex].Height);
this.newPanel.ResumeLayout(false);
}
我上面的代码创建了一个用作按钮的面板。它的动作取自一个名为Actions的类,它基本上只是在用户单击它时分配要分配的方法。我通过替换 BackgroundImage 来更改控件状态,同时更改其大小以避免裁剪区域。
提前致谢。
附加信息 - 我正在使用 .NET Framework 2.0