简体   繁体   English

C#-图片的自定义界面

[英]C# - Custom Interface from Image

I'm creating a WinForm program in C# and I want to customize its skin or interface using PNG or other image files. 我正在用C#创建WinForm程序,我想使用PNG或其他图像文件来自定义其外观或界面。 I'm not only concerned at the buttons but also its background, progress bar, and other few controls. 我不仅关心按钮,还关心它的背景,进度条和其他一些控件。 I realize from most of my research that TransparentKey is used for an irregular shape form then panel supports PNG transparency. 从我的大部分研究中,我意识到TransparentKey用于不规则形状的表单,然后面板支持PNG透明性。

My question: I was wondering if there's a better way, the actual and recommended way, to implement the custom interface for my program. 我的问题:我想知道是否有更好的方法(实际和推荐的方法)为程序实现自定义接口。

My current code uses the BackgroundImage property to replace the image. 我当前的代码使用BackgroundImage属性替换图像。 Yet I do believe this is not the way I should be implementing this. 但是我确实相信这不是我应该执行此方法的方式。

Here's a piece my current code: 这是我当前的代码:

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);
}

My code above creates a Panel which functions as a button. 我上面的代码创建了一个面板,它充当按钮。 Its action is taken from a class called Actions which basically just assign the method to be assigned when the user clicks it. 它的操作来自名为Actions的类,该类基本上只是在用户单击时分配要分配的方法。 I change the control state by replacing its BackgroundImage, at the same time changing its size to avoid getting cropped area. 我通过替换其BackgroundImage来更改控件状态,同时更改其大小以避免裁切区域。

Thanks in advance. 提前致谢。

Additional Information - I'm using .NET Framework 2.0 附加信息 -我正在使用.NET Framework 2.0

My question: I was wondering if there's a better way, the actual and recommended way, to implement the custom interface for my program. 我的问题:我想知道是否有更好的方法(实际和推荐的方法)为程序实现自定义接口。

I would recommend using WPF (Windows presentation foundation). 我建议使用WPF(Windows演示基础)。 It offers highly customizable controls and also supports hardware accelerated graphics. 它提供了高度可定制的控件,还支持硬件加速的图形。

With winforms, you will get flickering screen, issues with resize and redraw which you will not face with wpf. 使用winforms,您将获得闪烁的屏幕,调整大小和重绘的问题,而wpf则不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM